脚本重新启动另一个脚本

时间:2017-05-30 20:24:59

标签: bash

您好我正在尝试编写脚本以从命令行重新启动其他脚本。 用法应该是:

重启someotherscript.sh

cat restart

#!/bin/bash

for pids in $(ps -ef | grep $1 | grep -v grep | awk '{print $2}')
do 
kill -9 $pids
done
echo test
sleep 10
$1 &

输出是:

 root@xxxx:/scripts# restart pricealert.sh 
 Killed
 root@xxxx:

我的重启脚本正在自杀。 这有什么不对?你能帮我吗?

1 个答案:

答案 0 :(得分:0)

您的脚本在搜索结果中找到了自己,因为用于启动脚本的命令包含您尝试杀死的脚本名称。

您可以添加if语句来解决此问题($$是正在运行的脚本的pid):

if [ "$$" != "$pids" ]; then
     kill -9 $pids
fi