Scheduling periodic tasks

From ArmadeusWiki
Jump to: navigation, search

On Linux systems, when it's a matter of programming periodic tasks, the utility of choice is: cron.

Installation

  • It should be done by default on your APF. If not:
$ make busybox-menuconfig
Miscellaneous Utilities  --->
    [*] crond
    [ ]   Support option -d to redirect output to stderr
    [ ]   Report command output via email (using sendmail)
    (/var/spool/cron) crond spool directory
    [*] crontab

First usage

  • Create a script on your APF to automatically start cron daemon at each boot, for example /etc/init.d/S50crond:
#!/bin/sh
 
case "$1" in
        start)
                echo -n "Starting crond: "
                mkdir -p /var/spool/cron/crontabs
                start-stop-daemon -S -q -b -m -p /var/run/crond.pid --exec /usr/sbin/crond
                [ "$?" -eq 0 ] && echo "OK" || echo "KO"
        ;;

        stop)
                echo "Stopping crond"
                start-stop-daemon -K -q -p /var/run/crond.pid
                [ "$?" -eq 0 ] && echo "OK" || echo "KO"
        ;;
 
        restart)
                $0 stop
                $0 start
        ;;
 
        *)
                echo "Usage: $0 {start|stop|restart}"
                exit 1
        ;;
esac
 
exit 0
  • Give it executable rights:
 # chmod a+x S50crond 

Usage

  • When cron daemon is started you can ask it to do periodic tasks with the help of "crontabs". To edit a crontab do:
 # crontab -e
 * * * * * logger -t CRON "Hello World !"
  • then by looking at /var/log/messages you should see your message printed each minute:
# tail -f /var/log/messages
...
Apr  9 21:42:01 armadeus cron.info crond[573]: crond: USER root pid 605 cmd logger -t CRON "Hello World !"
Apr  9 21:42:01 armadeus user.notice CRON: Hello World !
...
Apr  9 21:43:01 armadeus cron.info crond[573]: crond: USER root pid 607 cmd logger -t CRON "Hello World !"
Apr  9 21:43:01 armadeus user.notice CRON: Hello World !
  • another example to poweroff your APF everyday at midnight (if your APF can control its power supply):
0 0 * * * poweroff
  • By defaults crontabs are stored in /var/spool/cron/crontabs/ and therefore are lost when you powerdown the APF (/var is located in RAM by default on APF).

Links