我们在 /etc/init 位置有一个名为 private-api.conf 的 .conf 文件,
所以我的文件位置是 - /etc/init/private-api.conf
我的文件内容如下-
# start when server starts
start on runlevel [23456]
# Stop when server shuts down/reboots
stop on shutdown
#Respawn the process if it crashes
#If it respawns more than 10 times in 5 seconds stop
respawn
respawn limit 10 5
#expect fork
script
cd /home/ubuntu/private-api && exec java -jar -Dspring.profiles.active=stage private-api-SNAPSHOT.jar > private-api.log 2>&1
end script
我需要执行的下一个命令是 -
sudo initctl reload-configuration
之后我应该运行该服务,使用以下命令 -
service private-api start/stop/restart/status
当我这样做时出现以下错误 -
initctl:无法连接到 Upstart:无法连接到套接字/com/ubuntu/upstart:连接被拒绝
我发现在 Ubuntu 16.04 中 upstarts 不起作用,现在已经转移到 Systemd,
并且 Systemd 文件需要位于位置 - /etc/systemd/system
,文件扩展名为 .service
之后运行 Systemd 需要触发以下 2 个命令 -
1. sudo systemctl daemon-reload
2. sudo systemctl start xyz.service
我指的是以下链接 -
1. https://wiki.ubuntu.com/SystemdForUpstartUsers
2. https://www.digitalocean.com/community/tutorials/understanding-systemd-units-and-unit-files
到目前为止,这是我使用上述引用文献所取得的成就 -
[Unit]
Description=Upstart for Private API
After=network.target network-online.target
Wants=network-online.target
[Service]
User=root
WorkingDirectory=/home/ubuntu/private-api
ExecStart=/usr/bin/java -classpath home/ubuntu/private-api/private-api-0.0.1-SNAPSHOT.jar -Dspring.profiles.active=stage > home/ubuntu/private-api/private-api.log 2>&1
SuccessExitStatus=143
Restart=on-failure
RestartSec=120s
[Install]
WantedBy=multi-user.target
我重新加载了我的 Systemd 服务 -
sudo systemctl daemon-reload
但是当我检查服务状态时,出现绝对路径错误 -
sudo systemctl status private-api.service
Apr 05 08:48:56 ip-10-10-1-153 systemd[1]: [/etc/systemd/system/private-api.service:9] Executable path is not absolute, ignoring: ExecStart=/usr/bin/java -classpath /home/ubuntu/private/astro-private-0.0.1-SNAPSHOT.jar -Dspring.profiles.active=dev > private.log 2>&1
Apr 05 08:48:56 ip-10-10-1-153 systemd[1]: private-api.service: Service lacks both ExecStart= and ExecStop= setting. Refusing.
Apr 05 08:49:07 ip-10-10-1-153 systemd[1]: [/etc/systemd/system/private-api.service:9] Executable path is not absolute, ignoring: ExecStart=/usr/bin/java -classpath /home/ubuntu/private/astro-private-0.0.1-SNAPSHOT.jar -Dspring.profiles.active=dev > private.log 2>&1
Apr 05 08:49:07 ip-10-10-1-153 systemd[1]: private-api.service: Service lacks both ExecStart= and ExecStop= setting. Refusing.
Apr 05 08:51:40 ip-10-10-1-153 systemd[1]: [/etc/systemd/system/private-api.service:9] Executable path is not absolute, ignoring: ExecStart=/usr/bin/java -classpath /home/ubuntu/private/astro-private-0.0.1-SNAPSHOT.jar -Dspring.profiles.active=dev > /home/ubuntu/private/private.log 2>&1
Apr 05 08:51:40 ip-10-10-1-153 systemd[1]: private-api.service: Service lacks both ExecStart= and ExecStop= setting. Refusing.
Apr 05 09:17:41 ip-10-10-1-153 systemd[1]: [/etc/systemd/system/private-api.service:9] Executable path is not absolute, ignoring: ExecStart=/usr/bin/java -classpath /home/ubuntu/private/astro-private-0.0.1-SNAPSHOT.jar -Dspring.profiles.active=dev > /home/ubuntu/private/private.log 2>&1
Apr 05 09:17:41 ip-10-10-1-153 systemd[1]: private-api.service: Service lacks both ExecStart= and ExecStop= setting. Refusing.
Apr 05 09:17:59 ip-10-10-1-153 systemd[1]: [/etc/systemd/system/private-api.service:9] Executable path is not absolute, ignoring: ExecStart=/usr/bin/java -classpath /home/ubuntu/private/astro-private-0.0.1-SNAPSHOT.jar -Dspring.profiles.active=dev > /home/ubuntu/private/private.log 2>&1
Apr 05 09:17:59 ip-10-10-1-153 systemd[1]: private-api.service: Service lacks both ExecStart= and ExecStop= setting. Refusing.
有人可以帮我转换我当前的 upstart .conf 脚本吗?
最佳答案
您已经设置了工作目录,您可以直接使用脚本中的命令。
ExecStart=/usr/bin/java -jar -Dspring.profiles.active=stage > private-api.log 2>&1
希望这能奏效。
关于ubuntu-16.04 - Upstart 到 Systemd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42998212/