大网数据广告活动
服务器运维问答

After= option not working in systemd service file

· 52CE 我爱测

I have two shell scripts name first.sh and second.sh. The contents of first.sh and second.sh are given below: >>first.sh #!/bin/bash while true do echo The current time is $(date) sleep(1) done >>second.sh #!/bin/bash while true …

问题原文

原帖链接 · Ajay Banstola · CC BY-SA 4.0

I have two shell scripts name first.sh and second.sh. The contents of first.sh and second.sh are given below:

    >>first.sh
    #!/bin/bash
    while true
    do 
        echo The current time is $(date)
        sleep(1)
    done



    >>second.sh
    #!/bin/bash
    while true
    do 
        echo The current time from second script is $(date)
        sleep(1)
    done

Similarly, the content of first.service is:

[Unit]
Description=first service
After=second.service
[Service]
ExecStart=/home/abanstola/first.sh

The content of second.service is:

[Unit]
Description=Second service
[Service]
ExecStart=/home/abanstola/second.sh

Note the After=second.service in the first.service code. According to the docs, the first service should not be running until the second service starts. But even if I disable or stop the second service, the first service runs without any problem. What am I doing wrong here?

已采纳解答

原帖链接 · Gerald Schneider · CC BY-SA 4.0

You misread the documentation. Quote:

If unit foo.service contains the setting Before=bar.service and both units are being started, bar.service's start-up is delayed until foo.service has finished starting up. After= is the inverse of Before=

Highlight by me. This only has an effect if both units are being started. If you disable one of them, it has no effect.

It's not quite clear what you actually want.

Probably better choices are:

Requires=

Similar to Wants=, but declares a stronger requirement dependency. Dependencies of this type may also be configured by adding a symlink to a .requires/ directory accompanying the unit file.

If this unit gets activated, the units listed will be activated as well. If one of the other units fails to activate, and an ordering dependency After= on the failing unit is set, this unit will not be started. Besides, with or without specifying After=, this unit will be stopped if one of the other units is explicitly stopped.

PartOf=

Configures dependencies similar to Requires=, but limited to stopping and restarting of units. When systemd stops or restarts the units listed here, the action is propagated to this unit. Note that this is a one-way dependency — changes to this unit do not affect the listed units.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2021-12-01)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。