【问题标题】:Upstart job running script新贵作业运行脚本
【发布时间】:2019-04-19 09:49:45
【问题描述】:

Ubuntu 14.04

我有一份简单的新贵工作test.conf:

root@ubuntutest:~# cat /etc/init/test.conf 
expect fork
script
/root/test.py
end script

和简单的python脚本/root/test.py:

root@ubuntutest:~# cat /root/test.py 
#!/usr/bin/python
import time

print("Hello, World")
time.sleep(30)
print("Goodbye")

我开始测试工作,确保 upstart 跟踪正确的 PID,等待脚本终止,但 upstart 不会停止跟踪不存在的 PID。所以 upstart 总是显示作业正在运行(而实际上它不是)

root@ubuntutest:~# initctl status test
test stop/waiting
root@ubuntutest:~# initctl start test
test start/running, process 1859
root@ubuntutest:~# ps aux | grep 1859
root      1859  0.2  0.7  29832  7188 ?        S    14:43   0:00 /usr/bin/python /root/test.py
root      1862  0.0  0.2  11760  2156 pts/0    S+   14:43   0:00 grep --color=auto 1859
root@ubuntutest:~# ps aux | grep 1859
root      1864  0.0  0.2  11760  2224 pts/0    S+   14:43   0:00 grep --color=auto 1859
root@ubuntutest:~# initctl status test
test start/running, process 1859

如果我从测试作业中删除 expect fork,那么一切正常(除了新贵轨道 sh 而不是 python) - 我想了解我的代码有什么问题?

如果我使用 bash 脚本而不是 python,也会发生同样的情况:

#!/bin/bash

sleep 30

【问题讨论】:

    标签: ubuntu-14.04 upstart


    【解决方案1】:

    为什么不简单地使用exec 节而不是script [...] end script

    description "my python script"
    start on startup
    task
    exec python
    

    或者,您可以从脚本部分执行 python 程序:

    description "my python script, with extra steps"
    start on started dbus
    task
    script
        echo foo >/tmp/bar
        exec python myscript.py
    end script
    

    作为关于为什么 upstart 认为进程仍然存在的简短回答,即使它已经退出:shell 进程正在拦截 upstart 在你的 python 程序退出时通常会收到的信号,并且 Upstart 无法知道进程已经退出而不这些信号。

    【讨论】:

    • 嗯...这是否意味着upstart 无法正确跟踪sh 的子进程?
    • 正确,upstart 无法监督 shell 的子进程。
    猜你喜欢
    • 1970-01-01
    • 2014-01-10
    • 2012-11-11
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    • 2011-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多