使用 ubuntu 从 systemd 启动服务

使用 ubuntu 从 systemd 启动服务

我正在尝试通过 systemd 启动 .jar 文件(Spring Boot 1.4.0),它返回错误退出代码 126

系统文件夹中的代码

/etc/systemd/system/Painel.service
[Unit] Description=Iniciar painel After=syslog.target

[Service] User=lrv ExecStart=/bin/bash sudo java -jar /home/lrv/painelWeb-20.0.1-SNAPSHOT.jar $

SuccessExitStatus=143 TimeoutStopSec=10 Restart=on-failure RestartSec=5

[Install] WantedBy=multi-user.target

之前显示错误退出代码 127,但我对命令做了一些更改,现在出现错误 126,我所做的主要更改是在 $PATH 中,我添加了 Java

echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/bin/java:/usr/bin/javac:/home/lrv/bin:/usr/lib/jvm/jdk1.8.0/bin

从我看到的错误 126 来看,它可以识别该命令,但它无法执行,这与 127 甚至无法识别它不同,这就是为什么在 ExecStart 中我添加了 sudo

我还使用 sudo chmod +x 授予了脚本权限,并且授予了文件(jar 文件)我想要从 Ubuntu 启动的权限

日志 journalctl -u Painel.service -l -n 50

Painel.service: Failed with result 'exit-code'.
Painel.service: Service hold-off time over, scheduling restart.
Painel.service: Scheduled restart job, restart counter is at 17.
Stopped Iniciar painel.
Started Iniciar painel.
 /usr/bin/sudo: /usr/bin/sudo: unable to run the binary file
 Painel.service: Main process exited, code=exited, status=126/n/a
 Painel.service: Failed with result 'exit-code'.
 Painel.service: Service hold-off time over, scheduling restart.
 Painel.service: Scheduled restart job, restart counter is at 18.
 Stopped Iniciar painel.
 Started Iniciar painel.
 /usr/bin/sudo: /usr/bin/sudo: unable to run the binary file
 Painel.service: Main process exited, code=exited, status=126/n/a
 Painel.service: Failed with result 'exit-code'.
 Painel.service: Service hold-off time over, scheduling restart.
 Painel.service: Scheduled restart job, restart counter is at 19.
 Stopped Iniciar painel.
 Started Iniciar painel.
 /usr/bin/sudo: /usr/bin/sudo: unable to run the binary file
 Painel.service: Main process exited, code=exited, status=126/n/a
 Painel.service: Failed with result 'exit-code'.
 Painel.service: Service hold-off time over, scheduling restart.
 Painel.service: Scheduled restart job, restart counter is at 20.
 Stopped Iniciar painel.
 Started Iniciar painel.
 /usr/bin/sudo: /usr/bin/sudo: unable to run the binary file
 Painel.service: Main process exited, code=exited, status=126/n/a
 Painel.service: Failed with result 'exit-code'.
 Painel.service: Service hold-off time over, scheduling restart.
 Painel.service: Scheduled restart job, restart counter is at 21.
 Stopped Iniciar painel.
 Started Iniciar painel.
/usr/bin/sudo: /usr/bin/sudo: unable to run the binary file
Painel.service: Main process exited, code=exited, status=126/n/a
Painel.service: Failed with result 'exit-code'.

答案1

您无法sudo在 SystemD 单元中安全使用。这是您在日志中收到“无法运行二进制文件”错误的原因之一。

您不需要sudo在服务文件定义中使用,而是需要定义UserGroup在其下运行您的服务,以使其以超级用户权限执行。

User=root和添加Group=root到您的服务部分。sudo从您的ExecStart行中删除。保存文件,然后运行sudo systemctl daemon-reload以刷新 SystemD 知道的服务文件数据。

然后启动您的服务。

相关内容