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!
try this https://hydra20online.com
Thanks for the information here. i found it really useful for fixing my cron problems.
Pingback: How can I keep my Raspbian "Wheezy" up to date? | DL-UAT
Hi Philipp, thanks for the detailed manual for creating a cronjob. I came here because I was searching this, duh. But I was also planning to use cronjobs for posting my external IP to my external domain hosting, through ftp. That way I can access the ssh, websites, etc through my hosting. Would you care to share your python script for doing so?
Kind regards
Hi Geronimo,
glad you liked the tutorial and sorry for my late reply. Regarding my script – it was just a simple sh script calling an url via wget, which did the mapping of the ip addresses. It won’t really help you in any way I’m afraid 🙁 The url was something like that ‘www.austrosolutions.at/dyndns/dnsupdate.php?rid=XXXX&rkey=YYYY’ and I got it from a friend of mine, which manages all my hosting needs 🙂 You’ll have to see if your hoster offers some solution like that I am afraid. Good luck!
Hi,
please advice if you enter the command manually “sudo /home/pi/backup.sh” works. If you enter it into the cron “5 12 * * * /home/pi/backup.sh” does not correctly. Where is my mistake?
Hi Mayki,
I don’t have too much time right now to check this, but it could be a permission issue. Maybe the cron service (running user) doesn’t have enough permissions to execute the backup.sh file. I’m a linux beginner so I can’t give you more details, but you can find many articles online about the permission system. Let me know if you still have problems and I’ll try to look more into it in the coming days 🙂
Pingback: Reverse Tunnels and RaspBMC | Neave Engineering
Can you automate a command line “tightvncserver” in the cron restarter, and if so how?
Hi Jack, I personally don’t know anything about tightvnc server, but I guess if it runs as a linux service, there’s no reason why you shouldn’t be able to restart it via cron 🙂 Just select the interval you see fit (probably with the help of this ) and paste in the command to restart or start it. The Raspberry Pi normally should keep cron enabled even if you restart it or shut it down, so cron just starts running again when booted. If you still have problems, I could look more into it, if you post a bit more info about your setup 😉
Cheers, wexoo
I dont have yet my Pi, but i order soon. How can i run php script with cron?
Hi, I personally don’t know how to execute php scripts but this post should get you going -> http://www.thegeekstuff.com/2011/07/php-cron-job/. Apparently there are different ways to do it – hopefully they will work for you. Have fun with your pi!
Somewhere an hour before the date I took one Levitra 20mg pill. After half an hour I wanted to go to the toilet, just touched my penis and it started growing. At the same time I did not have any excitement and it seemed to me that I somehow feel bad. Thanks to the Canadian online pharmacy I have a good hard erection from just one glance at my sexual partner.
Hi,
Thanks, helped me out a lot!
I’m using cron on my Pi to execute a python script every 15 minutes. The script fetches the mail from a gmail-account I created, downloads the attachments to a certain directory and then removes the read e-mails. Very helpful for sending stuff to my Pi from wherever I am, since I’m not able to provide access to it when I’m not at home..
Nice! Glad it helped! Someday I really wanna learn some scripting languages as well – at least so much that I know what’s possible with ’em 🙂
ive been looking for a way to do that (save attachments off to a directory), would you be willing to share your py script?
Thank you for this very clear explanation of cron! I’ve now got cron working on raspbmc, and I’ve learned a few new commands. 🙂
Thanks for your kind words, glad it helped! 😉
Pingback: Monitoring a Growatt PV inverter using Raspberry Pi | sisand
On my pi the cronscheduler option is missing in the gui.
When looking by ssh the settings file is also missing.
Do I have to add an addon first?
Hi Danie! It should be in the “Raspbmc Settings”-addon under Programs in the homescreen of Raspbmc. It is an addon but it should be installed in every raspbmc installation. I’ve not yet updated to the most recent version of raspbmc – maybe there were some changes to that logic – i’ll reply again, when my raspberry is up-to-date 🙂
Hi again, just updated my raspberry to the latest version of raspbmc – the settings addon is still there. It’s in the same menu item/”tab” (Programs) where you can also find the newly added browser.
Also checked the ssh option – should still work as well. The default username/password for a new raspbmc install is pi/raspberry. This default user should see the file (via SSH or FTP).
…a simple way to see if cron is running…..
/etc/init.d/cron status
Thanks for the tipp! I’ll include it in the post as soon as i got a chance to test it 🙂
Or you can use
service cron status
Thanks, just tried them both – work like a charm! The latter also shows the process id of the cron job, which is the only difference i can see. Included in the post now. Cheers!
Hey Thanks!! Makes a lot of sense enable cron service via SSH. Everybody says to do that via XBMC. I’m away from my TV so how can I do?? SSH saves the day once again…
Thanks man. Help me a lot…
Thanks for your kind words! 🙂 Hehe, yeah I always try to find both ways, as sometimes I don’t wanna switch on my TV just to make a simple setting’s change, especially if I’m already in a console session on it. Definitely good to know your possibilities 😉 Glad you enjoyed it!
PS: Sorry for replying so late, but I didn’t get a mail notification – gotta look into that matter 🙂