systemd'在启动时以不同的用户login时运行shell脚本'

问题:我想要在启动时从不同用户的login环境运行一个shell脚本。 这是我的.service文件:

[Unit] Description=Weather Application Server [Service] Type=oneshot User=weather RemainAfterExit=yes ExecStart=/usr/local/weather/startfeed ExecStop=/usr/local/weather/stopfeed [Install] WantedBy=multi-user.target 

这从命令行工作(即键入“systemctl开始/停止weatherService”)。 但是,当我重新启动机器,我通过journalctl -se得到以下错误:

 -- Subject: Unit weatherServer.service has begun start-up -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel -- -- Unit weatherServer.service has begun starting up. Jan 10 19:21:48 localhost.local.local systemd[1]: Started D-Bus System Message Bus. Jan 10 19:21:48 localhost.local.local systemd[784]: Failed at step USER spawning /usr/local/weather/startfeed: No such file or directory -- Subject: Process /usr/local/weather/startfeed could not be executed -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel -- 

我是一个systemd newbe,但是我search了高和低。 为什么我可以通过命令行'systemctl start / stop'来运行这个命令,而从命令行运行它,而不是从启动?


固定!!!!

解决scheme…

原始post中的“单元文件”configuration是正确的。 运行这个的系统使用了一个ActiveDirectory类似的进程,当这个进程被触发时,它还没有完全启动。 为我们工作的解决scheme是添加一个“计时器”(即.timer文件)。 下面的weatherServer.timer文件是为我们工作的:

 [Unit] Description=Weather Application Server Timer [Timer] OnBootSec=30 [Install] WantedBy=multi-user.target 

希望这会帮助也遇到这个问题的人。

看起来您的脚本正在试图在挂载/ usr / local / weather的文件系统之前运行。 你想要做的是放在一个或两个依赖关系告诉systemd等待,直到文件系统被安装在运行之前。 这可以通过在单元文件的[Unit]部分添加一个After=行来完成。 例如:

 [Unit] Description=Weather Application Server After=network.target local-fs.target 

进行更改后,您将需要运行systemctl daemon-reload ,或者您可以重新启动以查看是否可以解决您的问题。