繁体   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