Run Anacron as a user from user directory
Anacron
Anacron lets you schedule scripts for say daily or weekly run without stating the time of the day they should run. This is especially useful if you have a computer that is not running 24/7.
Normally (well, in Ubuntu at least) you put the scripts you want to be run in /etc/cron.daily and anacron is taking care of the the rest. This script get executed as root. This might be a problem if you for example need to have ssh keys involved or just if you are not that comfortable running the script as root.
I solution I found around the web[1][2] works as follows: Create the necessary directories:
mkdir $HOME/.anacron
mkdir $HOME/.anacron/cron.daily
mkdir $HOME/.anacron/cron.weekly
mkdir $HOME/.anacron/timestamps
Where the timestamps directory is used as the spool directory of anacron later. Create the following file $HOME/.anacron/anacrontab with the following content:
# See anacron(8) and anacrontab(5) for details.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# These replace cron's entries
1 5 user.daily nice run-parts --report $HOME/.anacron/cron.daily
7 10 user.weekly nice run-parts --report $HOME/.anacron/cron.weekly
# eof
Then add the following line to your $HOME/.profile (to run it automatically each time you log in) or run it manually (each time you login):
/usr/sbin/anacron -t ${HOME}/.anacron/anacrontab -S ${HOME}/.anacron/timestamps &> ${HOME}/.anacron/anacron.log
To have it run like a “normal” anacron task by the system’s anacron I created the following file: /etc/cron.daily/user-anacron :
#!/bin/bash
HOME="/home/USERNAME"
su USERNAME -c "/usr/sbin/anacron -t ${HOME}/.anacron/anacrontab -S ${HOME}/.anacron/timestamps &> ${HOME}/.anacron/anacron.log"
So it is working like a “normal” anacron task.
[1] http://forum.ubuntuusers.de/topic/wie-genau-funktioniert-anacron/
[2] http://ubuntuforums.org/showpost.php?p=4176860&postcount=4.