Cron jobs are one of the common ways developers schedule repeated tasks on a Linux system. A cron job is a simple way to schedule a script to run on your system on a regular basis. Writing a cron job happens in three basic steps:
- Write the script you wish to execute (e.g. mycron.php)
- Determine how often you’d like the script to run
- Write a crontab for it
Writing the script
Start by writing your script or command. It can be in any language you like, but most often crons are written in bash or PHP. Create your cron.txt or cron.sh file if needed. Here’s a basic PHP file that just prints “hello world” in stdout (standard out):
<?php echo "hello world"; ?>
Or a bash script that does the same thing:
!/usr/bin/env bash
echo "hello world";
Or if you want to have some fun, on a Mac you can use the say command to have your computer speak a line instead.
say -v fred "I love Linux"
You can save the say command as a bash file (cron.sh for example) and invoke it with bash.
Save your file as the appropriate file extension, then move on to the next step: determining how often the command should run.
Determining cron schedule
Now that you have your script written, it’s time to make some decisions about how frequently you wish the script to be executed.
Crontab allows you the freedom to run the script or command every minute, hour, day, month, or any combination thereof. If you want the script to be run at 5pm every 3rd Thursday of the month, or only Wednesday through Friday at 10am daily, that’s possible with cron.
When it comes to deciding a cron schedule, less is more. By that I mean, for the health of your server it’s best to run it as infrequently as possible to maximize your server resources. Keep in mind that the more resource intensive your script is, the more possible it is that your crons could be disrupting to the experience of your end users.
The basic format of a cron is:
* * * * * command to run
Each of those asterisks has a particular unit of time it represents, however.
- minute (0-59) of the hour you wish the command to execute. * means every minute of the hour.
- hour (0-23) of the day you wish the command to execute. * means every hour of the day.
- day (1-31) of the month you wish the command to execute. * means every day of the month.
- month (1-12) of the year you wish the command to execute. * means every/any month of the year.
- day of the week (0-6) you wish the command to execute. * means any day of the week.
Using these guidelines you can format your schedule. Below are some examples:
# every minute of every hour/day/month/year
* * * * *
# every hour, on the hour
0 * * * *
# every day at 10am
0 10 * * *
# every Thursday at midnight
0 0 * * 4
# every Monday, Wednesday, and Friday at 3:30pm
30 15 * * 1,3,5
If you want to check your syntax, there’s a handy website called crontab.guru which can help you test out some schedule combinations.
Writing the crontab
The crontab command is how scheduled cron jobs are executed. With crontab we will take the script and the schedule and combine them to actually execute the script or command on the schedule you’d like.
Start by listing any existing cron schedules on your machine with crontab -l
If you’ve never written a cron before, you’ll probably see the following:
$ crontab -l crontab: no crontab for [yourusername]
Now to edit the crontab, we’ll use crontab -e. This will automatically put you in the vim editor for Linux, so here’s a brief tutorial if you’ve never used it before.
Hit i to enter “insert” mode and copy your schedule first. Then you can either type the command directly after. If your cron script is written in PHP and is located in /var/www/html/cron.php you can use the syntax below:
* * * * * /usr/bin/php /var/www/html/cron.php
Or if you want to use the command right in crontab, you can do that too:
* * * * * echo "I love Linux" >> /var/log/cron-stdout.log
Notice in the command above I also added a log file to store the stdout results of the command. This is an option as well and can help you ensure your cron is running as scheduled!
And of course, if you wanted to prank a coworker when they leave their Mac unlocked at work:
* * * * * say -v fred "I am buying nachos for everyone at 5pm"
Once you’ve finished editing, hit the esc key to exit “insert” mode, then type wq! to force write and quit the file. You should see a message, “crontab: installing new crontab” after saving.
… And that’s it! Now your cron will execute on your schedule. Before we part ways, a few words of caution:
- Most managed web hosts don’t allow you to edit system crons. Most times this requires root SSH access on the Linux machine.
- If you’re using WordPress, you can easily get around this restraint by using WordPress cron, and managing it using a simple tool like WP Crontrol.
- Cron is a word that’s easy to mistype. Pro-tip: nobody knows what you want when you’re asking about “corn jobs” 😉
How about you? Any pro-tips for those looking to create a cron job? Leave a comment, or Contact Me.
Leave a Reply