When running a server it is often nice to be able to run specific tasks at pre-defined intervals. Sometimes it is nice to be able to strip logs every day, check for internet connection every 5 minutes or do something else every XX minutes/hours/days/weeks/months.
After following this tutorial you will be left with an easy-to-use crontab based system, that enables you to run tasks at specified intervals, with no knowledge about cron.
An OpenWRT Compatible device
Configuring cron
Since cron is a tool that runs stuff at specific scheduled times, the first thing that is needed, is to set the time in the router correctly.
You might have noticed, that every time the router is rebooted, the date and time is set to January 1st 2000 at 00:00. This we have to do something about
For setting the time at boot, we can use rdate which is provided with BusyBox. Create a script that runs rdate just after the network and dns servers have been initialized.
I have created the file /etc/inid.d/S60rdate containing:
#!/bin/sh rdate -s sunsite.auc.dk
Remember to make it executable
chmod +x /etc/init.d/S60rdate
This will set the system clock to GMT time with no correction for Daylight Savings (or "summertime" as we call it in Denmark).
In order to configure for the right timezone, you need to take a look at the
TimeZone Table over at
www.openwrt.org
Since I am in Denmark, I use the one from Copenhagen, Denmark:
echo "CET-1CEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00" > /etc/TZ
Now running /etc/S60rdate or rebooting your router, will set the time correctly according to your current timezone.
Now we are ready to configure cron.
The cron deamon (crond) is a standard part of OpenWRTs version of BusyBox. This means that there is nothing to install, just a few things to setup.
Since crontab can be a pain to use, lets create a few dirs, where we can place scripts that can be ran at specified times
mkdir /etc/cron.5mins
mkdir /etc/cron.hourly
mkdir /etc/cron.daily
mkdir /etc/cron.weekly
mkdir /etc/cron.monthly
In the above directories .sh scripts can now be placed, and after following the remaining few steps of this guide, they will run at pre-specified points in time
Now you need to create the file: /etc/crontabs/root . The file should contain something like this :
# Syntax for lines is : minute hour day month dayofweek command #
*/5 * * * * /usr/bin/run-parts /etc/cron.5mins
01 * * * * /usr/bin/run-parts /etc/cron.hourly
02 4 * * * /usr/bin/run-parts /etc/cron.daily
22 4 * * 0 /usr/bin/run-parts /etc/cron.weekly
42 4 1 * * /usr/bin/run-parts /etc/cron.monthly
As also noted in the file, the syntax for each line is :
minute hour day month dayofweek command
To take an example, the command: /usr/bin/run-parts /etc/cron.weekly is executed at time :
minute : 22
hour : 4
day : *
month : *
dayofweek : *
Where "*" is a wild-card meaning "ALL".
Thus the command is running : Every day of week, Every Month, Every Day at 4:22
Note that cron uses a 24h clock.
This means that 4:22 is 4:22 AM, 4:22 PM would be indicated as :
22 16 * * 0 root /usr/bin/run-parts /etc/cron.weekly
The special */5 in the first line, means that this job is executed every 5 mins. At the minutes 5, 10, 15.....55.
Having created this config-file, all that is needed is the command "run-parts" which is not a part of the firmware.
For this purpos I created a small script which I named run-parts and placed in /usr/bin
A script like this will do the job:
#!/bin/sh
#
# runparts.sh by
This e-mail address is being protected from spam bots, you need JavaScript enabled to view it
# intended for use with cron
#
# based on rc.unslung by unslung guys :-)
#
if [ -z "$1" ]
then
echo "Usage : $0 "
fi
RUNDIR=$1"/*"
for i in $RUNDIR ;do
# Ignore dangling symlinks (if any).
[ ! -f "$i" ] && continue
case "$i" in
*.sh)
# Source shell script for speed.
(
trap - INT QUIT TSTP
set start
. $i
)
;;
*)
# No sh extension, so fork subprocess.
$i start
;;
esac
done
Remember to chmod the script to be executable :
chmod +x /usr/bin/run-parts
All that is left to do, is to ensure the cron system get stated at boot-time.
Create the file /etc/init.d/S61crond containing:
#!/bin/sh
crond -c /etc/crontabs -b
Make the file executable :
chmod +x /etc/init.d/S61crond
This should be it!
In order to test the cron-system, you could try to make a small script in /etc/cron.5mins called test.sh, like this :
#!/bin/sh
date >> /tmp/crontest.txt
Remember to make it excutable :
chmod 755 /etc/cron.5mins/test.sh
Try to reboot your router and let it run for some 10 or 15 minuts. Then you should see a list of timestamps in :
/tmp/crontest.txt
showing when the cron has been running the script
I hope you found this tutorial useful.
Comments are welcome to
This e-mail address is being protected from spam bots, you need JavaScript enabled to view it