簡體   English   中英

BASH Shell腳本在命令提示符下正常運行,但不適用於crontab

[英]BASH shell script works properly at command prompt but doesn't work with crontab

這是我要使用crontab執行的腳本。

#!/bin/bash
# File of the path is /home/ksl7922/Memory_test/run_process.sh
# 'mlp' is the name of the process, and 'ksl7922' is my user account.

prgep mlp > /home/ksl7922/proc.txt

# This line will give the number of the process of 'mlp'
result=`sed -n '$=' /home/ksl7922/proc.txt`
echo "result = ${result}"

# if 'mlp' processes run less than six, than read text file one line and delete 
# it, and execute this line.

if ((result < 6)); then
    filename="/home/ksl7922/Memory_test/task_reserved.txt"
    cat $filename | while read LINE

    do
        # Delete line first.
        sed -i "$LINE/d" $filename

        # Execute this line
        eval $LINE

        break;
    done
else
    echo "You're doing great."
fi

之后,我編輯了crontab並使用crontab -l進行了檢查

*/20 * * * * sh /home/ksl7922/Memory_test/run_process.sh

此腳本可從命令行正常運行,但是不能與crontab正常運行。

似乎shell腳本無論如何都可以與crontab一起使用,因為生成了“ proc.txt”文件,並且刪除了“ task_reserved.txt”的第一行。

但是,我沒有看到任何消息以及“ mlp”進程的結果文件。

由於我的英語不好,所以恐怕你們不明白我的意圖。 無論如何,有人可以讓我知道如何處理嗎?

  1. 我敢打賭, cron內沒有正確設置PATH環境變量。 插入

     echo $PATH > /tmp/cron-path.txt 

    看看它目前有什么價值。 也許您需要在腳本中手動將其設置為適當的值。

    這實際上是常見問題解答

  2. 如果您的系統上沒有安裝任何mail ,以便cron從腳本中轉發錯誤消息,那么最好將所有錯誤消息手動重定向到您的首選位置。 例如。

     #! /bin/bash { date prgep mlp > /home/ksl7922/proc.txt ... snip ... fi } &> /tmp/cron-msg.txt 

您是否檢查了腳本的執行權限? 該文件應具有可執行權限。

ls -ltr /home/ksl7922/Memory_test/run_process.sh
chmod 755 /home/ksl7922/Memory_test/run_process.sh

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM