Run anacron as user

Posted: 18/04/2012 in Shell tips
Tags: ,

Anacron is a periodic command scheduler. It executes commands at intervals specified in days. Unlike cron, it does not assume that the system is running continuously. It can therefore be used to control the execution of daily, weekly and monthly jobs (or anything with a period of n days), on systems that don’t run 24 hours a day. When installed and configured properly, Anacron will make sure that the commands are run at the specified intervals as closely as machine-uptime permits.
Anacron jobs are executed as root but there is a way to execute anacron jobs as a user if you for example need to have ssh keys involved or just if you are not that comfortable running your scripts as root. Let’s start… (replace grinux with your-username)
mkdir /home/grinux/.anacron

cd ~
mkdir .anacron
cd .anacron/
mkdir cron.daily cron.weekly cron.monthly spool etc
pico etc/anacrontab

Add this to the anacrontab file:

# 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 daily-cron nice run-parts --report /home/grinux/.anacron/cron.daily
7 10 weekly-cron nice run-parts --report /home/grinux/.anacron/cron.weekly
@monthly 15 monthly-cron nice run-parts --report /home/grinux/.anacron/cron.monthly

now place your scripts whithin the cron.daily, cron.weekly and cron.monthly directories. Script files must have 755 permissions and start with
#!/bin/sh

Then add the following line at the end of your .profile to run it automatically each time you log in in your pc:

pico ~/.profile
/usr/sbin/anacron -t /home/grinux/.anacron/etc/anacrontab -S /home/grinux/.anacron/spool &> /home/grinux/.anacron/anacron.log

Done  ; )

Advertisements
Comments
  1. cinatic says:

    Nice! Unfortunately emails are not send after execution. Could not find the problem yet…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s