python脚本的shell启动/停止
我有一个简单的python脚本需要启动和停止,我需要使用start.sh和stop.sh脚本来完成 我已经开始了。sh:python脚本的shell启动/停止,python,linux,shell,Python,Linux,Shell,我有一个简单的python脚本需要启动和停止,我需要使用start.sh和stop.sh脚本来完成 我已经开始了。sh: #!/bin/sh script='/path/to/my/script.py' echo 'starting $script with nohup' nohup /usr/bin/python $script & 停下来,嘘 #!/bin/sh PID=$(ps aux | grep "/path/to/my/script.py" | awk '{print
#!/bin/sh
script='/path/to/my/script.py'
echo 'starting $script with nohup'
nohup /usr/bin/python $script &
停下来,嘘
#!/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”找到进程,但控制台输出:
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
住手,嘘
#!/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将完成,因此无法找到它
添加一行:ps aux|grep-v grep|grep YOURSCRIPT
其中-v表示排除。更多信息请访问man grep
ps aux | grep”/path/to/my/script.py
将返回script.py实例和grep实例的pid。这可能就是为什么你没有这样的过程:当你开始杀死格雷普时,它已经死了 方法可能是让脚本将其pid写入/var/run中的文件,并在终止脚本时将其清除。如果无法更改脚本,请查看
如果您想继续使用类似于grep
的方法,请查看。它们内置于大多数GNU/Linux机器上,在BSD(包括OS X)上随时可用:
pkill -f /path/to/my/script.py
我目前还没有一个unix设备,所以我无法测试它,但是它应该很容易理解 start.sh:
if[-e./temp]
然后
pid=`cat温度`
echo“进程已存在;$pid”
其他的
script='/path/to/my/script.py'
echo“使用nohup启动$script”
nohup/usr/bin/python$script&
echo$!>临时雇员
fi
stop.sh:
if[-e./temp]
然后
pid=`cat温度`
echo“杀死$pid”
杀死-15美元
室温
其他的
回显“进程未启动”
fi
试试看。初始化类型脚本对此很有用。这和我用的非常相似。您将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
#/bin/bash
script_home=/path/to/my
script_name=“$script_home/script.py”
pid_文件=“$script_home/script.pid”
#返回布尔值和可选的pid
运行(){
本地状态=false
如果[[-f$pid_file]];则
#检查以查看它是否对应于正在运行的脚本
本地pid=$(<“$pid\U文件”)
本地cmdline=/proc/$pid/cmdline
#您可能需要在grep命令中调整regexp
如果[[-f$cmdline]]&&grep-q“$script_name”$cmdline;则
status=“true$pid”
fi
fi
echo$状态
}
开始(){
echo“启动$script\u name”
nohup“$script\u name”&
echo$!>“$pid\U文件”
}
停止(){
#如果pid正在运行,则'kill-0 pid'会成功返回,但不会返回
#真的杀了它。
kill-0$1&&kill$1
rm“$pid_文件”
回声“停止”
}
读取运行pid<您可以使用$获得上一个命令的pid代码>。然后您可以在stop.sh
文件中使用它。您也没有任何东西可以处理多次启动,因此它会检查文件是否存在。看见