启动时脚本未 运行,systemd ubuntu

Script not running on start up, systemd ubuntu

我目前正在尝试在我的 Ubuntu 服务器启动时启动 Luigid,我尝试了几种技术,包括 rc.local、cronjob(@reboot)、upstart、systemd 和 none他们中的一些人似乎在工作。

我应该指出,如果我手动执行命令 运行 没问题,我只需要它在启动时 运行。在这一点上,我真的不关心我用哪种方式让它工作,所以这里有一些我试过的东西 -

Cron:

二手

sudo crontab -e

并输入

@reboot luigid --background --logdir /home/myuser/luigilog

Systemd:

我在 /usr/bin 中有一个名为 luigid 的脚本,它包含以下内容,它被标记为可执行文件,我已经尝试过使用和不使用 "exit 0" 担心可能需要正确的退出代码 -

#!/bin/sh
exec luigid --background --logdir /home/myuser/luigilog

和 /etc/systemd/system/ 中的服务文件名为 luigid.service -

[Unit]
Description=Luigid Service

[Service]
Type=forking
ExecStart=/usr/bin/luigid

[Install]
WantedBy=multi-user.target

我试过分叉(并在服务和 lugid 命令中指定 PID 文件,一次性和简单的类型,没有运气。

我已使用 -

启用该服务
systemctl enable luigid.service

它似乎试图启动服务,因为使用

检查状态

systemctl status luigid.service

显示

luigid.service - Luigid Service
   Loaded: loaded (/etc/systemd/system/luigid.service; enabled; vendor preset: enabled)
   Active: failed (Result: timeout) since Mon 2016-10-10 15:18:00 UTC; 18min ago

Oct 10 15:16:29 Analytics systemd[1]: Starting Luigid Service...
Oct 10 15:18:00 Analytics systemd[1]: luigid.service: Start operation timed out. Terminating.
Oct 10 15:18:00 Analytics systemd[1]: Failed to start Luigid Service.
Oct 10 15:18:00 Analytics systemd[1]: luigid.service: Unit entered failed state.
Oct 10 15:18:00 Analytics systemd[1]: luigid.service: Failed with result 'timeout'.

一定有一些明显的东西我遗漏了,在启动时向 运行 发送命令真的不会这么难!

查看启动选项:如果服务稍后崩溃,"rc.local" 和 "cron @reboot" 都不会帮助您,因此它们并不理想。由于看起来您有一个基于 systemd 的系统,因此 Upstart 并不是一个真正的选择。 systemd 是一个值得关注的好选择,因为它不仅会启动您的进程,还会在它崩溃时重新启动它。

我不知道 Type= 的正确选项对于这项服务是什么,但这很重要。查看 man systemd.serviceType= 的文档以确定哪一个适合您的服务。

不需要您的包装器脚本。你应该能够有一个像这样的 ExecStart= 行:

ExecStart=/usr/bin/luigid --background --logdir /home/myuser/luigilog

如果您还没有尝试过该组合,我也会尝试删除 --background 选项并将 Type= 设置为 simple

如果你不能让它工作,试着让你的守护进程提供更详细的日志记录为什么它没有启动,或者求助于添加 /usr/bin/strace 到你的 ExecStart= 行的开头获取二进制文件在启动期间进行的系统调用的详细记录。最后几个调用之一可能指向它挂起的位置。