linux Crontab失败的shell脚本if语句

e5nqia27  于 2024-01-06  发布在  Linux
关注(0)|答案(1)|浏览(124)

我有一个shell脚本,它首先检查Python程序是否正在运行,
然而,问题是,它似乎在if语句的第一个块失败了。
这就是我的shell脚本的样子:

PATH=/opt/conda/bin:/opt/conda/condabin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

if [ $(/bin/pgrep -f "miner_nbeats.py") ]; then
    echo "script running"
else
    echo "script not running"
    exec tmux new-session -d \; send-keys "source activate python310 && cd /home/putsncalls23/directory/ && python miner_nbeats.py" Enter
fi

字符串
这就是我的/etc/crontab文件:

SHELL=/bin/bash
PATH=/opt/conda/bin:/opt/conda/condabin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

*/5 *    * * *  root    putsncalls23 -l /home/putsncalls23/run_script.sh


有人知道我哪里做错了吗?

编辑:

在做了更多的谷歌搜索后,我也看到了这样的东西,

PATH=/opt/conda/bin:/opt/conda/condabin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
if /bin/pgrep -f "miner_nbeats.py" >/dev/null; then
    echo "script running"
else
    echo "script not running"
    tmux new-session \; send-keys "source activate python310 && cd /home/putsncalls23/directory && python miner_nbeats.py$
fi


我只是想澄清一下新的shell脚本是否有效,以及/bin/pgrep -f "miner_nbeats.py" >/dev/null的效果如何。例如,在执行miner_beats.py之后,它似乎输出:

21080
21128


谢谢

blmhpbnm

blmhpbnm1#

我建议做一些修改。首先,不要替换进程中的stdout,只需检查退出代码。接下来,你的“cd”命令试图cd到一个文件。我认为你需要这样的东西:

PATH=/opt/conda/bin:/opt/conda/condabin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

if /bin/pgrep -f "miner_nbeats.py" > /dev/null; then
    echo "script not running"
    exec tmux new-session -d \; send-keys "source activate python310 && cd /home/putsncalls23 && ./miner_nbeats.py" Enter
else
    echo "script running"
fi

字符串

相关问题