How to disable crontab “-r” option in Linux ?

In this article, we will look at a trick how to disable crontab “-r” option in Linux.

“E” and “R” lies nearby, hence there are chances while typing “crontab -e”, we run “crontab -r” and it will remove the crontab file.

To prevent this, we will add a function to /etc/profile file to enable this option system-wide.

Lets see the usage of crontab -l, -r, options.

General Crontab commannd Usage

Here we will see how crontab works.

### Lists crontab file
[root@nglinux ~]# crontab -l
1 1 1 1 1 echo "hello"
[root@nglinux ~]# 

### removes crontab file
[root@nglinux ~]# crontab -r
[root@nglinux ~]#

### Now list again, it will say no crontab for root user.
[root@nglinux ~]# crontab -l
no crontab for root
[root@nglinux ~]# 

Different options of crontab file

	
-e	(edit user's crontab)
	-l	(list user's crontab)
	-r	(delete user's crontab)
	-i	(prompt before deleting user's crontab)
	-s	(selinux context)

 

Creating custom crontab command

[root@nglinux ~]# cat /etc/profile | tail -10
function crontab()
{
if [ $1 == "-r" -o $1 == "-sr" -o $1 == "-rs" ]; then
echo "-r option removes the crontab file."
echo "To remove the file use the option '-ir' to interactively remove crontab file"
else
command crontab $1
fi

}
[root@nglinux ~]# 

Output/Usage of Crontab function

### Usage via root user

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

[root@nglinux ~]# crontab -r
-r option removes the crontab file.
To remove the file use the option '-ir' to interactively remove crontab file


### usage via nglinux user

[root@nglinux ~]# su - nglinux
Mysql password is root/redhat
PHPMyAdmin can be accessed at http://localhost/phpmyadmin
Apache and FTP Server already setup.
Your IP addr:10.0.2.15 addr:172.21.49.107

NGEL System Console can be accessed at URL http://localhost/system
Run NGELINUX_HARDDISK_INSTALL to create partitions and install this live CD on your system hard disk
Run NGELINUX_INSTALL to copy block by block your ISO file to a USB or Complete Hard disk

[nglinux@nglinux ~]$ crontab -l
no crontab for nglinux

[nglinux@nglinux ~]$ crontab -r
-r option removes the crontab file.
To remove the file use the option '-ir' to interactively remove crontab file
[nglinux@nglinux ~]$ 
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments