如果程序没有运行,请通过检查他的pid重新启动它

时间:2017-10-26 20:21:46

标签: linux bash shell

你们其中一个人可以告诉我,我的bash脚本是否合适。我的目标是这个脚本查看bash.pid(包含pid号的文件),然后如果pid正在运行,脚本将退出0,如果没有,则重新运行程序我执行./run

脚本:

#!/bin/sh
# Printing the directory
pwd > dir.dir
dir=$(cat dir.dir)
# Identifying the directory
pid=\$(cat $dir/bash.pid
# looking if pid is running - if it is running just exit
if \$(kill -CHLD \$pid >/dev/null 2>&1)
then
exit 0
fi
fi
# if not running then re-launch the program
cd $dir
./run &>/dev/null" > upd
chmod u+x upd
./upd

感谢。

1 个答案:

答案 0 :(得分:0)

我不知道应用程序“run”或“udp”是否是您要运行的应用程序。我们假设它是“运行”。

在脚本结束时,运行的pid应写入bash.pid,以便下次检查脚本。

在我们重启“run”之后,我们应该再次确认它是否已经开始。

通常我们使用run.pid来保持运行的pid。

#!/bin/sh
# Printing the directory
pwd > dir.dir
dir=$(cat dir.dir)
# Identifying the directory
pid=$(cat $dir/bash.pid)
# looking if pid is running - if it is running just exit
if $(kill -CHLD $pid >/dev/null 2>&1)
then
    echo  "run has been started\n"
    exit 0
fi
# if not running then re-launch the program
cd $dir
chmod u+x run
./run & 
pgrep run > bash.pid
pid=$(cat $dir/bash.pid)
if $(kill -CHLD $pid >/dev/null 2>&1)
    echo "failed to start run \n"
then
    echo  "run has been started\n"
fi