我正在使用 Ubuntu 的 upstart 功能运行我的 Python 脚本,这样,如果我的 Python 脚本因某种原因死亡或被终止,它可以自动重新启动,并且一切都正常工作 -
所以我决定使用UPSTART
Ubuntu 的功能自动重启 Python 脚本。
testing.conf
在以下位置创建如下文件后/etc/init/testing.conf
-
start on runlevel [2345]
stop on runlevel [016]
chdir /tekooz
exec python testing.py
respawn
我运行下面的 sudo 命令来启动它,我可以看到该进程正在运行,ps ax
并且我的 python 脚本也运行良好。
root@bx13:/tekooz# sudo start testing
testing start/running, process 27794
现在,如果我终止上述进程的 PID,那么它会在几毫秒或微秒内自动重新启动。
如果我的 Python 脚本关闭或被终止,是否有可能在 1 秒后重新启动它?我不想立即重新启动。
答案1
我不太明白你为什么要使用根目录进行这个“测试”,而且我也不认为这是你想要做的(最终可能会严重破坏你的系统)。另外,请注意,如果你是 root(提示符#
),则不需要“sudo”。请考虑编辑问题以反映你想要实现的目标。
为了完整起见,我将重复一遍:
考虑放入 conf 文件,
~/.config/upstart/
这样您就不需要 sudo:initctl start testing
例如,您可以使用它来启动脚本。(我假设您的 conf 文件不需要 root 权限。)
阅读时食谱,它表示pre-start
,、post-start
和post-stop
节将在重生的作业上重新运行。这意味着您可以执行以下操作:
start on runlevel [2345]
stop on runlevel [016]
chdir /path/to/a/folder
respawn
exec python testing.py
post-stop script
sleep 1
end script
这样,脚本重新生成之前会有 1 秒的延迟。(您可以对节执行相同操作pre-start
,但这样第一次调用会有 1 秒的延迟。)