Table of Contents
If you want to send us your comments, please do so. Thanks
More on comments
Anacron
Run a command with a certain interval in hours, days, weeks and months if the computer is running .
Only @monthly is a valid '@' interval designator.
@reboot is not valid.
Run times
Anacron runs:
Interval | Time |
---|---|
Hourly | x:17 |
Daily | 6:25 |
Weekly | Sunday on 6:47 |
Monthly | On the 1st of the month on 6:52 |
From /etc/crontab:
17 * * * * root cd / && run-parts --report /etc/cron.hourly 25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily ) 47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly ) 52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
Main rule
Always put the code in a script
Start the script with
#! /bin/bash
Then call the script from anacron. If you do not use a script you can get something like
/bin/sh: 1: [[: not found
in the case you used an [[ statement for example
Servers
On servers do not user anacron, use cron
Alternative
Put scripts in a file in /etc/cron.daily, /etc/cron.weekly or /etc/cron.monthly
- They are executed after the anacrontab file is processed
- Scripts in these directories are processed in alphanumerical order. Scripts starting with a number first
Restart service
systemctl restart anacron.service
Configuration files
File | Remark |
---|---|
/home/user/.local/anacrontab | For user specific entries |
/etc/anacrontab | Put the non user entries in this file |
/etc/cron.d/anacron | |
/etc/default/anacron | |
/etc/init.d/anacron |
Environment
Syntax: VAR=VALUE
Command | Remark |
---|---|
SHELL=/bin/bash | |
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin | |
HOME=/root | Or used in crontab only? |
LOGNAME=root | Used in the From: field the e-mail. Username, may not be changed. Or used in crontab only? |
MAILTO=user | User to whom the output of anacron has to be mailed |
START_HOURS_RANGE=10-12 | In this time frame jobs are started. If missed they are skipped for that day |
RANDOM_DELAY=delaytime | 0: off. Other value: Add a delay of 0 to delaytime minutes to the delay time (see commands section) |
Comments
- Blank lines
- White-space only lines
- White-space followed by a '#' followed by a comment
Commands
Option | Explanation |
---|---|
period | days |
delay | minutes |
job-identifier | Make up yourself. “ ” (space) and “/” are not allowed |
command | Any shell command or script. Field separation with blank spaces or tabs |
Syntax | Remark |
---|---|
period delay job-identifier command | period is in days |
monthly delay job-identifier command | daily nor weekly are possible. Use the previous syntax with a period of 1 or 7 |
Command continuation
Place a '\' and continue the command on the next line
4 10 backupSomething cp /home \ /backup
Options - --
Short, -, nor long, --, options are allowed. There will be an error message: “/bin/sh: 0: Illegal option -”, (long --)
Solution: write a script and have anacron run it
Examples
# Comment 4 10 backupSomething cp /home /backup monthly 20 backupSomething cp /home /backup # An other comment
Test
Run as root
Command | Explanation |
---|---|
anacron -T | Test the anacrontab file |
anacron -T -t /home/user/.local/anacrontab | Test the local user's anacrontab file |
anacron -nf | Run all commands now regardless the timestamp |
Last run date
Check the date when an anacron job has last run in /var/spool/anacron/jobname. The format is yyyymmdd (may depend on the locale setting)
Run in user mode
anacron needs root privileges to run. You can run it in user mode. Do
- mkdir -p /home/user/.local/spool/anacron
- crontab -e and add:
@reboot /usr/sbin/anacron -s -t $HOME/.local/anacrontab -S $HOME/.local/spool/anacron @hourly /usr/sbin/anacron -s -t $HOME/.local/anacrontab -S $HOME/.local/spool/anacron @daily /usr/sbin/anacron -s -t $HOME/.local/anacrontab -S $HOME/.local/spool/anacron
@hourly runs at every full hour: [0-24]:00
@daily runs very day at midnight: 0:00
man pages
- man 8 anacron
- man 5 anacorntab
- man run-parts (run scripts or programs in a directory)
- man cron
Useful links
Why would anacron not be running
Main subjects on this wiki: Linux, Debian, HTML, Microcontrollers, Privacy
RSS
Disclaimer
Privacy statement
Bugs statement
Cookies
Copyright © : 2014 - 2023 Webevaluation.nl and the authors
Changes reserved.