Use Anacron to handle Linux shutdown Problems

Source: Internet
Author: User
Article Title: Use Anacron to handle Linux shutdown problems. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

Linux? Initially, Cron is used to automatically perform regular system maintenance, such as replacing log files and updating junk information filtering rules. This method can serve servers and other systems that run around the clock. However, you cannot run Cron if you want to disable it when you are not using a computer to save resources (typically in desktops and laptops. If you cannot run Cron regularly, a large number of log files are generated and other problems are caused. The Anacron utility provides a solution that allows computers to run regular maintenance jobs after each boot, even if these boot times are not fixed.
Problem

If you run Linux (or most other UNIX systems) on a desktop or laptop, the system may be shut down frequently. Indeed, some systems may be shut down for a longer period of time than the start time. This helps reduce the cost of electricity. For a laptop, if there is no power outlet during the trip, frequent shutdown will help prolong the use of the computer. Unfortunately, this frequent shutdown may cause problems for the Linux system, because Linux uses a utility called Cron To run daily maintenance jobs in the background. When these jobs stop running, it will lead to problems. After a long time, the system performance will get lower and lower.

Cron is designed to start the program at a specified time when it is unattended. Generally, there are two types of Cron jobs (programs run by Cron): system Cron jobs run by the system to execute system maintenance tasks, and user Cron jobs, created by the user to execute user tasks. For these two jobs, the system Cron job is more important. These jobs may include clearing old files in/tmp, replacing log files, updating spam filter rules, and updating the locate database.

Most Linux systems include a series of Cron-related subdirectories in/etc. The/etc/crontab file specifies which jobs should be run by Cron as system Cron jobs. Generally, this file tells Cron to run at/etc/cron every hour, every day, every week, and every month. hourly,/etc/cron. daily,/etc/cron. weekly and/etc/cron. monthly script. The exact time for running these jobs is specified in/etc/crontab. For jobs running every day and running at a longer interval, the running time is usually morning. The problem is that desktop and laptop systems are likely to be shut down at these times.

When the Cron job cannot run, various problems arise. The specific problem depends on the Cron job that should be run, but for example, the problem includes Log File enlargement,/tmp directory accident, and database expiration problems, these databases are used to store available software updates or local system commands.

Obviously, it is very important to keep running Cron jobs. If you need to shut down a system frequently, you should find a way to avoid these problems. Anacron is the solution to achieve this goal.


Start using Anacron

If you use Anacron to benefit your managed system, you should first understand its functions. Then, you can obtain, install, and configure the software, and then link it to the existing Cron configuration.

Basic Principles of Anacron

The purpose of Anacron is not to completely replace Cron. Although Cron may cause some serious problems for systems that are frequently shut down, for jobs that run frequently, such as new mail checks on the remote mail server every hour, cron is still an important tool. Anacron aims to supplement Cron rather than replace it.

Cron runs as a daemon. In contrast, Anacron runs and terminates as a common process. Anacron maintains a set of tasks that should be run, each of which has a relevant operation interval. At each running time, Anacron checks the job list to see if the specified interval of a job has exceeded since the last job was run. If yes, Anacron will run the job.

This design means that Anacron must run regularly. The running Anacron section describes two methods. Each method has its own advantages and disadvantages. Generally, Anacron is run by the root user, but normal users may also use Anacron, as described in Anacron's personal application.

Obtain and install Anacron

Most Linux distributions include Anacron in their software packages. Therefore, you can install the software by entering yum install anacron, apt-get install anacron, or similar content. If your release does not contain the Anacron package, you need to download and install the package from the source code. Unfortunately, the Anacron Web site does not contain a tarball link; you need to use Concurrent Versions System (CVS) for download:


Listing 1. Download Anacron from CVS

Cvs-d: pserver: anonymous@anacron.cvs.sourceforge.net:/cvsroot/anacron login
Cvs-z3-d: pserver: anonymous@anacron.cvs.sourceforge.net:/cvsroot/anacron co-P anacron


Enter the first command and log on to the CVS database as the guest user. This will prompt you to enter the password. Press Enter (password is not required for guest access ). The second command downloads the source code of Anacron to the subdirectory named anacron.

One disadvantage of using CVS to obtain Anacron is that this method is sometimes unreliable. If the software has not been thoroughly compiled, you can try to solve the problem by yourself. Otherwise, wait a day or two until someone else helps you fix the problem. If the problem persists, it is best to track pre-compiled binary files, even if this means looking for files from another release.

A common bug in Anacron is gregor. c: 79: error: assignment of read-only variable 'isleap '. To fix this bug, edit the 79th row of the gregor. c file and delete the const string at the beginning of the row.

After obtaining the source code of Anacron, you can install it according to the following steps (Note: if a pre-compiled binary file has been installed for the release version, you do not need to perform these steps ):

Switch to the Anacron directory created by cvs.
As an optional step, edit Makefile to adjust the software installation directory or other compilation details.
Enter make to build the software.
As root, enter make install to install the software.
After the software is installed, you should continue to configure and run Anacron, whether using the package management of the release or installing it from the source code.

Configure Anacron

Anacron is controlled through the/etc/anacrontab file. This file can contain annotation lines (represented by a leading hashed #), Environment Variable Allocation (such as SHELL =/bin/bash), and job definition. The last one is the most important. They are similar to the following:


Listing 2. Example job definition

1 5 cron. daily run-parts/etc/cron. daily


Each job definition contains four fields:

Running time, in days (1 in the previous example ).
The delay between starting Anacron and running the job, measured in minutes, provided that the time elapsed since the last run has exceeded the running time. You may need to use different operation latencies for the job to prevent all jobs from running at the same time, thus affecting system performance.
Job name (cron. daily in workers ).
The command to run. In tables, the command name is run-parts/etc/cron. daily.
The objective of replacement is to replace the common code lines in the/etc/crontab that runs the daily Cron job. After you set and run Anacron, you should be able to delete the corresponding/etc/crontab line and the system will continue to run normally-if the system is often disabled when running a daily Cron job, after replacement, the system will get better performance.

When configuring Anacron, the first step is to check/etc/crontab and convert its long routine entries to Anacron jobs. However, the Cron job executed every hour should not be converted to Anacron. for jobs with a shorter interval than one day, Anacron is not suitable for replacing Cron. Strictly speaking, when to run an Anacron job is determined by the Anacron startup method you selected, as shown below.


Run Anacron

After configuring Anacron, you can start it now. Since this is a task that must be executed on a regular basis, it is far from enough to input Anacron only once. There are two common ways to run Anacron on a regular basis: You can start Anacron when you start a computer, or start the program through a Cron job. Which method is used depends on how computers are used. Generally, Anacron will process system jobs, but you can also use it to process content equivalent to your Cron job. Therefore, you may need to configure it to run both types of jobs at the same time.

Run Anacron at startup

If you frequently shut down and restart your computer (once or multiple times a day), running Anacron at startup is a good option. After the computer starts, Anacron checks the jobs that should be run and runs them if necessary. In a typical business environment, for desktops, it is very likely that users will run Anacron jobs shortly after their work begins. For a laptop, the running cycle may be more unpredictable (note that "start" means that the entire system is started; if the computer is usually placed in sleep mode rather than shut down, the system startup script will not run, therefore, this method will not run Anacron jobs on a regular basis ).

To run Anacron at startup, you must create a SysV STARTUP script to start the software. The script to complete this task is simple, with the following two lines of code:


Listing 3. Run the Anacron Startup Script

#/Bin/bash
Anacron


You can place this script in/etc/init. d,/etc/rc. d/init. d, or any location of your launch script. You can then create a link to the script in the standard running directory (such as/etc/rc3.d or/etc/rc. d/rc3.d. You should specify a name for this link (such as S99anacron) to ensure that Anacron is started at system startup ("S" will indicate that the system runs this script at startup, "99" indicates when the system should run the script relative to other startup scripts ).

If your release contains an Anacron package, this package may contain the corresponding SysV STARTUP script, so you only need to check to ensure that Anacron runs when you start your computer. You can find the link to the startup script whose name starts with "S", or use tools such as chkconfig or ksysv to adjust the configuration.

After configuring Anacron to run at system startup, you should observe the log files and other features managed by Anacron jobs for a period of time. Check to ensure that the Anacron job runs at the appropriate time (note that each time Anacron runs a log file replacement job, the log file is not necessarily replaced, depending on the logrotate configuration ).

Run Anacron from Cron

If your computer is shut down and restarted less than once in a day, or you are using sleep mode instead of shutting down the computer completely, you need to run Anacron through the Cron job. You can create an entry in your/etc/crontab file to complete this task, as shown below:


Listing 4. Running Anacron's Cron job

17 * root anacron


This entry will run Anacron 17th minutes each hour, it will check the job and run it as necessary. Note that Anacron only runs its job when necessary. Therefore, if the number of running times within an hour is less than once, this is of little significance, unless you can predict the system startup time-and in this case, you may need to use Cron to schedule jobs.

When running Anacron through a Cron job, you are not sure about the running time of Anacron job relative to user behavior. The Anacron job may exhaust the CPU time at an inappropriate time. In contrast, the user runs the Anacron job shortly after the computer is started and runs Anacron in the form of a system startup process.

Anacron personal applications

The previous step assumes that Anacron is used to run the system Cron job. In most cases, however, normal users can also run Anacron to manage personal Cron jobs, which should be executed every day or at a longer interval. The basic principle is the same, but to create a personalized Anacron configuration, you must use the-t parameter to start the program. This will get the file name to replace the configuration file and use a-S parameter, this parameter specifies the directory where Anacron stores its spool file:

Anacron-t ~ /. Anacrontab-S ~ /. Anacron/

You can use this type of call in your personal Cron configuration, or use your own login script to complete the job. In either case, the spool directory must exist, and the Anacron configuration file uses the same format as the system Anacron job. Like the system configuration, the common Cron tool should run once an hour or use a time interval less than one day.


Conclusion

Anacron is a useful tool for computers that do not need to run around the clock. You can move most or all of the Cron jobs executed daily or longer to Anacron to ensure that these jobs run at each startup of the system, or by Cron hourly (or other time intervals) run Anacron to run unpredictable jobs.

Although Anacron is a very useful tool, it also has disadvantages. Most obviously, when you need to complete the work, Anacron may run out of CPU time or damage system performance in other ways. For this reason, it is better to apply Anacron to systems that really need it, such as Laptops and Desktops that often shut down at night, while Cron jobs usually run at night.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.