Here’s another little tutorial especially for people new to raspberry pi or linux in general (such as myself). Cron jobs are used to execute specific actions at pre-configured times. These actions can be executing scripts, writing data to a file, etc. (would be glad to hear of stuff you do with cron jobs in the comment section ;))
In my specific case I needed a script to map my changing external ip address at home to the domain I specified for my raspberry to have access to it when I’m away. Big thanks for all the help to my good friend Thomas for domain hosting and tipps on how to get this to work! Now let’s get to it!
- Activate cron jobs in Raspbmc
Per default running cron jobs is deactivated in Raspbmc and there are two ways to activate them.
– In the Raspbmc GUI under Programs -> Raspbmc Settings -> System Configuration -> Service Management -> Cronjob Scheduler
– Via SSH/FTP by modifying sys.service.cron value to “true” the settings file under /home/<your_username>/.xbmc/userdata/addon_data/script.raspbmc.settings/settings.xml – for more infos on changing settings via SSH / FTP, check my latest blog post
- Configure your ‘crontab’
All cron jobs are defined in this file and you can specify multiple of them, each on its own line – to edit your crontab in your default text editor, type ~$ crontab -e in the console. By using ~$ crontab -l the content is shown.
- Write your cronjob
The syntax isn’t too easy to understand in the beginning, but there are tools online, which can help you write your (first) cron jobs. In my case the cron job looks like this:
20,40 * * * * sh /dns_update.sh
Every hour at minute xx:20 and xx:40 the dns_update.sh script in the root directory of my raspberry is executed, which just calls a magic dns mapping domain via wget.
Especially in the beginning it’s a good idea to add a test job, which executes every minute and only helps to see if your cron configuration is set up properly. I use the following:
* * * * * echo “crontest $(date) $(whoami)” >> /tmp/crontest.txt
This just writes the current date and the current user to the specified textfile.
Below is a screenshot of my current crontab:
- Control the cron service
In order to get your cron jobs to run, the cron service must be active. The following commands let you control the cron service:
~$ /etc/init.d/cron stop
~$ /etc/init.d/cron start
~$ /etc/init.d/cron restart
These commands will probably need a leading ‘sudo‘ – just check your console output. To quickly repeat your last command with leading sudo type ‘sudo !!‘.
- Check if your cron jobs are running
If you also specified a test cron job like the one above, you can test if it’s working by printing the latest contents of the text file to your console. This can be done by using the ‘tail’ command and the ‘-f‘ parameter updates the output as new content is added to the file. See below screenshot for example output:
And… That’s it! Thanks for reading, hope it helps someone, glad for every kind of feedback! Bye!
Update (10.11.2013): As jazz and CFWhitman pointed out in the comment section, you can also just use the commands /etc/init.d/cron status or service cron status to check if cron jobs are running. For output see screenshot below 🙂 Thanks!