寫了一個shell腳本,手動執行有效,在crontab中執行卻沒有效果
最后發現貌似是環境變量的問題。
腳本如下:
#!/bin/sh
#this is a script to control the message of mail
##########################
. /etc/profile
. ~/.bash_profile
##########################
sum=`/usr/sbin/postqueue -p |wc -l`
if [[ $sum -gt 100 ]]
then
/usr/sbin/postqueue -p | awk '/cs_0105@qingfeng.com/ {print $1}' | tr -d '*' | xargs -n 1 postsuper -d
fi
exit 0
在crontab中寫:00 07,09 * * * source ~/.bashrc && cd /home/zhuo && ./mailctrl.sh &> /dev/null
加入紅色字體后生效,具體可參考:http://www.justwinit.cn/post/3377/
——————————————————————————————————————
關 於登錄linux時,/etc/profile、~/.bash_profile等幾個文件的執行過程。 在登錄Linux時要執行文件的過程如下: 在剛登錄Linux時,首先啟動 /etc/profile 文件,然后再啟動用戶目錄下的 ~/.bash_profile、 ~/.bash_login或 ~/.profile文件中的其中一個,執行的順序為:~/.bash_profile、 ~/.bash_login、 ~/.profile。如果 ~/.bash_profile文件存在的話,一般還會執行 ~
關於登錄linux時,/etc/profile、~/.bash_profile等幾個文件的執行過程。
在登錄Linux時要執行文件的過程如下:
在 剛登錄Linux時,首先啟動 /etc/profile 文件,然后再啟動用戶目錄下的 ~/.bash_profile、 ~/.bash_login或 ~/.profile文件中的其中一個,執行的順序為:~/.bash_profile、 ~/.bash_login、 ~/.profile。如果 ~/.bash_profile文件存在的話,一般還會執行 ~/.bashrc文件。因為在 ~/.bash_profile文件中一般會有下面的代碼:
if [ -f ~/.bashrc ] ; then
. ./bashrc
fi
~/.bashrc中,一般還會有以下代碼:
if [ -f /etc/bashrc ] ; then
. /bashrc
fi
所以,~/.bashrc會調用 /etc/bashrc文件。最后,在退出shell時,還會執行 ~/.bash_logout文件。
執行順序為:/etc/profile -> (~/.bash_profile | ~/.bash_login | ~/.profile) -> ~/.bashrc -> /etc/bashrc -> ~/.bash_logout
關於各個文件的作用域,在網上找到了以下說明:
(1)/etc/profile: 此文件為系統的每個用戶設置環境信息,當用戶第一次登錄時,該文件被執行. 並從/etc/profile.d目錄的配置文件中搜集shell的設置。
(2)/etc/bashrc: 為每一個運行bash shell的用戶執行此文件.當bash shell被打開時,該文件被讀取。
(3)~/.bash_profile: 每個用戶都可使用該文件輸入專用於自己使用的shell信息,當用戶登錄時,該文件僅僅執行一次!默認情況下,他設置一些環境變量,執行用戶的.bashrc文件。
(4)~/.bashrc: 該文件包含專用於你的bash shell的bash信息,當登錄時以及每次打開新的shell時,該該文件被讀取。
(5)~/.bash_logout: 當每次退出系統(退出bash shell)時,執行該文件. 另外,/etc/profile中設定的變量(全局)的可以作用於任何用戶,而~/.bashrc等中設定的變量(局部)只能繼承 /etc/profile中的變量,他們是"父子"關系。
(6)~/.bash_profile 是交互式、login 方式進入 bash 運行的~/.bashrc 是交互式 non-login 方式進入 bash 運行的通常二者設置大致相同,所以通常前者會調用后者。
