Managing the crontab entry

We don't add or modify an entry of a crontab directly. It is done by using the crontab command that allows you to add, modify, and list crontab entries. Each user can have their own crontab where they can add, delete, or modify tasks. By default, it is enabled for all users, but if a system administrator wants to restrict some of the users, he or she can add that user in the /etc/cron.deny file.

The syntax of using the crontab command is as follows:

crontab [-u user] file
crontab [-u user] [option]

The options of the crontab are explained in the following table:

Option

Description

-u user

Appends the name of the user whose crontab is to be modified

-l

Displays the current crontab on stdout

-e

Edit the current crontab using an editor specified by the EDITOR env

-r

Remove the current crontab

-i

Interactive removal of the current crontab when used with the -r option

Listing crontab entries

To list the crontab entries, we use the -l option for the current user:

$ crontab -l
no crontab for foo

The output says that there is no crontab entry for the user foo. It means the user foo has not added any task in his or her crontab yet.

To view crontab as the root user, type the following command:

# crontab -l
no crontab for root

Alternatively, use the following command:

$ sudo crontab -l

Editing crontab entries

Crontab of the current user can be edited or modified by using the -e option with crontab:

$ crontab -e

After executing the preceding command, an editor will open where the user can add tasks into the crontab file. In our case, the vi editor is launched. The following entries have been added into the user foo crontab entry:

Editing crontab entries

After saving and exiting from the editor, the output obtained is as follows:

no crontab for foo - using an empty one
crontab: installing new crontab

To view the modified crontab entry of the user foo, run the –l option again:

$ crontab -l
Editing crontab entries

To create the crontab entry of the user root, we can run crontab with the -e option as the root:

# crontab -e

OR

$ sudo crontab -e

After running the preceding command, the editor opens to modify crontab for the user root that looks as follows after adding entries:

Editing crontab entries

To view the crontab entry of the root, we can use crontab -l as the root user:

# crontab -l
Editing crontab entries

The root user can also view and modify the crontab entry of another user. This is done by specifying the -u option followed by the username:

# crontab -u foo -e  # Modifying crontab of user foo as root

Crontab of the user foo will be opened for modification as follows:

Editing crontab entries

To view the crontab entry of another user, run the -l option with –u as follows:

# crontab -u foo -l

We can display the crontab of the user foo as follows:

Editing crontab entries

Crontab entries are created using the crontab command and are stored in the /var/spool/cron/ directory. A file is created by the name of the user:

# ls /var/spool/cron
root  foo

We can see that a file is created for the users root and foo.

Removing crontab entries

We can also remove crontab using the -r option with the crontab command. By default, crontab of the current user is deleted. Using the option with -i allows the interactive removal of crontab:

# crontab -i -r
crontab: really delete root's crontab? Y

By running the preceding command, the crontab entry of the user root has been deleted. We can verify this by running the -l option:

# crontab -l
no crontab for root

#  ls /var/spool/cron
foo

The user root can also delete crontab of other users by specifying the user in the–u option:

# crontab -r -i -u foo
crontab: really delete foo's crontab? n

We specified 'n' (no) instead of 'y' (yes), so the removal of the user foo crontab will be aborted.

Let's delete this now:

# crontab -r -i -u foo
crontab: really delete foo's crontab? Y

Now, the crontab entry of the user foo has been removed. To verify, run the following command:

$  crontab -l
no crontab for foo
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.19.207