Monday, November 25, 2019

How to create a cron job on a linux server


Cron is a command to the Server, for a job that is to be executed at a specified time. Cron is one of the most useful tool in a server. It can be run for jobs like creating backups, cleaning files, monitoring tasks etc.
The ‘crontab’ command is used to edit/create cron jobs for the server.
Each user has there own crontab file and they can be accessed via command line by :
crontab -e
The Syntax for a crontab entry is
1 2 3 4 5 /full/path/to/the/file/example.php
Here is what each of the codes from 1-5 specifies in the syntax :

* * * * * command/or/file/path

| | | | |

| | | | ----------- Day of the Week (0-7) (0 and 7 = Sunday) 

| | | ------------ Month (1 = 'January', 12 = 'December') 

| | -------------- Date of Month (1 - 31)

| ---------------- Hour (0 - 23)

----------------- Minute (0 - 59)

Other operators which you can use are :

– Asterisk (*) : This is used whenever you wish to include all the values for a field.
e.g if you wish to run a cron job every hour :
0 * 20 * * /command/file/path
This command will run every hour for every 20th date of every month
– Comma (,) : When you wish to specify multiple values for a field
e.g If you wish to run a command every minute in the month of January, march and October :
* * * 1,3,10 * * /command/file/path
– Dash(-) : This Specifies the range of a field.
e.g If you require a command to execute at mid-day of every weekday :
0 12 * * 1-5 * /command/file/path
– Separator (/) : This specifies the step value.
e.g if you require a command to run every 3 hours :
* */3 * * * /command/file/path

Special Strings

You can use special strings in the following syntax :
'Special String' /command/file/path
Here is a list of the special strings :
– @reboot : Run once at startup
– @annually or @yearly : Run once a year.
– @monthly : Once a month
– @weekly : Once a Week.
– @midnight or @daily : Once a day
– @hourly : Run every hour
e.g
@hourly /command/file/path
Load disqus comments

0 comments