Linux 禁用crontab -r


crontab -r 会清空所有定时任务,比较危险;

我们接下来把它禁用掉;

1、写一个脚本

vim /usr/bin/cron.sh      #内容如下

#!/bin/bash
source /etc/bashrc
source /etc/profile
source ~/.bashrc


if [ "$1" = "-r" ] ; then
  echo -e "\033[31m严重错误:禁止执行crontab -r \033[0m"
  echo "Exit..."
  exit 2
fi

if [ "$1" = "-l" ] ; then
  /usr/bin/crontab -l
  exit 0
fi

if [ "$1" = "-e" ] ; then
  /usr/bin/crontab -e
fi

有的还会加入备份的内容,我这里没有加;

添加脚本的执行权限:

chmod +x /usr/bin/cron.sh

2、在/etc/bashrc文件末尾添加 

alias crontab='/usr/bin/cron.sh' 

3、测试 

 已生效,已经不能执行crontab -r 了;