How to add Crontab and cron job in Linux ?

How to add Crontab and cron job in Linux ?

Cron jobs are the specified tasks which are scheduled to execute commands. Cron is a daemon which executes automatically at a specific time. Cron job is a powerful tool which helps to set tasks like every day, every week, every month, etc. It’s a job scheduler and work very effectively in Unix- like operating system. Through cron jobs you can schedule tasks and run them periodically.

There are two ways to add cron job in cpanel :
  • You can also schedule cron job as a root user through commands.
How to set cron job in Linux or Unix system?

1. To set cron job you have to use command. Let’s see how it works :

Code:
crontab –e
Through this command a file opens whih might be have some jobs in it. Now you can add the cron job in this file but with proper syntax. You have to follow this syntax :

Min Hour Day Month DoW /path/to/command arg1 arg2 … arg

  • Min = you have to use numeric value from 0 to 59
  • Hour = you have to use numeric value from 0 to 23, when you want to execute the command.
  • Day = you have to use numeric value from 1 to 31, when you want to execute the command.
  • Month = you have to use numeric value from 1 to 12, in which 1= January and 12 = December.
  • DoW = you have to use numeric value from 0 to 7, in which 0 or 7=Sunday.
You can also use pattern * to modify your command and can be easily execute the command.

For example :

Code:
10 * * * * /home/user/backup.sh
If you check this file you will find following code in backup.sh file :

Code:
rsync --progress --partial -avz /folder/to/copy/user@remote.server:/remote/folder
If you want to use intervals in to add cron job then you can do this just like :
Code:
50 04 * * * /home/user/public_html/ script.sh
Here this cron job will run 4:50AM in the morning. Here * means everyday or month script.sh will run.

2. To add cron job here is another way through which you can schedule cron job and execute them by adding your script in following folder:
  • /etc/cron.daily/
  • /etc/cron.hourly/
  • /etc/cron.monthly/
  • /etc/cron.weekly/
You can add your scrip in one of these folders like hourly, weekly, monthly or daily and it will execute your task. But it’s necessary to have permission to write on these folders.
Author
bhawanisingh
Views
1,584
First release
Last update
Rating
0.00 star(s) 0 ratings
Top