≡ Menu

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

# crontab -u oracle -r

# crontab -u oracle -l
no crontab for oracle
Add your comment

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

Comments on this entry are closed.

  • SN December 14, 2011, 3:49 am

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

  • asingh December 14, 2011, 4:29 pm

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

  • farooq ahmed May 24, 2012, 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.

    Regards,
    Farooq Ahmed

  • koperundevi August 22, 2012, 6:45 am

    Extremely good one.

  • MohamedH October 11, 2012, 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?

    Thanks

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

    i like so much this information

  • Andrey November 15, 2013, 5:53 am

    Would be great to see backup of crontab list.

  • Anonymous December 24, 2013, 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.

  • satish March 27, 2015, 1:25 am

    i have removed cronjob but still its running in background.. is there any way i can stop it. i dont have root access of server

  • Anonymous November 28, 2015, 1:07 pm

    i want to run this script for 30 mins after every 15 mins how can i do that ??

    the below one is doing for 5 min after 1 hour. i want 30 min after 15 mins break

    0 * * * * nohup /home/pi/mmal/motion -n -c /home/pi/mmal/motion-mmalcam.conf 1>/dev/null 2>&1 </dev/null &

    5 * * * * pkill -9 motion

  • Przemyslaw Sedzicki February 28, 2016, 6:14 pm

    I pu “sudo crontab -e” into terminal and the answer was like:
    “no crontab for root – using an empty one
    888”
    Now I can write here but I don’t even know how to SAVE/EXIT this edit mode?
    Any help?