1

I have an application (OS is Ubuntu 16.04) that basically open the camera do some stuff with the video stream. It's managed by a systemd service:

[Unit]
Description=My Application
After=network.target
StartLimitInterval=0

[Service]
Type=simple
Restart=always
RestartSec=5
User=myuser
WorkingDirectory=/opt/foo/
ExecStart=/opt/foo/myapplication

[Install]
WantedBy=multi-user.target

Then I have a desktop entry that stop the service and re-run my application with an argument:

[Desktop Entry]
Name=Myapp
Type=Application
Exec=/opt/foo/launcher.sh
Terminal=true
Path=/opt/foo/
Categories=None;

This is my launcher:

#!/bin/bash
service myservice stop
/opt/foo/myapplication --debug

This is working, anyway I canno find a way to restart the service when I close the application launched by the desktop entry. I tried to add a service restart at the end of the launcher but it doesn't work, because when I close the terminal in which the application run, everything I presume is killed. How can I do it?

0

You can write a unit file in, say, /etc/systemd/system/myprocess.service:

[Unit]
Description=My Process

[Service]
ExecStart=/bin/myprocess
Restart=always

[Install]
WantedBy=multi-user.target

And then in the desktop entry create a launcher to do

systemctl start myprocess.service

But remember not to enable that service in startup so can you start it manually from you executable entry.
EDIT:
If you also need to run in debug mode than create another service unit with the --debug parameter. You can create another desktop entry to point a script that look like(just representative idea)

systemctl stop myprocess.service // to stop previous service
systemctl start processwithdebug.servce // to debug
| improve this answer | |
  • my application must run continuously, I need to sometimes to stop the service, open in debug mode and automatically restart the service when I close the application.. – rok Jul 3 at 8:06
  • Does edited answer helped? – Sudip Ghimire Jul 3 at 8:14
  • what happen when I close my application in debug mode ? who is gonna restart the normal service in this way...? – rok Jul 3 at 13:16
  • to close you can again make the script which restart the previous service soon after the current one is closed – Sudip Ghimire Jul 3 at 14:14
0

You can have systemd do some of the work for you with a second unit B by saying that it Conflicts with the first unit A; then when you start B, it will stop A. Sadly, when B stops, A is not restarted by default, but you can add a OnFailure line; if B exits with a failure, A will be started. To ensure failure in case of normal exit of B you can use an ExecStop that sets the return code to non-zero.

A.service:

[Service]
Type=simple
ExecStart=/opt/foo/myapplication

B.service:

[Unit]
Conflicts=A.service
OnFailure=A.service
[Service]
Type=simple
ExecStart=/opt/foo/myapplication --debug
ExecStop=/usr/bin/false
| improve this answer | |
  • I need to see also the console output when I run in Debug mode.. in this way I guess I'm not able to automatically open a terminal with the output from my application.. – rok Jul 6 at 7:30
0

You could use a lock file to ensure that only one instance of your application runs. Instead of systemctl stop myservice in your launcher, run systemctl restart myservice, and in the 5 seconds it needs to restart the application, start your debug version after acquiriing a lock file:

systemctl restart myservice
flock /opt/foo/mylock /opt/foo/myapplication --debug

and change the unit file to use the same lock:

ExecStart=/usr/bin/flock /opt/foo/mylock /opt/foo/myapplication

It will restart after 5 seconds then wait to acquire the lock, and hold it whilst running the application. The lock file can be anywhere. You can actually use /opt/foo/myapplication as the lock file, but if you are recompiling it or something you will lose the lock when it changes.

| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.