How to Set Cron Job on Linux Server – Cron Job Working & Scheduling

Set Cron Job

IT operations or DevOps team usually works on roasters to give users 100% uptime services. They need to do a lot of tasks that need to be done continuously, on certain time intervals and keep the data up-to-date. Manually performing these tasks at regular time intervals or for the specified period is not always possible. These tasks can be automated using jobs or automated processes.

Task Automation

What does it take to automate tasks? Set up a cron job. Cron is a set of commands that are given/written in a crontab file to be executed as per schedule. There are tasks such as sending up the reminders to a set of users daily at 8am, taking data backup at night 2:00am everyday or setting alerts to the user if the server is running out of space at a specified threshold or performing system scans every Sunday of the week.

Writing the cron job makes all the above tasks possible via scheduling. The cron job can be written on the server on which your whole IT environment is set up. The Linux service is the most used server platform where all the services are hosted and can be administered.

Cron Job

Difference between Cron Job and Batch Job

Both Cron job and Batch jobs are Linux commands that help in setting up auto run jobs on hosting. Cron Job executes one or more tasks at a specified time one or more times. Batch Job is executing multiple tasks at a go. 

You can also schedule a batch job that can be executed simultaneously or sequentially. As it comprises multiple tasks, you can define a sequence of the tasks to be processed one after another or all the specified tasks to be processed simultaneously. You can set up a cron job for batch processes. 

Difference between Cron Job and At Job

Cron Job can be scheduled with multiple time parameters and executed based on the schedule. The cron job can be edited, deleted and re-scheduled as per requirements. 

At Job is scheduled to execute a specific task only once. Once created, it cannot be edited once scheduled. It can be deleted the same as Cron Job.

Crontab file

Crontab is the file in which cron job commands are written. The command line is made up of 2 parts –

Cron Job file
  • Part 1 – specify the time parameter(s) for scheduling the job. Time parameters are:
    • minute  – the value is between 0-59. 
      • If you put an asterisk sign, it will be considered as “every minute”. The job will be executed every minute.
      • If you put 10, it will be considered as “every 10 minutes”.
    • hour – the value range is 0-23. 
      • If you put the value as 18, it will be considered as 6pm.
      • If you put the value as 0, it will be considered as midnight.
    • day of the month – the value range is 1-12.
      • If you put the value as 1, it will be considered as the month of January.
      • If you put the value as 3, it will be considered as the month of March.
    • day of the week – the value range is 0-7. 
      • If you put the value as 3, Wednesday is considered
      • If you put the value as 7, Sunday is considered. 0 is also considered as Sunday.
  • Part 2 – specify the command and the directory of the file or folder on which you are scheduling the job
    • rm /home/tinas/tmp/* – removes the temporary files from the “tmp” folder
      • rm is the “remove” command
      • “tmp” is referred to as temporary folder  
    • /home/tinas/scriptname.sh – run the script available in this folder path specified

Now, let us see some examples of the entire command in the crontab file.

CommandDescription
0 23 * * 3 * /bin/shRun a backup job every Wednesday night at 11pm. 
0 7 3 */3 * /home/tinas/script.shRun the job quarterly on the 3rd day at 7am. Slash (/) is used to specify intervals.
0 11,19 * * * /bin/sh backup.shRun the backup job 2 times a day at 11am and 7pm. Comma (,) is used to execute the job at intervals specified. 
0 */3 * * * /scripts/script-monitor.shRun the job every 3 hours. 
* * * * * /bin/shRun the job every minute
* * * 10,11,12 *  /script/script.shRun the job in October, November, December months.
0 * 1 7 * /bin/sh script.shRun the job every 1st of July.

Setup the Cron job on Linux Server – Step by Step Guide

  1. Open the terminal on the Linux server where all the services are configured.
  2. Type sudo su.
  3. Enter the password for the admin user. You will be able to enter crontab commands.
  4. To edit another user’s crontab file, use the below command.
Cron Job
  1. – u is used for username
  2. – e is used for edit
  3. – l is used to list the crontab files
  4. e is used to create and edit the file
  5. -a <filename> is used to install the <filename> as a crontab file.
  6. To remove the crontab file from the user. Use the below command.
Cron tab -r
  1. To view contents of the crontab file, use -l after the crontab command.
crontab -l
  1. To view contents of the crontab file, use -l after the crontab command.
  2. Enter the command to specify the job schedule. (Refer to the Crontab file section for commands.)
  3. Save the file. The job will be executed at specified time and intervals.

File Permissions

You can set permissions for the users in the DevOps team who can use the cron jobs. There are two types of permissions given – Allow and Deny.

  1. cron.allow – This file contains the list of users who are allowed to use the crontab files created on the server.
  2. cron.deny – This file contains the list of users who are restricted from using the crontab files created on the server.

Conclusion

You as Linux administrator can use the cron jobs for so many events and tasks such as setting up monitoring alerts for broken links, send out email notifications to the users on specific events, deactivate user accounts and so on. 

There is another set of tasks on the administration side that can be executed using the cron jobs. System maintenance automation tasks such as database backup, disk space monitoring, alerts when the service goes down, network is down or any one of the servers is down. 

Cron jobs help manage the server administrator’s tasks efficiently and help the team to take the appropriate actions on alerts that are being sent by the cron jobs. Automating tasks with the cron jobs prevents the customers or end users from multiple failures. The only thing to take care of setting up the cron job is to use the right command with the correct time specified. 

Also Read: