Systemd with multiple execStart
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.
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:
If they differ on 1 param
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
And then you exec:
$ systemctl start foo@parameter1.service foo@parameter2.service
or...
Target dependencies
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
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
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.
Execute multiple commands with && in systemd service ExecStart on RedHat 7.9
The command line is not given to a shell, so &&
is not valid. You would need, for example,
ExecStart=/bin/bash -c 'sleep 45 && /bin/bash bin/eum.sh start'
or you could separate the commands into
ExecStartPre=/usr/bin/sleep 45
ExecStart=/bin/bash bin/eum.sh start
Systemd service with multiple execStart and watchdog for each execStart
I found that using the template service is the better solution and covers what I need. Thanks to you all.
How to execute multiple command in ExecStart
A separate script is a possible solution, as Charles wrote, or you can run the command with bash -c
or sh -c
, like this:
/bin/bash -c '/bin/tar -zcvf "/var/log/test/$(/bin/date)_syslog_archive.tar.gz" "/tmp/log/"'
As an aside, you probably want a +
parameter of the date
command, like date +%s
or date +%Y%m%d
so that you get something that is suitable for a filename, making it something like this:
/bin/bash -c '/bin/tar -zcvf "/var/log/test/$(/bin/date +%s)_syslog_archive.tar.gz" "/tmp/log/"'
How to run multiple command systemd
You can start multiple background processes from one systemd unit, but systemd will not be able to track them for you and do all the nice things that it does to support a daemon, such as send signals to it on various system events or auto-restart it when needed.
If you must have it as a single unit, then you can do one of the following (in my order of preference):
make the two servers separate units (note you may be able to use the same config file for both, so they are two 'instances' of the same service - which makes sense, they run the same server). You will have two entries in the list of running services when you run 'systemctl'.
make that unit a one-shot (runs a program that exits and is not monitored and restarted). Make the one-shot command start both servers in background, e.g.,
sh -c " { pserve production.ini http_port=5000 & pserve production.ini http_port=5001 & } </dev/null >/dev/null >&1"
make a script that launches both daemons and watches for them, restarting them if needed and kills them when it is killed itself. Then you make that script the 'daemon' that systemd runs. Not really worth it, IMO - because you're doing much of the work that systemd itself is best suited to do. Of course you can spin a new copy of systemd that is configured to run just those two servers (and make that systemd as your 'one-service-for-two-commands' unit), but that seems an overkill.
Executing multiple Python scripts in a particular order using systemd
What you need are 2 different systemd unit and define the dependency using requires
[Unit]
Description=My script
[Service]
ExecStart=/usr/bin/python3 /home/pi/Desktop/myscript.py
Requires=dhangioserver.service
[Install]
WantedBy=multi-user.target
It will also be good to specify RequiredBy
in DjangoService unit
There is a related spec called Wants
which differs only in whether service should continue if dependendcy fails or not. Looking at your requirement , it looks like you need Requires
and not Wants
systemd : two services are running together
The main process (PID 1625) is most probably forking another process (PID 1627).
To check that the parent process of 1627 is 1625: ps -o ppid 1627
How to run multiple ExecStart in kafka.service for Multiple brokers in Kafka?
You cannot. You could use multiple systemd scripts that depend on each other or wrap all three in one command (for example, docker-compose -f kafka-cluster.yaml up
)
FWIW, in no way is this fault tolerant, or really a good idea unless you have at least 18G memory (giving 6G RAM each) and at least a factor of 3 available mechanical HDDs on that machine (three or more pools of JBOD for log.dirs
) and at least 3 separate CPU.
Note: ExecStop
is also only stopping one of the broker processes
Systemctl starting multiple commands
First, you need to fix [service]
to [Service]
. That is a syntax error. Second, you need to fix %i
to %f
. systemd escapes /
in instance name to -
. %f
can get unescaped instance name.
More detail.
- https://www.freedesktop.org/software/systemd/man/systemd.unit.html#String%20Escaping%20for%20Inclusion%20in%20Unit%20Names
- https://www.freedesktop.org/software/systemd/man/systemd.unit.html#Specifiers
Related Topics
Pipe Bash Command Output to Stdout and to a Variable
Linux: Modpost Does Not Build Anything
Searching a Process' Memory on Linux
Printing Variable to Command Line Using Assembly in Linux
Why Do My Results Different Following Along the Tiny Asm Example
How to Limit the Number of Results Returned from Grep
How to Purge Disk I/O Caches on Linux
Linux Shell to Restrict Sftp Users to Their Home Directories
Using Iconv to Convert from Utf-16Le to Utf-8
A Light Solution to Convert Text to PDF in Linux
What Does Pss Mean in /Proc/Pid/Smaps
Cmake Complains "The Cxx Compiler Identification Is Unknown"
After Changing /Etc/Profile, What Do I Have to Do to Reset My Shell