The crontab command and switches with example

Linux crontab and job scheduling

In Linux, we can not ignore the usages of cron. It’s command line utility and it helps to schedule tasks. Those tasks will execute periodically pas per the configuration. In this article I am just mentioning the basic operation of crontab command.

Crontab is the program used to install, remove or list the tables used to drive the cron daemon. Cronjobs are predefined jobs which are running periodically according to the settings we have done previously.

Syntax

# crontab [-u user] [-l | -r | -e] [-i] [-s]

Important switches with example 

1, -u

Append the name of the user whose crontab is to be tweaked. If this option is not given, crontab examines “your” crontab, i.e., the crontab of the person executing the command. Note that su(8) can confuse crontab and that if you are running inside of su(8) you should always use the -u option for safety’s sake. The first form of this command is used to install a new crontab from some named file or standard input if the pseudo-filename “-” is given.

2, -l

Lists all jobs.

Example

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

For a user

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

3, -e

This option is for editing the current crontab using the editor specified by the VISUAL or EDITOR environment variables. After you exit from the editor, the modified crontab will be installed automatically.

Example:

[root@localhost ~]# crontab -e

For a user:

[root@localhost ~]# crontab -u crybit -e

4, -r

To remove current crontab. Do it carefuly, it will remove all cronjobs under the current user.

Example:

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

For a user:

[root@localhost ~]# crontab -u crybit -r
no crontab for crybit

5, -i

This option modifies the -r option to prompt the user for a ’y/Y’ response before actually removing the crontab.

Example:

[root@localhost ~]# crontab -ir
crontab: really delete root's crontab? y
no crontab for root

For a user:

[root@localhost ~]# crontab -u crybit -ir
crontab: really delete crybit's crontab? y
no crontab for crybit

6, -s

It will append the current SELinux security context string as an MLS_LEVEL setting to the crontab file before editing / replacement occurs – see the documentation of MLS_LEVEL in crontab(5).

Example:

[root@localhost ~]# crontab -u crybit -es
no crontab for crybit - using an empty one
crontab: installing new crontab
[root@localhost ~]# crontab -u crybit -l
MLS_LEVEL=s0-s0:c0.c1023

That’s it..!! 🙂

Related links:
list cronjobs running under a user
Examples of Job Scheduling Using Crontab

More:
ls, head, tail, top, ps, find, crontab, groupdel, groupmems, groupmod, useradd , usermod , chgrp, chown

How to check the execution time of your script in cron?

Post navigation

Arunlal A

Senior System Developer at Zeta. Linux lover. Traveller. Let's connect! Whether you're a seasoned DevOps pro or just starting your journey, I'm always eager to engage with like-minded individuals. Follow my blog for regular updates, connect on social media, and let's embark on this DevOps adventure together! Happy coding and deploying!

5 thoughts on “The crontab command and switches with example

Leave a Reply

Your email address will not be published. Required fields are marked *