You can use a cron job for any program which runs on Linux by using the following method.
Tl;dr
- Create a
script.sh
file perform any action. - Make use of
crontab -e
command 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.
Contents
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 (/etc/crontab/
, /etc/cron.*/
and 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.
Commands
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.
cool! right?
let's view our cron table entries before creating a new one.
crontab -l
It will list if it has any. Anyway, let's move on to the step to create a cron job.
crontab -e
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.
Cron syntax
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
Field | Description | Allowed Value |
---|---|---|
MIN | Minute field | 0 to 59 |
HOUR | Hour field | 0 to 23 |
DOM | Day of Month | 1-31 |
MON | Month field | 1-12 |
DOW | Day Of Week | 0-6 |
CMD | Command | 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
Meaning, Run /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
That's it!
Save it all to run the cron job for the specified time period.
Troubleshoot
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"
Conclusion
Let's go through the steps we've done.
- Created a
script.sh
file to perform the action. - Add an entry to the cron table using
crontab -e
command. - The crontab entry should point to the script.sh file.
- We troubleshoot the job by looking at the logs
Top comments (0)