在服务器启动时运行 Upstart 脚本时出现问题

在服务器启动时运行 Upstart 脚本时出现问题

我有一个使用以下代码的 Upstart 脚本:

start on runlevel [2345]
stop on runlevel [!2345]

respawn

env PYTHONPATH=$PYTHONPATH:/opt/lrms/lib/python2.6/site-packages

exec python /opt/foo_service/src/foo/foo.py >> /var/log/foo.log 2>&1

pre-start script
echo "[`date`] - Starting Foo Service..." >> /var/log/foo.log
end script

pre-stop script
echo "[`date`] - Stopping Foo Service..." >> /var/log/foo.log
end script

post-stop script
echo "[`date`] - Post-Stop Stanza..." >> /var/log/foo.log
end script

start foo我可以使用或轻松启动和停止该服务stop foo。但是,当我重新启动服务器并检查服务状态时,它处于stop/waiting.

然后我检查日志,并看到以下内容(这些是启动过程中此新贵脚本发生的事件):

[Mon Aug 22 12:20:34 EDT 2016] - Starting Foo Service...
[Mon Aug 22 12:20:35 EDT 2016] - Post-Stop Stanza...
[Mon Aug 22 12:20:35 EDT 2016] - Starting Foo Service...
[Mon Aug 22 12:20:35 EDT 2016] - Post-Stop Stanza...
[Mon Aug 22 12:20:35 EDT 2016] - Starting Foo Service...
[Mon Aug 22 12:20:35 EDT 2016] - Post-Stop Stanza...
[Mon Aug 22 12:20:35 EDT 2016] - Starting Foo Service...
[Mon Aug 22 12:20:36 EDT 2016] - Post-Stop Stanza...
[Mon Aug 22 12:20:36 EDT 2016] - Starting Foo Service...
[Mon Aug 22 12:20:36 EDT 2016] - Post-Stop Stanza...
[Mon Aug 22 12:20:36 EDT 2016] - Starting Foo Service...
[Mon Aug 22 12:20:36 EDT 2016] - Post-Stop Stanza...
[Mon Aug 22 12:20:36 EDT 2016] - Starting Foo Service...
[Mon Aug 22 12:20:36 EDT 2016] - Post-Stop Stanza...
[Mon Aug 22 12:20:36 EDT 2016] - Starting Foo Service...
[Mon Aug 22 12:20:36 EDT 2016] - Post-Stop Stanza...
[Mon Aug 22 12:20:36 EDT 2016] - Starting Foo Service...
[Mon Aug 22 12:20:37 EDT 2016] - Post-Stop Stanza...
[Mon Aug 22 12:20:37 EDT 2016] - Starting Foo Service...
[Mon Aug 22 12:20:37 EDT 2016] - Post-Stop Stanza...
[Mon Aug 22 12:20:37 EDT 2016] - Starting Foo Service...
[Mon Aug 22 12:20:37 EDT 2016] - Post-Stop Stanza...

对我来说,这意味着新贵脚本正在崩溃,考虑到停止前节没有被执行,并且它重生了多次。

我的下一个猜测是,Python 脚本可能依赖于新贵脚本启动时当前未运行的某些内容。 Python 服务从 ActiveMQ 捕获事件并将其存储到 MySQL 中。我尝试编辑 Upstart 脚本,以便它在 MySQL 准备就绪时运行,但这根本没有向日志文件输出任何内容。

我是否正确地假设这可能是 Python 服务的问题,具体取决于正在运行的其他服务?我应该如何调试这个问题?

答案1

我想出了解决办法。我的 Python 脚本确实依赖于 MySQL,因此我需要通过添加initctl emit mysql-started/etc/init.d/mysqld.

然后在我的新贵脚本中我可以使用start on started mysql-started而不是start on runlevel [2345].

相关内容