在 RedHat 7.9 上的 systemd 服务 ExecStart 中使用 && 执行多个命令

时间:2021-07-02 10:06:08

标签: redhat systemd

我在 Red Hat Enterprise Linux Server 7.9 (Maipo) 上有这个 systemd 服务

[Unit]
Description = EUM Server Service
PartOf=eum.service
# Start this unit after the app.service start
After=eum.service
After=eum-db.service

[Service]
Type=forking
User=root
WorkingDirectory=/prod/appdynamics/EUMServer/eum-processor/
ExecStart=/usr/bin/sleep 45 && /bin/bash bin/eum.sh start
RemainAfterExit=true
ExecStop=/bin/bash bin/eum.sh stop

[Install]
WantedBy=multi-user.target

失败是因为它试图选择 /usr/bin/sleep 之后的所有内容作为该命令的参数。我只想执行 /usr/bin/sleep 45 并成功执行 bin/eum.sh start。我怎样才能让它工作?

● eum-server.service - EUM Server Service
   Loaded: loaded (/etc/systemd/system/eum-server.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Fri 2021-07-02 00:00:53 CEST; 9min ago
  Process: 13860 ExecStart=/usr/bin/sleep 45 && /bin/bash bin/eum.sh start (code=exited, status=1/FAILURE)

Jul 02 00:00:53 lmlift06mnp001 systemd[1]: Starting EUM Server Service...
Jul 02 00:00:53 lmlift06mnp001 sleep[13860]: /usr/bin/sleep: invalid time interval ‘&&’
Jul 02 00:00:53 lmlift06mnp001 sleep[13860]: /usr/bin/sleep: invalid time interval ‘/bin/bash’
Jul 02 00:00:53 lmlift06mnp001 sleep[13860]: /usr/bin/sleep: invalid time interval ‘bin/eum.sh’
Jul 02 00:00:53 lmlift06mnp001 sleep[13860]: /usr/bin/sleep: invalid time interval ‘start’
Jul 02 00:00:53 lmlift06mnp001 sleep[13860]: Try '/usr/bin/sleep --help' for more information.
Jul 02 00:00:53 lmlift06mnp001 systemd[1]: eum-server.service: control process exited, code=exited status=1
Jul 02 00:00:53 lmlift06mnp001 systemd[1]: Failed to start EUM Server Service.
Jul 02 00:00:53 lmlift06mnp001 systemd[1]: Unit eum-server.service entered failed state.
Jul 02 00:00:53 lmlift06mnp001 systemd[1]: eum-server.service failed.

1 个答案:

答案 0 :(得分:1)

命令行未提供给 shell,因此 && 无效。例如,您需要

ExecStart=/bin/bash -c 'sleep 45 && /bin/bash bin/eum.sh start'

或者你可以将命令分成

ExecStartPre=/usr/bin/sleep 45
ExecStart=/bin/bash bin/eum.sh start