我有一個簡單的 shell 腳本,我需要在其中檢查我的 EMR 作業是否正在運行,我只是在列印日志,但是在使用 cron 調度腳本時它似乎無法正常作業,因為它總是列印 if 塊陳述句,因為“status_live” var 的值始終為空,因此如果有人可以建議此處有什么問題,否則在手動運行腳本時它可以正常作業。
#!/bin/sh
status_live=$(yarn application -list | grep -i "Streaming App")
if [ -z $status_live ]
then
echo "Running spark streaming job again at: "$(date) &
else
echo "Spark Streaming job is running, at: "$(date)
fi
uj5u.com熱心網友回復:
您的腳本無法運行,cron因為 cron 腳本根本沒有環境背景關系。
例如,嘗試將您的腳本作為另一個nobody沒有外殼的用途來運行。
sudo -u nobody <script-full-path>
它會失敗,因為它沒有環境背景關系。
解決方案是將您的用戶環境背景關系添加到您的腳本中。只需添加source到您的.bash_profile
sed -i "2a source $HOME/.bash_profile" <script-full-path>
您的腳本應如下所示:
#!/bin/sh
source /home/<your user name>/.bash_profile
status_live=$(yarn application -list | grep -i "Streaming App")
if [ -z $status_live ]
then
echo "Running spark streaming job again at: "$(date) &
else
echo "Spark Streaming job is running, at: "$(date)
fi
現在嘗試使用 user 再次運行它nobody,如果它有效,那么它cron也會有效。
sudo -u nobody <script-full-path>
請注意,cron它沒有標準輸出。并且您需要將標準輸出從腳本重定向到日志檔案。
<script-full-path> >> <logfile-full-path>
uj5u.com熱心網友回復:
# $? will have the last command status in bash shell scripting
# your complete command here below and status_live is 0 if it finds in grep (i.e. true in shell if condition.)
yarn application -list | grep -i "Streaming App"
status_live=$?
echo status_live: ${status_live}
if [ "$status_live" -eq 0 ]; then
echo "success
else
echo "fail"
fi
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/398245.html
