从 Upstart 到 Systemd

从 Upstart 到 Systemd

我们在 中有一个 Upstart 作业/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: Unable to connect to Upstart: Failed to connect to socket /com/ubuntu/upstart: Connection refused

我发现在 Ubuntu 16.04 中 Upstart 未被使用,并且已经转移到 Systemd,并且 Systemd 文件需要位于 - 位置/etc/systemd/system,文件扩展名为.service。之后,要运行 systemd 服务,需要触发以下 2 个命令:

sudo systemctl daemon-reload
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 脚本吗?

输出systemctl show private-api.service-

Type=oneshot
Restart=on-failure
NotifyAccess=none
RestartUSec=2min
TimeoutStartUSec=infinity
TimeoutStopUSec=1min 30s
RuntimeMaxUSec=infinity
WatchdogUSec=0
WatchdogTimestampMonotonic=0
FailureAction=none
PermissionsStartOnly=no
RootDirectoryStartOnly=no
RemainAfterExit=no
GuessMainPID=yes
MainPID=0
ControlPID=0
FileDescriptorStoreMax=0
NFileDescriptorStore=0
StatusErrno=0
Result=success
ExecMainStartTimestampMonotonic=0
ExecMainExitTimestampMonotonic=0
ExecMainPID=0
ExecMainCode=0
ExecMainStatus=0
Slice=system.slice
MemoryCurrent=18446744073709551615
CPUUsageNSec=18446744073709551615
TasksCurrent=18446744073709551615
Delegate=no
CPUAccounting=no
CPUShares=18446744073709551615
StartupCPUShares=18446744073709551615
CPUQuotaPerSecUSec=infinity
BlockIOAccounting=no
BlockIOWeight=18446744073709551615
StartupBlockIOWeight=18446744073709551615
MemoryAccounting=no
MemoryLimit=18446744073709551615
DevicePolicy=auto
TasksAccounting=no
TasksMax=18446744073709551615
UMask=0022
LimitCPU=18446744073709551615
LimitCPUSoft=18446744073709551615
LimitFSIZE=18446744073709551615
LimitFSIZESoft=18446744073709551615
LimitDATA=18446744073709551615
LimitDATASoft=18446744073709551615
LimitSTACK=18446744073709551615
LimitSTACKSoft=8388608
LimitCORE=18446744073709551615
LimitCORESoft=0
LimitRSS=18446744073709551615
LimitRSSSoft=18446744073709551615
LimitNOFILE=4096
LimitNOFILESoft=1024
LimitAS=18446744073709551615
LimitASSoft=18446744073709551615
LimitNPROC=31855
LimitNPROCSoft=31855
LimitMEMLOCK=65536
LimitMEMLOCKSoft=65536
LimitLOCKS=18446744073709551615
LimitLOCKSSoft=18446744073709551615
LimitSIGPENDING=31855
LimitSIGPENDINGSoft=31855
LimitMSGQUEUE=819200
LimitMSGQUEUESoft=819200
LimitNICE=0
LimitNICESoft=0
LimitRTPRIO=0
LimitRTPRIOSoft=0
LimitRTTIME=18446744073709551615
LimitRTTIMESoft=18446744073709551615
WorkingDirectory=/home/ubuntu/private-api
OOMScoreAdjust=0
Nice=0
IOScheduling=0
CPUSchedulingPolicy=0
CPUSchedulingPriority=0
TimerSlackNSec=50000
CPUSchedulingResetOnFork=no
NonBlocking=no
StandardInput=null
StandardOutput=journal
StandardError=inherit
TTYReset=no
TTYVHangup=no
TTYVTDisallocate=no
SyslogPriority=30
SyslogLevelPrefix=yes
SyslogLevel=6
SyslogFacility=3
SecureBits=0
CapabilityBoundingSet=18446744073709551615
AmbientCapabilities=0
User=root
MountFlags=0
PrivateTmp=no
PrivateNetwork=no
PrivateDevices=no
ProtectHome=no
ProtectSystem=no
SameProcessGroup=no
UtmpMode=init
IgnoreSIGPIPE=yes
NoNewPrivileges=no
SystemCallErrorNumber=0
RuntimeDirectoryMode=0755
KillMode=control-group
KillSignal=15
SendSIGKILL=yes
SendSIGHUP=no
Id=private-api.service
Names=private-api.service
Requires=sysinit.target
Wants=network-online.target
Conflicts=shutdown.target
Before=shutdown.target
After=network.target basic.target systemd-journald.socket sysinit.target network-online.target
RequiresMountsFor=/home/ubuntu/private-api
Description=Upstart for AMS private-CMS
LoadState=error
ActiveState=inactive
SubState=dead
FragmentPath=/etc/systemd/system/private-api.service
UnitFileState=disabled
UnitFilePreset=enabled
StateChangeTimestampMonotonic=0
InactiveExitTimestampMonotonic=0
ActiveEnterTimestampMonotonic=0
ActiveExitTimestampMonotonic=0
InactiveEnterTimestampMonotonic=0
CanStart=no
CanStop=no
CanReload=no
CanIsolate=no
StopWhenUnneeded=no
RefuseManualStart=no
RefuseManualStop=no
AllowIsolate=no
DefaultDependencies=yes
OnFailureJobMode=replace
IgnoreOnIsolate=no
NeedDaemonReload=no
JobTimeoutUSec=infinity
JobTimeoutAction=none
ConditionResult=no
AssertResult=no
ConditionTimestampMonotonic=0
AssertTimestampMonotonic=0
LoadError=org.freedesktop.DBus.Error.InvalidArgs "Invalid argument"
Transient=no
StartLimitInterval=10000000
StartLimitBurst=5
StartLimitAction=none

cat private-api.service 的输出 -

[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

答案1

首先,您发布的文件确实报告了“绝对路径”错误,经 Ubuntu 16.04 使用 systemd 229 检查发现。您可以使用以下命令自行检查文件的语法:

systemd-analyze verify /etc/systemd/system/private-api.service

其次,不需要包含User=root在文件中。系统单元默认由 root 运行。

第三,尝试以这种方式重定向服务的输出可能不起作用,也不建议这样做。systemdStandardOutput=StandardInput=指令来控制此输出的去向。 默认情况下,它会做一些有用的事情,它将数据发送到 systemd 日志。 如果您只想查看服务的日志,您可以使用:

journalctl -u private-api

答案2

我注意到几件事:

  1. 我认为 ExecStart 参数可能有误。您是否在控制台中尝试过:复制命令并查看它是否“自行”运行?

java 确实安装在 /usr/bin/java 下吗?ls -l /usr/bin/java

  1. 我认为 jar 文件的路径中缺少一个斜线:

ExecStart=/usr/bin/java -classpath home/ubuntu/private-api/private-api-0.0.1-SNAPSHOT.jar

应该

ExecStart=/usr/bin/java -classpath /home/ubuntu/private-api/private-api-0.0.1-SNAPSHOT.jar

与日志文件的路径相同。

  1. 您需要一个 ExecStop 脚本来告诉 systemd 如何正常关闭您的服务。

相关内容