2

I have two programs that I called program1 and program2 which perform a main task of two operations. When program1 start running it pipe its results to program2 which performs the final result. Because both programs must run continuously, I implemented a systemd service for this and here is the content of /lib/systemd/system/servers.service file.

[Unit]
Description=Start servers
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/servers >> /var/log/servers.log
Restart=always
TimeoutStartSec=100
User=root

ExecStartPre=killall -w -q program1 && killall -w -q program2
User=root

ExecStopPost=killall -w -q program1 && killall -w -q program2
User=root

[Install]
WantedBy=multi-user.target

When run the service, systemd create two process to realize the task, here you can see the status.

sudo service servers status
# result
● servers.service - Start servers
   Loaded: loaded (/lib/systemd/system/servers.service; enabled)
   Active: active (running) since Wed 2016-06-29 21:00:27 UTC; 2h 5min ago
 Main PID: 11942 (server)
   CGroup: /system.slice/servers.service
           ├─11942 program1
           └─11944 program2

when one of the program crash the service is still in it running state which is not logically correct because the main task is stopped.

Please I will like to know if it is possible to implement such a situation with systemd services? If so, how?

  • if the programs are connected by a pipe, then when one end of the pipe closes, further reads or writes on the other end will get eof or an errors. Isnt this what you observe? – meuh Jun 30 '16 at 18:18
  • Thank @meuh, In fact both programs work as servers. The first program (program1) just knows he writing to a file and the file here is our stdout. And the second one is reading to a file and the here is stdin. When the pipe broke the don't stop, I think they know about the availability of resources. – Erman Jul 1 '16 at 4:11
  • Another thing I noticed is that, When the first program crash, the service restart automatically. Now I want to work on cron jobs to check if the second process still alive. – Erman Jul 1 '16 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.