6 Linux Crontab Command Examples

by Ramesh Natarajan on December 14, 2011

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 oracle user.

crontab -u oracle -l
crontab -u oracle -e
crontab -u oracle -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 oracle -l
01 00 * * * /bin/sh /home/oracle/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

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 oracle -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 oracle /home/oracle/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
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 oracle -l
01 00 * * * /bin/sh /home/oracle/bin/rman-backup

# crontab -u oracle -r

# crontab -u oracle -l
no crontab for oracle

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 8 comments… read them below or add one }

1 SN December 14, 2011 at 3:49 am

crontab -l is also a viable option to access the crontab entries of a different user.

2 asingh December 14, 2011 at 4:29 pm

What about all the cron jobs in cron.xxxx folders. How do they work?

3 farooq ahmed May 24, 2012 at 10:12 am

Hi Ramesh,
I work as a Platform Testing Engineer in a company. I was assigned some linux servers to work on and had to schedule CRON Jobs on it for backups. I really struggled understanding CRON untill i came across your website. It was of immense help and ur articles about Linux are very helpful and user friendly (I have seen that many of the linux stuff on the Net is useless, unless you know the basics).

Thanks again.

Farooq Ahmed

4 koperundevi August 22, 2012 at 6:45 am

Extremely good one.

5 MohamedH October 11, 2012 at 6:25 am

Hi All,

Thanks for the article. However, I created my own crontab task but did not run.

Should I do further steps after editing and saving?


6 Kanji N. Madhar March 1, 2013 at 11:24 pm

i like so much this information

7 Andrey November 15, 2013 at 5:53 am

Would be great to see backup of crontab list.

8 Anonymous December 24, 2013 at 6:15 pm

This is really amazing article…This helps many people who does not even fundamentals of linux…thank you once again man for your clear cut article.

Leave a Comment

Previous post:

Next post: