且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

与多个execStart一起使用

更新时间:2023-09-19 10:35:16

如果单元文件中的Type=simple仅可以指定一个ExecStart,但是可以添加尽可能多的ExecStartPre, ExecStartPost`,但这都不是适用于长时间运行的命令,因为它们是顺序执行的,并且每次启动之前的所有操作都将在启动下一个命令之前被终止.

if Type=simple in your unit file, you can only specify one ExecStart, but you can add as many ExecStartPre,ExecStartPost`, but none of this is suited for long running commands, because they are executed serially and everything one start is killed before starting the next one.

如果Type=oneshot您可以指定多个ExecStart,则它们将串行运行而不是并行运行.

If Type=oneshot you can specify multiple ExecStart, they run serially not in parallel.

如果要并行运行多个单元,则可以执行以下操作:

If what you want is to run multiple units in parallel, there a few things you can do:

您可以使用模板单元,因此可以创建/etc/systemd/system/foo@.service. 注意:(@很重要).

You can use template units, so you create a /etc/systemd/system/foo@.service. NOTE: (the @ is important).

[Unit]
Description=script description %I

[Service]
Type=simple
ExecStart=/script.py %i
Restart=on-failure

[Install]
WantedBy=multi-user.target

然后执行:

$ systemctl start foo@parameter1.service foo@parameter2.service

或...

您可以创建链接到单个目标的多个单元:

You can create multiple units that links to a single target:

#/etc/systemd/system/bar.target
[Unit]
Description=bar target
Requires=multi-user.target
After=multi-user.target
AllowIsolate=yes

然后您只需将.service单元修改为WantedBy=bar.target,如:

And then you just modify you .service units to be WantedBy=bar.target like:

#/etc/systemd/system/foo@.service
[Unit]
Description=script description %I

[Service]
Type=simple
ExecStart=/script.py %i
Restart=on-failure

[Install]
WantedBy=bar.target

然后,您只需并行启用所需的foo服务,并像这样启动bar目标:

Then you just enable the foo services you want in parallel, and start the bar target like this:

$ systemctl daemon-reload
$ systemctl enable foo@param1.service
$ systemctl enable foo@param2.service
$ systemctl start bar.target

注意::这适用于任何类型的单元,而不仅是模板单元.

NOTE: that this works with any type of units not only template units.