Running cron jobs on Raspberry Pi in Raspbmc

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!

  1. 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
  2. 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.
  3. 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:
    Screen Shot 2013-04-05 at 11.48.22 PM
  4. 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 !!‘.
  5. 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:
    Screen Shot 2013-04-06 at 12.00.48 AM

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!

Screenshot 2013-11-10 04.31.52

26 thoughts on “Running cron jobs on Raspberry Pi in Raspbmc

  1. Pingback: How can I keep my Raspbian "Wheezy" up to date? | DL-UAT

  2. Geronimo

    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

    Reply
    1. wexoo Post author

      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!

      Reply
  3. Mayki

    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?

    Reply
    1. wexoo Post author

      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 🙂

      Reply
  4. Pingback: Reverse Tunnels and RaspBMC | Neave Engineering

  5. Jack Davison

    Can you automate a command line “tightvncserver” in the cron restarter, and if so how?

    Reply
    1. wexoo Post author

      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

      Reply
  6. mynameishidden

    I dont have yet my Pi, but i order soon. How can i run php script with cron?

    Reply
  7. Kgeebelen

    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..

    Reply
    1. wexoo Post author

      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 🙂

      Reply
    2. dale

      ive been looking for a way to do that (save attachments off to a directory), would you be willing to share your py script?

      Reply
  8. iemand@nederland.nl

    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. 🙂

    Reply
  9. Pingback: Monitoring a Growatt PV inverter using Raspberry Pi | sisand

  10. Danie

    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?

    Reply
    1. wexoo Post author

      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 🙂

      Reply
    2. wexoo Post author

      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).

      Reply
      1. wexoo Post author

        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!

        Reply
  11. Thiago

    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…

    Reply
    1. wexoo Post author

      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 🙂

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *