5 UNIX / Linux Anacron Command Example for Background Jobs

The main advantage of anacron is that you don’t need to have the system be up and running always to execute the background jobs.

This is very helpful when you want to run background jobs on laptops, which might not be available 24×7. But, you still can schedule routine jobs on these machines using anacron.

This way whenever the laptop starts again, any pending scheduled jobs will be executed by anacron

1. Test Anacron Syntax

All anacron jobs are located under /etc/anacrontab file as shown below:

# cat /etc/anacrontab
1       5       cron.daily              nice run-parts /etc/cron.daily
7       25      cron.weekly             nice run-parts /etc/cron.weekly
@monthly 45     cron.monthly            nice run-parts /etc/cron.monthly
@bimonthly 45   cron.bimonthly          nice run-parts /etc/cron.bimonthly

In the above example, the last line is invalid, as the timeperiod “bimonthly” it not support by anacron.

So, you can check for any syntax error, or other issues with the anacrontab file using the following command.

This output indicates that we have an unknown name period in the file.

# anacron -T
anacron: /etc/anacrontab: Unknown named period on line 17, skipping

If you have your own crontab file, you can test that syntax using -T -t and specifying your custom anacrontab file name as shown below.

# anacron -T -t /etc/myanacron
anacron: Invalid syntax in /etc/myanacron on line 14 - skipping this line
anacron: Invalid syntax in /etc/myanacron on line 15 - skipping this line

2. Custom Spool directory

Apart from specifying your own anacrontab file, you can also specify your own spool directory for anacron. This is helpful when you are testing some anacron jobs, and don’t want to messup the standard anacrontab file and spool directory.

The following uses /root/myspool to store all the timestamp information for the jobs that are defined in the /etc/myanacron file.

# anacron -S /root/myspool -t /etc/myanacron

# ls -altr /root/myspool/
total 8
dr-xr-x---. 9 root root 4096 Jan 31 23:35 ..
-rw-------. 1 root root    0 Jan 31 23:36 myjob
drwxr-xr-x. 2 root root 4096 Jan 31 23:36 .

The default spool directory is /var/spool/anacron as shown below, which stores the timestamp for the jobs from the /etc/anacrontab file.

# ls -l /var/spool/anacron
total 16
-rw-------. 1 root root 9 Jan 31 23:56 cron.daily
-rw-------. 1 root root 9 Jan 31 23:56 cron.monthly
-rw-------. 1 root root 9 Jan 31 23:56 cron.weekly

They typically store the information on when was this particular job last executed:

# cat /var/spool/anacron/cron.daily
20140131

# cat /var/spool/anacron/cron.monthly
20140131

3. Run Anacron in the Foreground

You can also run the anacron in the foreground. Sometime this is easy, as you can see the output on the screen itself. This is good for checking something very quick during testing purpose.

For example, when you don’t give -d option, anacron forks a background jobs, and it will just not display anything on the screen. This example, just updates the timestamp for the jobs, but really doesn’t execute anything. You won’t know what really happened.

# anacron -u

But, when you run the anacron in the foreground, using -d option, you can see what it is doing. Don’t do this when you have jobs that will run for long period of time.

# anacron -d -u
Updated timestamp for job `cron.daily' to 2014-01-31
Updated timestamp for job `cron.weekly' to 2014-01-31
Updated timestamp for job `cron.monthly' to 2014-01-31

4. Force the Job Execution

The -f option will do a force execution of the jobs. But this will still consider the delay specs that you’ve set in the anacrontab file.

# anacron -d -f
Anacron started on 2014-01-31
Will run job `cron.daily' in 24 min.
Will run job `cron.weekly' in 44 min.
Will run job `cron.monthly' in 64 min.

However, -n option will execute the job immediately. This will not consider any delay specs that you’ve set in the anacrontab file. Please note that -n option also implies -s option.

# anacron -d -n
Anacron started on 2014-01-31
Checking against 0 with 31
Normal exit (0 jobs run)

5. Specify a Job Name

If you have defined multiple jobs in the anacrontab file, you can execute only a specific job by specifying its job name.

In the following example, it will execute only the job with the name “myjob” from the custom /etc/myanacron tab file.

# anacron -f -d -t /etc/myanacron myjob
Anacron started on 2014-01-31
Will run job `myjob' in 1 min.

Job `myjob' started
Job `myjob' terminated
Normal exit (1 job run)

Comments on this entry are closed.