You can use a cron job for any program which runs on Linux by using the following method.
- Create a
script.shfile perform any action.
- Make use of
crontab -ecommand to create the cron job.
- Set the job to run the script for the specified timings.
- We troubleshoot the job by looking at the logs.
Cron is a useful tool in Linux that most developers love because it lets you run automated commands at specific periods (minutes, hours, days, etc.).
Cron jobs run in the background and its keep checking few files and directories (
var/spool/cron ..etc). Cron files are not supported to be edit directly, and each user has a unique crontab. The
crontab stands for the cron table. It is the list of commands that you want to run on a regular schedule.
The commands for creating and editing cron jobs are the same and simple. And what's even cooler is that you don't need to restart the cron after making changes to the existing one.
let's view our cron table entries before creating a new one.
It will list if it has any. Anyway, let's move on to the step to create a cron job.
Nothing special, single parameter change to command!.
The command may prompt you to select a text editor(nano, vim), go on with the comfortable one.
Now, we have a crontab file opened on a text editor to write our entries.
Just as it is with any language, working with cron is a lot easier when you understand its syntax.
MIN HOUR DOM MON DOW CMD
|0 to 59
|0 to 23
|Day of Month
|Day Of Week
|Any command to be executed.
That's not all. Cron uses 3 operator symbols which allow you to specify multiple values in a single field.
- Asterisk (*) - specifies all possible values for a field.
- The comma (,) - specifies a list of values.
- Dash (-) - specifies a range of values.
- Separator (/) - specifies a step value.
By looking at the syntax we can write our entries.
0 3 * * * /home/user/path/to/script.sh
/home/user/path/to/script.sh at 3 am every day.
You can adjust periods by changing the time parameters.
*/30 * * * * /home/user/path/to/script.sh
The above one will run the script every 30 minutes.
30 * * * * /home/user/path/to/script.sh
This would run at 1:30,2:30,3:30.
0,30 * * * * /home/user/path/to/script.sh
This would run at 1:30,2:00,2:30,3:00,3:30.
Write this entry on the text editor we're previously opened using
crontab -e command. Before saving it we need to set up our script for this.
Setting up the script
Cron doesn't support
relative path, therefore you've to write the
absolute path for everything that you are pointing.
You can find the absolute paths of directories by simply typing
pwd on your terminal.
Make sure your script has execute permission.
chmod +x script.sh
Save it all to run the cron job for the specified time period.
Cron jobs are commands that your service runs at a specified interval and, as such, can be difficult to troubleshoot.
Although we can't directly troubleshoot, some of the common mistakes are:
- Using a relative path. You must be sure to use only absolute paths inside that script.
- Permissions are too strict. Please be sure all scripts, files that are being used are set to executable.
chmod +x <file name>
You can check the cron logs to make sure that the crontab is working correctly. The logs are default located in
/var/log/syslog. And running the following grep command will get you all cron logs.
grep -i "CRON" /var/log/syslog
If you're not finding the syslog file in your system, Then try the below command.
journalctl | grep "CRON"
Let's go through the steps we've done.
- Created a
script.shfile to perform the action.
- Add an entry to the cron table using
- The crontab entry should point to the script.sh file.
- We troubleshoot the job by looking at the logs