HomeLinuxHow to run Cron Jobs in linux

How to run Cron Jobs in linux

Cron jobs are an integral part of Linux system administration, allowing users to schedule repetitive tasks and automate routine processes. In addition to cron jobs, the ‘at‘ command provides a complementary solution, enabling users to schedule one-time tasks at specific future times with flexibility and precision. In this article, we’ll explore the fundamentals of cron jobs, providing practical insights and examples to help you master this essential aspect of Linux management.

How ‘cron’ works

Cron is a time-based job scheduler utility in Unix-like operating systems that enables users to automate and schedule recurring tasks. It functions by executing commands or scripts at predefined intervals, that is specified by the user in a crontab file. To manage scheduled jobs, use the crontab command. Now, let’s delve into how cron works:

  1. Cron Daemon: cron is controlled by a daemon process, typically named cron or crond, that runs continuously in the background. This daemon reads the crontab files, checks the scheduled time, and executes the associated tasks when the time matches the schedule.
  2. User crontab Files: Each user can create their own crontab file. These files are stored in /var/spool/cron/crontabs or /var/spool/cron in some Unix-based systems. Users have permissions to edit and manage their individual crontab files.
  3. Job Execution: The cron daemon continuously checks the system time against the schedules that are specified in the crontab files. When the time matches a scheduled job, it executes the associated command or script in a separate shell environment. The output of the command is typically sent to the user’s email address or a system log file.

Crontab commands

We can start, stop, or restart the cron service in our system. The commands for managing the cron service on systemd-based systems are:

[root@ervintest ~]# systemctl start cron
[root@ervintest ~]# systemctl stop cron
[root@ervintest ~]# systemctl restart cron
List the jobs for the current user
[root@ervintest ~]# crontab -l 
Remove all jobs for the current user.
[root@ervintest ~]# crontab -r 
Edit jobs for the current user.
[root@ervintest ~]# crontab -e

Describe Cron Job Format

The fields in the crontab file appear in the following order:

  • Minutes
  • Hours
  • Day of month
  • Month
  • Day of week
  • Command

Additionally, the command or script to execute follows the schedule. An asterisk (*) can be used as a wildcard to indicate “every” in a field.

Examples of Scheduling Cron Job

Example 1: Running a Script Daily

“Suppose you have a script named ‘backup.sh,’ and if the user, let’s say ‘admin,’ wants to run it daily at 3:30 AM:”

Open the crontab file with the default text editor.

[root@ervintest ~]# crontab -u admin -e

Insert the following line:

30 3 * * * /home/support/backup.sh
daily cron job
  • 30 in the first field represents the minute.
  • 3 in the second field represents the hour.
  • * in the third and fourth fields means every day of the month and every month.
  • * in the fifth field means every day of the week.
  • To verify that your job has been added successfully, you can view your crontab with the following command:

To verify that admins job has been added successfully, you can view your crontab with the following command:

[root@ervintest ~]# crontab -u admin -l

Example 2: Weekly Maintenance

The system engineer wants to schedule a weekly maintenance task, and they plan to do it every Sunday at midnight:

To open the crontab file.

[root@ervintest ~]# crontab -u engineer -e

Insert the following line:

0 0 * * 0 /home/maintenance/weekly_maintenance.sh
weekly cron job
  • 0 in the first and second fields represents midnight.
  • * in the third and fourth fields means every day of the month and every month.
  • 0 in the fifth field represents Sunday.

To verify that the engineer has successfully added the job, you can view your crontab with the following command.

[root@ervintest ~]# crontab -u engineer -l

When you can’t find a cron job in your crontab, it’s crucial to understand how to handle scheduled tasks. If the issue is not resolved by managing individual cron jobs in the crontab, restarting the cron service is a common step to take. If the problem persists, restarting the chronyd service is important for ensuring time synchronization.

[root@ervintest ~]# systemctl restart chronyd.service

Accurate timekeeping is essential for various system functions, including log management and proper execution of scheduled tasks. Restarting both the cron service and the time synchronization service can help maintain the reliability and functionality of your system.

Scheduling recurring user jobs is an efficient method for automating routine tasks on Unix-like operating systems. Furthermore, by creating and managing crontab entries, you can not only save time but also guarantee that critical tasks are executed regularly. Additionally, it’s essential to test your jobs and verify their execution to ensure that everything runs smoothly.

Scroll to Top