Open up a terminal window and type the following command to create a cron job
Skip the following if already have a editor selected:
You should be prompted to choose an editor. Press enter to select nano as the default editor.
Now if you skiped the above then start again here:
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 PHP script every day at 6 am:
0 6 * * * /home/pi/testscript.php
This most likely makes no sense right now. Lets see how a cron command is designed.
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.