How to schedule Cron Jobs on the Raspberry Pi

About: I have recently been working on a MMORPG turn based text game and needed a way to run a php script every 2 hours to generate a turn. Since I use the Raspberry Pi as a LAMP server for testing out my game before releasing official updates I figured it would work well as a cron job to call a website page every 2 hours.

You can schedule many types of cron jobs using your Raspberry Pi. Cron is basically the Linux version of the Windows Task Scheduler. You can schedule commands to run at certain times of the day or only run once a month. The shortest time a cron job can run is 1 minute in between commands. If you need a command to repeat every 30 seconds then a cron job will not work.

Objective: To create and schedule a cron job using the terminal on the Raspberry Pi

Material: You will need the following:

  • Raspberry Pi (Click the link to check out the price on Amazon. Usually around $36 with free shipping)

Instructions: Open up a terminal window and type the following command to create a cron job

You should be prompted to choose an editor. Press enter to select nano as the default editor.

You will now be looking at a empty file if this is the first time trying to schedule a cron job. If you already have cron jobs scheduled then you will see a list of cron jobs scheduled. Each line should contain a cron job.

Note: You are only viewing the cron jobs that are running for your current user. Most likely that is ‘pi’. Check out the commands below to see how you can view the cron jobs for other users as well as schedule cron jobs for other users.

Lets add a new scheduled task. The layout for a cron entry is made up of six components: minute, hour, day of month, month of year, day of week, and the command to be executed.

The following command would run a script every day at 6 am:

This most likely makes no sense right now. Lets see how a cron command is designed.

Basic structure of the Cron Command

The first asterisk represents the minutes, followed by the hours, day of the month, the month, and day of the week. You can leave a point with an asterisk and it will execute the command on all the values. For example if you would like something to run everyday then you would leave the day of the month, month, day of the week with asterisk and only edit the values for minute and hours.

It may seem very annoying to write out the cron command if the time schedule is very complicated or long. I recommend using a cron command generator which allows you to select the time, days of week, and months to run your cron job.

Once you put a new cron job in the crontab go ahead and save it using CTRL+X and then Y to save your new cron jobs.

View Scheduled Task

To view your currently scheduled cron job tasks type the following command:

You may need to run this command as sudo or it will not return anything. This command should list a line for every cronjob that is currently running.

That’s the basics of setting up a cronjob on the Raspberry Pi using the terminal. As usual be sure to comment below if you have any questions.

Leave a Reply