[英](re)start python script with applescript
I have a Python script that runs in an infinite loop (it's a server). 我有一个在无限循环(这是服务器)中运行的Python脚本。
I want to write an AppleScript that will start this script if it isn't started yet, and otherwise force-quit and restart it. 我想编写一个AppleScript,如果尚未启动该脚本,它将启动该脚本,否则强制退出并重新启动它。 This will make it easy for me to make changes to the server code while programming. 这将使我在编程时轻松更改服务器代码。
Currently I only know how to start it: do shell script "python server.py"
目前,我只知道如何启动它: do shell script "python server.py"
On the shell, if you do ps aux | grep python\\ server.py | head -n1
在外壳上,如果您执行ps aux | grep python\\ server.py | head -n1
ps aux | grep python\\ server.py | head -n1
ps aux | grep python\\ server.py | head -n1
, you'll get the ID of the process running server.py
. ps aux | grep python\\ server.py | head -n1
,您将获得运行server.py
的进程的ID。 You can then use kill -9
to kill that process and restart it: 然后,您可以使用kill -9
该进程并重新启动它:
kill -9 `ps aux | grep python\ server.py | head -n1 | python -c 'import sys; print(sys.stdin.read().split()[1])'`
That'll kill it. 那会杀了它。 Al you have to do now is to restart it: Al,您现在要做的是重新启动它:
python server.py
You can combine the two with &&
: 您可以将两者与&&
结合使用:
kill -9 `ps aux | grep python\ server.py | head -n1 | python -c 'import sys; print(sys.stdin.read().split()[1])'` && python server.py
Of course, you already know how to put that in a do shell script
! 当然,您已经知道如何将其放入do shell script
!
Note that AppleScript's do shell script
starts the shell ( /bin/sh
) in the root directory ( /
) by default, so you should specify an explicit path to server.py
请注意,AppleScript的do shell script
默认情况下会在根目录( /
)中启动外壳程序( /bin/sh
),因此您应指定server.py
的显式路径。
In the following examples I'll assume directory path ~/srv
. 在以下示例中,我将假定目录路径~/srv
。
Here's the shell command: 这是shell命令:
pid=$(pgrep -fx 'python .*/server\.py'); [ "$pid" ] && kill -9 $pid; python ~/srv/server.py
As an AppleScript statement, wrapped in do shell script
- note the \\
-escaped inner "
and \\
chars.: 作为AppleScript语句,包装在do shell script
-注意\\
转义的内部"
和\\
字符。:
do shell script "pid=$(pgrep -fx 'python .*/server\\.py'); [ \"$pid\" ] && kill -9 $pid; python ~/srv/server.py"
pgrep -fx '^python .*/server\\.py$'
uses pgrep
to find your running command by regex against the full command line ( -f
), requiring a full match ( -x
), and returns the PID (process ID), if any. pgrep -fx '^python .*/server\\.py$'
使用pgrep
通过正则表达式针对完整的命令行( -f
)查找正在运行的命令,要求完全匹配( -x
),并返回PID(进程ID) )(如果有)。
pgrep
(always) treats its search term as a regular expression . 请注意,我使用了更为抽象的正则表达式来强调pgrep
(始终)将其搜索项视为正则表达式的事实。 python ~/srv/server\\.py
- note the \\
-escaping of .
要将完整的启动命令行指定为正则表达式,请使用python〜 python ~/srv/server\\.py
\\
.py-注意\\
转义.
for full robustness. 具有完全的鲁棒性。 [ "$pid" ] && kill -9 $pid
kills the process, if a PID was found ( [ "$pid" ]
is short for [ -n "$pid" ]
and evaluates to true only if $pid
is nonempty); [ "$pid" ] && kill -9 $pid
杀死进程, 如果找到了PID( [ "$pid" ]
是[ -n "$pid" ]
缩写,并且仅当$pid
为非空时才计算为true) ; -9
sends signal SIGKILL
, which forcefully terminates the process. -9
发送信号SIGKILL
,该信号强制终止进程。
python ~/srv/server.py
then (re)starts your server. python ~/srv/server.py
然后(重新)启动服务器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.