Cyberithub

25 Simple and Easy Crontab command examples to Schedule Cron Jobs in Linux

Advertisements

In this article, I will take you through 25 Best Crontab Command examples in Linux to Schedule Cron Jobs. You might have faced a scenario where you need to schedule a specific Job at certain day and at certain time in your Linux Servers to perform some specific task. This can be easily achieved by scheduling a Cron Job in Linux. Cron Jobs are very popular functionality in use today where you can Schedule complex Jobs very easily. Hence it is very important to understand the different ways through which a Job can be scheduled. I have tried to cover all the methods in detail with examples.

Crontab Format

0   2   *   *   *   /home/centos/example.sh
|   |   |   |   |              |
|   |   |   |   |      Command or Script to Execute        
|   |   |   |   |
|   |   |   |   |
|   |   |   |   |
|   |   |   | Day of the Week(0-6)
|   |   |   |
|   |   | Month of the Year(1-12)
|   |   |
|   | Day of the Month(1-31)  
|   |
| Hour(0-23)  
|
Min(0-59)

SYNOPSIS

crontab [-u user] file
crontab [-u user] [-l | -r | -e] [-i] [-s]
crontab -n [ hostname ]
crontab -c

25 Simple and Easy Crontab command examples to Schedule Cron Jobs in Linux 2

Crontab Command Examples to Schedule Cron Jobs

Also Read: 11 Unix/Linux chmod Command examples to Change File Permissions

Example 1: How to List the Crontab of Logged in User

If you want to list the crontab of current logged in user then you need to use crontab -l command as shown below.

[root@localhost ~]# crontab -l
0 2 * * * /home/centos/example.sh

-l : Displays the current crontab on standard output. More on crontab command Man Page.

NOTE:

Please note that here I am using root user to run all the below commands. You can use any user with sudo access to run all these commands. For more information Please check Step by Step: How to Add User to Sudoers to provide sudo access to the User.

Example 2: How to Check crontab version

If you want to check the installed crontab version then need to use below rpm command. As you can see from below output, current version is 1.4.11.

[root@localhost ~]# rpm -qf $(which crontab)
cronie-1.4.11-23.el7.x86_64

Example 3: How to List Crontab of CentOS user

If you want to List crontab of another user then you need to specify the User with -u option as shown below. In this example, we are checking the crontab of CentOS User using crontab -l -u root command.

[root@localhost ~]# crontab -l -u root
0 2 * * * /home/centos/example.sh

-u : Appends the name of the user whose crontab is to be modified. More on crontab command Man Page.

Example 4: How to Edit the Crontab of Logged In User 

If you want to edit the crontab of logged in User then you need to use crontab -e command as shown below. It will open the crontab with the editor specified by the VISUAL or EDITOR environment variables. In most of the cases, you will see default vi editor. After editing the file, you can save and exit the file by Pressing Esc and then using :wq!

[root@localhost ~]# crontab -e
0 2 * * * /root/example.sh

-e : Edits the current crontab using the editor specified by the VISUAL or EDITOR environment variables. More on crontab command Man Page.

Example 5: How to Edit the Crontab of Another User

If you want to edit the crontab of another user then you need to specify the User with -u option as shown below. In this example, we are editing the crontab of centos User using crontab -e -u centos command.

[root@localhost ~]# crontab -e -u centos
0 2 * * * /home/centos/example.sh

Example 6: How to Run a Cron Job Every Minute

If you want to run a cron job every minute then you can simply use the default value in all the five fields as shown below. This crontab will run the /root/example.sh script every minute.

[root@localhost ~]# crontab -e
* * * * * /root/example.sh

Example 7: How to Run a Cron Job Every 10 Mins

If you want run a Cron Job every 10 mins then you need to set the minute field in crontab to 10 as shown below. This crontab will run /root/example.sh script every 10 mins.

[root@localhost ~]# crontab -e
10 * * * * /root/example.sh

Example 8: How to Run a Cron Job Every Hour

If you want to run a Cron Job every hour then you need to set the minute field(1st field) to 0 as shown below. This crontab will run /root/example.sh script every hour.

[root@localhost ~]# crontab -e
0 * * * * /root/example.sh

Example 9: How to Run a Cron Job on Every Weekday at 1 AM

If you want to run a cron job on every weekday at 1 AM then you need to set 2nd field to value 1(1 AM) and 5th field to 1-5(Mon-Fri) as shown below. Also the 1st field will remain set as 0.

[root@localhost ~]# crontab -e
0 1 * * 1-5 /root/example.sh

Example 10: How to Run a Cron Job on Every Mon and Wed at 1 AM

If you want to run a cron job on every Monday and Wednesday at 1 AM then you need to set the 2nd field to 1 and 5th field to 1,3 as shown below. In the 5th field, value 1 represents Monday and value 3 represents Wednesday.

[root@localhost ~]# crontab -e
0 1 * * 1,3 /root/example.sh

Example 11: How to Run a Cron Job on Friday at Midnight

If you want to run a Cron Job on Friday at Midnight then you need to set the 5th field value to 5. Also the 1st and 2nd field value remain 0 as shown below. Here value 5 in the 5th field represents Friday.

[root@localhost ~]# crontab -e
0 0 * * 5 /root/example.sh

Example 12: How to Run a Cron Job on Every 13th of the Month

If you want to run a cron job on every 13th of the Month then you need to set the 3rd field to value 13(13th day of the month) as shown below. Here 1st and 2nd field will remain set to value 0.

[root@localhost ~]# crontab -e
0 0 13 * * /root/example.sh

Example 13: How to Run a Cron Job at Every 10 mins Using Slash Operator

If you want to run a cron job at every 10 mins using slash operator then you need to set the 1st field to */10 as shown below.

[root@localhost ~]# crontab -e
*/10 * * * * /root/example.sh

Example 14: How to Delete Logged in User Crontab

If you want to delete the Logged in User(root) crontab then you need to use crontab -r command as shown below.

[root@localhost ~]# crontab -r

Example 15: How to Delete Another User Crontab

If you want to delete another user crontab, then you need to specify that User with -u option. In this example, we are deleting the crontab of user centos using crontab -u centos -r command as shown below.

[root@localhost ~]# crontab -u centos -r

Example 16: How to Save or take backup of User's Crontab

If you want to save or take the backup of User's crontab then you need to use below crontab command to take the backup in a file known as crontab.

[root@localhost ~]# crontab -l > crontab

Then you can check the backup using ls -lrt crontab command.

[root@localhost ~]# ls -lrt crontab
-rw-r--r-- 1 root root 103 Sep 26 19:17 crontab

You can also verify the contents of backup using cat crontab command.

[root@localhost ~]# cat crontab
0 2 * * * /home/centos/example.sh
#backup.sh
*/10 * * * * /usr/lib/sysstat/sa1 1 1 > /dev/null 2>&1

Example 17: How to Restore a User crontab from Backup

If you check the current crontab using crontab -l command then you can see there is no crontab set for root user through which we logged in.

[root@localhost ~]# crontab -l
no crontab for root

Now you can try to restore the crontab from the backup taken in the previous example using crontab crontab command as shown below.

[root@localhost ~]# crontab crontab

After restoring crontab from backup you can check the status using crontab -l command as shown below.

[root@localhost ~]# crontab -l
0 2 * * * /home/centos/example.sh
#backup.sh
*/10 * * * * /usr/lib/sysstat/sa1 1 1 > /dev/null 2>&1

Example 18: How to Run a Cron Job Every Day at 8 AM and 10 AM

If you want to run a cron job every day at 8AM and 10 AM then you need to set the hour section in crontab as shown below. Here the 1st field will remain set to value 0.

[root@localhost ~]# crontab -e
0 8,10 * * * /root/example.sh

Example 19: How to Schedule a Cron Job at the Beginning of Every Month Using @monthly

If you want to run a cron job at the starting of every month at 00:00 hrs then you need to use @monthly in the crontab as shown below. This type of job can be scheduled to take monthly backup.

[root@localhost ~]# crontab -e
@monthly /root/example.sh

Example 20: How to Schedule a Cron Job at the Beginning of Every Year Using @yearly

If you want to run a cron job at the starting of every year at 00:00 hrs then you need to use @yearly in the crontab as shown below. This type of Job can be scheduled to run yearly maintenance every year.

[root@localhost ~]# crontab -e
@yearly /root/example.sh

Example 21: How to Execute a Linux Command After Every Reboot

If you want to execute a command from crontab after every reboot then you need to mention the command with @reboot as shown below.

[root@localhost ~]# crontab -e
@reboot uptime

Example 22: How to Schedule a Cron Job Every day using @daily

If you want to run a cron job at the starting of every day at 00:00 hrs then you need to use @daily in the crontab as shown below.

[root@localhost ~]# crontab -e
@daily /root/example.sh

Example 23: How to Schedule a Cron Job Every Second

You cannot schedule a Job Every second as the lowest unit is minute which allows to schedule a Job for every minute. Then it comes hours, days of the month, month of the year and days of the week. Hence a cron job cannot be scheduled every second.

Example 24: How to Prompt before deleting User's Crontab

If you want to the System to prompt for a confirmation before removing the crontab of a User then you need to use -i option with -r as shown below.

[root@localhost ~]# crontab -i -r
crontab: really delete root's crontab?

Example 25: How to Check Man Page of crontab command

If you want to check the man page of crontab command then you need to use man crontab command as shown below.

[root@localhost ~]# man crontab
CRONTAB(1) User Commands CRONTAB(1)

NAME
crontab - maintains crontab files for individual users

SYNOPSIS
crontab [-u user] file
crontab [-u user] [-l | -r | -e] [-i] [-s]
crontab -n [ hostname ]
crontab -c

DESCRIPTION
Crontab is the program used to install a crontab table file, remove or list the existing tables used to serve the cron(8) daemon. Each user can have their
own crontab, and though these are files in /var/spool/, they are not intended to be edited directly. For SELinux in MLS mode, you can define more crontabs
for each range. For more information, see selinux(8).

In this version of Cron it is possible to use a network-mounted shared /var/spool/cron across a cluster of hosts and specify that only one of the hosts
should run the crontab jobs in the particular directory at any one time. You may also use crontab(1) from any of these hosts to edit the same shared set of
crontab files, and to set and query which host should run the crontab jobs.


 

 

 

 

Popular Recommendations:-

Solved: nrpe.service: main process exited, code=exited, status=2/INVALIDARGUMENT

C# data types with Best Examples (.NET v4.7)

How to Transfer Files to an AWS EC2 Instance Using WinSCP in 3 Easy Steps

Learn HTML Image Maps(v5) with Best Examples

Learn HTML Tables(v5) with Best Examples

How to Install PHP on RedHat/CentOS 7 with Easy Steps

How to Install Ruby on Ubuntu 18.04 with Easy Steps

Easy Steps to Install GCC(C and C++ Compiler) on CentOS 7

1 thought on “25 Simple and Easy Crontab command examples to Schedule Cron Jobs in Linux”

Leave a Comment