如何禁止 crontab -r 以防止誤殺排程工作


今天上 RHCE 時候,在上到 crontab 這一個單元時,老師忽然提到這樣的一個問題:




crontab -r  可以清除掉定時排程的工作,但是 e 和 r 就在旁邊,如果不小心按錯,又沒有備份排程檔的時候,所有的排程工作不就都沒了嗎?試著想個方法去取消 crontab -r 。




 當聽到這樣的一個問題的時候,馬上就想到用 alias 來解決,可是 alias 'crontab -r'='echo "not permit"' ,在對於 alias 不熟的情形下,試了,當然是沒有什麼用,於是就想到用 shell script 來解決,首先先在 .bashrc 中增加下述 alias




alias crontab='~/test.sh'




在自己的 home 目錄下建立一個名為 test.sh 的檔案,並將內容設定如下:



#!/bin/bash
# This is a shell script which is used to cancel the crontab -r

cont=''
for i in $@
do
   if [ $i = '-r' ] ; then
        echo "'crontab -r' is not permitted"
        exit 3
    fi
cont="$cont $i"
done
$(which crontab) $cont


這樣下次當不小心打 e 打成 r 的時候,就不會誤刪排程工作了。

ps: 記得要將 test.sh 變更為可執行 ( chmod 700 test.sh)

留言

這個網誌中的熱門文章

在 Chromebook 上使用嘸蝦米輸入法簡介

修復 Thunderbird 壓實重整造成的信件內容異常

[Ubuntu] 在 IBus 輸入法中,嘸蝦米的安裝心得