Home » Linux Basics » 6 Linux Crontab Command Examples

6 Linux Crontab Command Examples

Crontab command manages the cron table that is used by the cron daemon to execute the cron jobs. This article explains the various command line options of the crontab command.

 

1. Tweaking Other Users Crontab using Option -u

-u stands for user. This should be followed by a valid username in the system. -u option alone doesn’t do anything. It should be combined with other options. Actually, it can be combined with any other crontab command line options.

If you don’t specify -u username, crontab commands wil be executed on the current user. For example, all of the following crontab commands will be executed on the current logged in user.

crontab -l  crontab -e  crontab -r  ..

If you specify -u username, the crontab command will be executed on the given username. For example, all of the following crontab commands will be execute on the jsmith user.

crontab -u jsmith -l  crontab -u jsmith -e  crontab -u jsmith -r  ..

2. Display Cron Table using Option -l

-l stands for list. This displays the crontab of the current user. Since I’m logged in as root, this will display the cron jobs of root user.

# crontab -l  53 00 * * 7 /bin/sh /home/root/bin/server-backup

To display the cron jobs of other users, combine -l with -u option.

# crontab -u jsmith -l  01 00 * * * /bin/sh /home/jsmith/bin/rman-backup

The 15 crontab examples explains practical ways of using the cron job entries.

3. Edit Cron Table using Option -e

-e stands for edit. This allows you to edit the crontab of the current user. Since I’m logged in as root, this will automatically open root’s cron jobs in a Vim editor, and allow me to edit it.

# crontab -e  53 00 * * 7 /bin/sh /home/root/bin/server-backup  ~  ~  /tmp/crontab.7dgqju

As you notice from the above, /tmp/crontab.7dgqju is a temporary file created by the crontab automatically where you can edit your cron jobs.

When you save your edits and come out of the Vim editor, it will display oone of the following messages, depending on whether you made any changes or not.

# crontab -e  crontab: no changes made to crontab    # crontab -e  crontab: installing new crontab

Note: The editor that crontab uses to open the cron jobs for editing depends on the VISUAL or EDITOR environment variable. By default, it will use Vim editor on Linux environment. But you can change it using the VISUAL/EDITOR environment variable.

To edit the cron jobs of other users, combine -e with -u option.

# crontab -u jsmith -e  crontab: installing new crontab

To understand the meaning of the crontab entries itself, refer to How to Run a Cron Job Every 5 Minutes (or Hours, or Days, or Months).

4. Load Crontab from a File

Instead of manually editing the crontab to add new jobs, you can also upload all the cron jobs from a file. This is helpful when you have to maintain lot of servers that has the same cron job entries.

In the following example, all the cron jobs are in the /home/root/mycronjobs.txt file.

# cat /home/root/mycronjobs.txt  53 00 * * 7 /bin/sh /home/root/bin/server-backup  01 00 * * * /bin/sh /home/root/bin/check-user-quota

To upload the mycronjobs.txt jobs to current user crontab, do the following:

# crontab /home/root/mycronjobs.txt

Validate to make sure the cron jobs are successfully uploaded.

# crontab -l  53 00 * * 7 /bin/sh /home/root/bin/server-backup  01 00 * * * /bin/sh /home/root/bin/check-user-quota

Note: Be careful while using this upload method, as this will wipe-out all the current cron job entries before uploading the new ones.

To upload the cron job from a file to another user, combine it with -u option.

# crontab -u jsmith /home/jsmith/mycronjobs.txt

5. Add SELinux Security using Option -s

-s stands for SELinux. This will add the MLS_LEVEL variable to the crontab that contains the current SELinux security context.

To use -s option, you should upload the cron jobs from a file.

# cat /home/root/mycronjobs.txt  53 00 * * 7 /bin/sh /home/root/bin/server-backup  01 00 * * * /bin/sh /home/root/bin/check-user-quota    # crontab -s /home/root/mycronjobs/my.txt  SELINUX_ROLE_TYPE=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023  53 00 * * 7 /bin/sh /home/root/bin/server-backup  01 00 * * * /bin/sh /home/root/bin/check-user-quota

Depending on your system the above will add either SELUNUX_ROLE_TYPE variable or MLS_LEVEL variable that contains the SELinux security context string. If you are not using SELinux in your environment, don’t worry about what this option does. SELinux is a separate topic of discussion, that we might cover in detail in future articles.

6. Delete All Cron Jobs using Option -r

-r stands for remove. This will remove all the cron job entries of the current user as shown below.

# crontab -l  53 00 * * 7 /bin/sh /home/root/bin/server-backup  01 00 * * * /bin/sh /home/root/bin/check-user-quota    # crontab -r    # crontab -l  no crontab for root

-i stands for interactive mode. Combining -i with -r will ask you a confirmation before removing all the crontab entries.

# crontab -ir  crontab: really delete root's crontab? n

To remove the cron jobs of other users, combine -r with -u option.

# crontab -u jsmith -l  01 00 * * * /bin/sh /home/jsmith/bin/rman-backup    # crontab -u jsmith -r    # crontab -u jsmith -l  no crontab for jsmith

Originally posted 2016-01-11 05:53:13.

About admin

I have been working with Linux based systems since 2002. I hope the article published on this site will be useful to those new to server administration.