2016-05-09 31 views
0

我有下一個問題: 當我從unix shell(CentOS 6.7)運行我的腳本時,我的腳本運行時沒有任何錯誤消息,並且工作正常。Cron向我展示腳本錯誤

當我嘗試在特定時間從cron作業執行它時,會出現問題。

這是腳本:

#! /bin/bash 

#Retrieve the status of the workstations into a file thanks to Nagios 
echo -e "GET hosts\nColumns: address state\nFilter: address != 127.0.0.1" | unixcat /usr/local/nagios/var/rw/live > workstation_state.txt 

#Read that file line by line 
while read line 
do 
    IP=$(echo $line | cut -d ';' -f 1) 
     STATE=$(echo $line | cut -d ';' -f 2) 

     # Check the state of the workstation. If is 0, it means that is up, else is down 
     # Only if the workstation is up we are going to connect through SSH and reload the device 
     if [ $STATE -eq 0 ] 
     then 
       (echo "reload in 1"; echo "y"; echo "exit";) | sshpass -p 'K910p.,lo-16' ssh -A [email protected]$IP 
     fi 
done < workstation_state.txt 

cron的錯誤說:

/root/reload-cisco.sh: line 4: unixcat: command not found 

爲什麼會發生呢?

在此先感謝。

+0

使用'/ full/path/to/unixcat'時仍然會出現同樣的錯誤嗎? – drewyupdrew

+0

不,如果我把unixcat的完整路徑使用cron作業,腳本可以正常工作。 – Jesfer

+0

那麼解決了你的問題?我可以發佈答案,以便這篇文章可以幫助其他人。 – drewyupdrew

回答

1

運行which unixcat查找該命令的絕對路徑,並在腳本中使用該路徑。在cron中使用命令的完整路徑,因爲它可能會擦掉你的$ PATH變量,這大概是你所有的二進制文件和可執行文件所在的位置 - 所以當cron執行腳本時,它不知道在哪裏尋找unixcat命令。