Quaintproject

How to schedule a job on the raspberry pi

28 Comments


In this example I would like to restart my raspberry pi every night at 12 pm.
For this usage a crontab can be really handy. If you are interested in how to get it working, follow this guide:

Before starting with the crontab,  make sure to make a

 sudo chmod 755 restart.sh

on your particular script (in my case restart.sh).

The restart.sh script looks like this:

sudo reboot

1.  View your crontab file.

sudo crontab -e

2.Now comes the point where we can schedule our script:

Put this line at the end of the file.


0 23 * * * <path_to_your_script>/restart.sh

For your understanding:


1.M   2.H     3.D     4.M     5.W
1. M = minute  (0 - 59)2. S = hour (0 - 23)3. D = day     (1 - 31)4. M = month   (1 - 12)5. W=weekday (0 - 7) (Sonntag is represented by 0 and 7)

3. Save

Thats it, The script will be running every midnight.

If you have any questions about the crontab or any other Pi Topic, feel free to comment.
Cheers Alex

Advertisements

28 thoughts on “How to schedule a job on the raspberry pi

  1. Is there a way to schedule a task every hour? As far as I can tell, you can only schedule one at a certain time.

    Like

  2. Thanks much for this. It’s very helpful. Is it possible to add multiple scheduled jobs in this one file? Do I just enter each onto its own line or do this some other way?

    Like

  3. Hi when I type this line “sudo chmod 755 restart.sh” I get a “bash: cd: chmod: No such file or directory” Am I doing something wrong here?

    Like

    • Hi Lungelo,
      did you make sure that you’ve saved the restart.sh before? Further notice, that your restart.sh file has to be in the same directory (home directory for example), from which you run the ‘sudo chmod ….’ command. Otherwise you would need to make a ‘cd’ to your appropriate directory. I hope this helps you. 🙂

      Like

  4. Hi I would like to start the recordings cyclical automatic overwrite that file on the same two files

    Like

    • Sorry Francescos for this horrible late reply. Your comment seem to be forgotten. We are eager to help you, but could you please explain what exactly you want to do? Thank you. Cheers 😀

      Like

  5. Hi, can a script be scheduled to only run between certain hours every day? For example to run between 8am and 8pm, then start again the next day etc. Many thanks.

    Like

    • Hi,

      yes that is possible!

      Simply change it to the following:

      0 8-20/1 * * * /restart.sh

      This would restart the pi every hour in the time from 8am to 8pm.

      Another way (less beautiful, but working):

      0 8,9,10,11,12,13,14,15,16,17,18,19,20 * * * /restart.sh

      I hope this helps you.

      Alex

      Like

  6. Hi how do i do a cron job at 6:40pm every day?

    Like

  7. Hi i know this is about cronjob but can you help me with this I want to make a script whereas on boot a script searches for a file that has the raspberry pi serial number and if the file doesn’t have the raspberry pi serial number make the raspberry pi go into a boot loop.

    Like

    • Hey Bobby thanks for your comment. Unfortunatelly I cant provide you a working script, as my pi is currently built in, but what you could do is to create a custom shell script and Start it Form RC.local.

      Like

    • Hey Bobby, you have asked a pretty interesting question. We are almost 100 % sure that a task like this should be doable. Unfortunately I currently just don’t have the time to figure a stable solution out. 😦 You may want to post your question on the raspberry pi forum though. I could imagine that people have tried similar things in the past. Kind regards Markus

      Like

  8. Thank you sir!
    Just amazing how well you answear the comments too. I don’t have any questions, just creds to give.

    Like

  9. Hi there.
    Im trying to get crontab to work, it just seems like it wont for me, basicly it doesnt execute anything at all.

    this is my crontab file sudo crontab -e

    0 16 * * * /home/pi/sunrise.sh
    11 21 * * * /home/pi/sundown.sh
    05 06 * * * /home/pi/sunrise.sh
    05 08 * * * /home/pi/sunrise.sh
    19 20 * * * /usr/bin/sundown.sh

    ive tested different times just to see wether it uses my local time or UTC. what ever i try it doesnt work.

    This is the script in the sunrise.sh file.

    sudo pigpiod
    pigs p 18 1
    sleep 1
    pigs p 18 2
    sleep 1
    pigs p 18 3
    sleep 1
    pigs p 18 4
    sleep 1
    pigs p 18 5

    Like

    • Hey Brian,

      thanks for your comment.

      The reason why it is not working can have various causes.
      -Please try to run the call the script from the terminal and check if its working. Does it work there?
      -In your crontab file you seem to have multiple locations for your scripts (usr/bin/ and /home/) is this correct?
      -You could try to remove the sudo (before pigpiod) as the sudo crontab will automatically run the script with superuser privileges.
      Alex

      Like

    • Hi there thanks for your quick answer.

      The reason i called the script from different locations was because i read that cron might have trouble using my home folder, hence i tryed putting it in another place aswell.

      I figured out why my script didnt work. I might have had serveral mistakes in it.
      But to start off for others, i put in date >>/home/pi/sunrise.log in the beginning of the script to see if it was run.
      It was run at that time but just stopped before my commands was run.

      Then i read that tabs instead of spaces could give me some problems. Since i wrote the script in and excell file to make the 1 – 2 – 3 faster to type in.
      I changed the tabs to spaces.

      When all that was done it still didnt work.

      Then i added

      #!/bin/sh
      PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin

      in the beginning of my script and it started working. I guess the reason is that cron don’t know the general linux commands.

      Liked by 1 person

    • Hey there, thanks for letting us know how you were able to solve your problem.
      The reason why you had to add #!/bin/sh at the beginning is that almost every linux comes with different shells (like Bash and Dash).
      Cheers Alex

      Liked by 1 person

  10. Very useful ! Thanks a million 🙂

    Like

  11. Pingback: Save the day: How to back up Raspberry Pi projects to dropbox | Thoughts, by Oliver Quinlan

  12. If I need to run a script on Monday to Friday, do I put DOW in 12345 or 1,2,3,4,5?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s