与多个execStart一起使用

与多个execStart一起使用

本文介绍了与多个execStart一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

i我想知道是否可以使用以不同输入参数开头的相同脚本创建服务.如:

i Would know if it's possible to create service with the same script started with different input parameters.Such as:

[Unit]
Description=script description

[Service]
Type=simple
ExecStart=/script.py parameters1
ExecStart=/script.py parameters2
Restart=on-failure

[Install]
WantedBy=multi-user.target

有可能吗?那么它将启动为串行模式吗?还是分成两个不同的过程?最好的问候

is it possible? then it will launched to serial-mode? or into two different process? Best regards

推荐答案

如果单元文件中的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/[email protected]. 注意:(@很重要).

You can use template units, so you create a /etc/systemd/system/[email protected]. 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 [email protected] [email protected]

或...

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

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/[email protected]
[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 [email protected]
$ systemctl enable [email protected]
$ systemctl start bar.target

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

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

这篇关于与多个execStart一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 07:28