Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run multiple command systemd

Tags:

systemd

i want to multiple command in myapp.service file in systemd

[Unit]
Description=to serve myapp

[Service]
User=ubuntu
WorkingDirectory=/home/ubuntu/myapp
ExecStart=/home/ubuntu/.local/bin/pserve production.ini http_port=5000
ExecStart=/home/ubuntu/.local/bin/pserve production.ini http_port=5001
Restart=always

[Install]
WantedBy=multi-user.target

it throws error saying invalid argument. i want to run two commands

pserve production.ini http_port=5000
pserve production.ini http_port=5001

How do i do that??

like image 604
which_part Avatar asked Oct 30 '25 13:10

which_part


1 Answers

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.

like image 51
Leo K Avatar answered Nov 01 '25 04:11

Leo K



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!