gpt4 book ai didi

linux - BASH shell 脚本在命令提示符下工作正常,但不适用于 crontab

转载 作者:太空宇宙 更新时间:2023-11-04 04:04:58 25 4
gpt4 key购买 nike

这是我想用 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. 我敢打赌,PATH 环境变量在 cron 中设置不正确。插入

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

    看看它目前有什么值(value)。也许您需要在脚本中手动将其设置为正确的值。

    这实际上是常见问题解答

  2. 如果您的系统上没有安装任何mail,以便 cron 转发脚本中的错误消息,则最好手动将所有错误消息重定向到您的首选位置。例如。

    #! /bin/bash
    {
    date

    prgep mlp > /home/ksl7922/proc.txt
    ... snip ...
    fi
    } &> /tmp/cron-msg.txt

关于linux - BASH shell 脚本在命令提示符下工作正常,但不适用于 crontab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21594752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com