用于python脚本的shell启动/停止 [英] shell start / stop for python script
问题描述
我有一个简单的python脚本,我需要启动和停止,我需要使用start.sh和stop.sh脚本来执行此操作.
I have a simple python script i need to start and stop and i need to use a start.sh and stop.sh script to do it.
我有start.sh:
I have 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脚本.我认为这是找到pid的一种适当方法,但我不会对此押注太多. start.sh成功启动它.当我运行stop.sh时,我无法再通过"ps aux | grep 'myscript.py'"
找到该进程,但控制台将输出:
I'm mainly concerned with the stop.sh script. I think that's an appropriate way to find the pid but i wouldn't bet much on it. start.sh successfully starts it. when i run stop.sh, i can no longer find the process by "ps aux | grep 'myscript.py'"
but the console outputs:
killing 25052
25058
./stop.sh: 5: kill: No such process
因此似乎可以正常工作,并显示没有这样的过程"的错误提示.
so it seems like it works AND gives an error of sorts with "No such process".
这实际上是错误吗?我是否以一种理智的方式来处理这个问题?还有其他我应该注意的事情吗?
Is this actually an error? Am I approaching this in a sane way? Are there other things I should be paying attention to?
编辑-我实际上得到了这样的内容: start.sh
EDIT - I actually ended up with something like this: 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
进程,因为有些东西匹配.执行完grep后,因此找不到它.
It is because ps aux |grep SOMETHING
also finds the grep SOMETHING
process, because SOMETHING matches. After the execution the grep is finished, so it cannot find it.
添加一行:ps aux | grep -v grep | grep YOURSCRIPT
其中-v表示排除. man grep
中的更多内容.
Where -v means exclude. More in man grep
.
这篇关于用于python脚本的shell启动/停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!