shell脚本启动/停止

我有一个简单的Python脚本,我需要启动和停止,我需要使用start.sh和stop.sh脚本来做到这一点。

我有start.sh:

#!/bin/sh script='/path/to/my/script.py' echo 'starting $script with nohup' nohup /usr/bin/python $script & 

和stop.sh

 #!/bin/sh PID=$(ps aux | grep "/path/to/my/script.py" | awk '{print $2}') echo "killing $PID" kill -15 $PID 

我主要关心stop.sh脚本。 我认为这是一个适当的方式来findPID,但我不会打赌很多。 start.sh成功启动它。 当我运行stop.sh,我不能再通过"ps aux | grep 'myscript.py'"find进程,但控制台输出:

 killing 25052 25058 ./stop.sh: 5: kill: No such process 

所以它看起来像它的工作,并给出了“没有这样的过程”的错误。

这实际上是一个错误? 我以理智的方式接近这个吗? 还有其他的事情我应该关注吗?

编辑 – 我实际上结束了这样的事情:start.sh

 #!/bin/bash ENVT=$1 COMPONENTS=$2 TARGETS=("/home/user/project/modules/script1.py" "/home/user/project/modules/script2.py") for target in "${TARGETS[@]}" do PID=$(ps aux | grep -v grep | grep $target | awk '{print $2}') echo $PID if [[ -z "$PID" ]] then echo "starting $target with nohup for env't: $ENVT" nohup python $target $ENVT $COMPONENTS & fi done 

stop.sh

 #!/bin/bash ENVT=$1 TARGETS=("/home/user/project/modules/script1.py" "/home/user/project/modules/script2.py") for target in "${TARGETS[@]}" do pkill -f $target echo "killing process $target" done 

这是因为ps aux |grep SOMETHING也找到了grep SOMETHING过程,因为SOMETHING匹配。 执行完成后,grep完成,所以找不到它。

添加一行: ps aux | grep -v grep | grep YOURSCRIPT ps aux | grep -v grep | grep YOURSCRIPT

其中-v意味着排除。 更多的man grepman grep

init类型的脚本对此很有用。 这与我使用的非常相似。 您将pid存储在一个文件中,当您想检查它是否正在运行时,请查看/ proc文件系统。

 #!/bin/bash script_home=/path/to/my script_name="$script_home/script.py" pid_file="$script_home/script.pid" # returns a boolean and optionally the pid running() { local status=false if [[ -f $pid_file ]]; then # check to see it corresponds to the running script local pid=$(< "$pid_file") local cmdline=/proc/$pid/cmdline # you may need to adjust the regexp in the grep command if [[ -f $cmdline ]] && grep -q "$script_name" $cmdline; then status="true $pid" fi fi echo $status } start() { echo "starting $script_name" nohup "$script_name" & echo $! > "$pid_file" } stop() { # `kill -0 pid` returns successfully if the pid is running, but does not # actually kill it. kill -0 $1 && kill $1 rm "$pid_file" echo "stopped" } read running pid < <(running) case $1 in start) if $running; then echo "$script_name is already running with PID $pid" else start fi ;; stop) stop $pid ;; restart) stop $pid start ;; status) if $running; then echo "$script_name is running with PID $pid" else echo "$script_name is not running" fi ;; *) echo "usage: $0 <start|stop|restart|status>" exit ;; esac 

ps aux | grep“/path/to/my/script.py”

将返回script.py实例的pid和grep的这个实例。 这可能就是为什么你没有这样一个过程:当你开始杀死grep的时候,它已经死了。

“正确的”方法可能是让你的脚本把它的pid写到/ var / run中的一个文件中,当你杀死脚本的时候清除它。 如果更改脚本不是一个选项,请查看start-stop-daemon 。

如果您想继续使用类似grep的方法,请参阅proctools 。 它们内置在大多数GNU / Linux机器上,并且可以在包括OS X的BSD上使用:

 pkill -f /path/to/my/script.py 

目前我还没有unix box,所以我不能测试这个,但是这个想法应该相当简单。

start.sh:

 if [ -e ./temp ] then pid=`cat temp` echo "Process already exists; $pid" else script='/path/to/my/script.py' echo 'starting $script with nohup' nohup /usr/bin/python $script & echo $! > temp fi 

stop.sh:

 if [ -e ./temp ] then pid=`cat temp` echo "killing $pid" kill -15 $PID rm temp else echo "Process not started" fi 

试试这个。