17 December 2010

Anacron for a user

Normally, anacron is launched by root. And here is how a user can launch his/her own anacron jobs.

Prepare file and dirs

$ mkdir -p ~/etc/cron.daily ~/etc/cron.weekly ~/etc/cron.monthly \
$ cp /etc/cron.daily/0anacron ~/etc/cron.daily/
$ cp /etc/cron.daily/.placeholder ~/etc/cron.daily/
$ cp /etc/cron.weekly/0anacron ~/etc/cron.weekly/
$ cp /etc/cron.weekly/.placeholder ~/etc/cron.weekly/
$ cp /etc/cron.monthly/0anacron ~/etc/cron.monthly/
$ cp /etc/cron.monthly/.placeholder ~/etc/cron.monthly/
$ find ~/etc/cron.* -name 0anacron -exec chmod u+x {} \;



1   5   cron.daily   nice run-parts --report /etc/cron.daily
7   10  cron.weekly  nice run-parts --report /etc/cron.weekly
@monthly    15  cron.monthly nice run-parts --report /etc/cron.monthly
Replace MAILTO value with your mailbox. If mailx package installed, you should receive anacron job error outputs by E-mail.

Make anacron launched on login

$ echo "
/usr/sbin/anacron -t \$HOME/etc/anacrontab -S \$HOME/var/spool/anacron
" >> ~/.bash_profile
Note, if ~/.profile exists, ~/.bash_profile may be ignored(as in my case). I wonder, why? Bash man page bash(1) claims:
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.
Put your scripts in ~/etc/cron.daily, ~/etc/cron.weekly and ~/etc/cron.monthly directories. Note, that script file names must consist entirely of upper and lower case letters, digits, underscores, and hyphens, since we launch them through run-parts(see man run-parts 8). In case of custom fienames you should apply --regex option.