具有多个 execStart 的 Systemd [英] Systemd with multiple execStart
问题描述
我想知道是否可以使用以不同输入参数启动的相同脚本创建服务.如:
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?
推荐答案
如果在你的单元文件中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.
这篇关于具有多个 execStart 的 Systemd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!