2017-05-30 177 views
1

你好我正在尝试编写一个脚本来从命令行重新启动其他脚本。 用法应该是:重新启动脚本的脚本

重启someotherscript.sh

猫重启

#!/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 & 

输出为:

[email protected]:/scripts# restart pricealert.sh 
Killed 
[email protected]: 

我重新启动脚本是杀害自己。 这里有什么问题?你能帮我么?

+2

更好地使用'pgrep'甚至'pkill' –

回答

0

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

您可以添加一个if语句来解决这个问题($$是行书的PID):

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

非常感谢这个固定我的问题.. ..我欣赏 –