I have created a service on an Ubuntu 16.04 in order to run a c++ app after every boot.The c++ app runs among others ping measurements ,and that's why I need to start it after the network is up and to have cap_net_raw=ep. The service unit file I created and stored it under /etc/systemd/system/ is the following (app.service):
[Unit]
Wants=network-online.target
After=network-online.target
[Service]
Type=simple
Restart=always
ExecStart=/home/app/script.sh
[Install]
WantedBy=default.targetThe script.sh contains the following:
#!/bin/bash
sudo setcap cap_net_raw=ep /home/app/C++_APP
cd /home/app
gnome-terminal -e "./C++_APP"I have also given the following commands:
chmod +x /home/app/script.sh chmod 664 /etc/systemd/system/app.service systemctl daemon-reload systemctl enable app.serviceHowever when I reboot the os the c++ app is not run and when I give:
systemctl status app.serviceI get the following message :
app.service Loaded: loaded (/etc/systemd/system/app.service; enabled; vendor preset: enabled) Active: inactive (dead) (Result: exit-code) since Mon 2017-04-03 16:14:21 EEST; 2min 24s ago Process: 2937 ExecStart=/home/user/script.sh (code=exited, status=1/FAILURE)When I run the script manually it works fine. Has anyone any idea of what I have done wrong?
32 Answers
systemd is not the right tool to run desktop apps like gnome-terminal in the foreground. Your systemd config will be running the app as root, which I presume you intend. It will be unaware an unable to run an app in a particular user's foreground session.
Update your script to not use gnome-terminal and just make the app executable and run it directly.
To debug, you can have it log to STDOUT or STDERR and use journalctl -u app.service to review the logs. Reviewing nearby lines in the full journalctl output may also be helpful.
Also see this related post about running apps via the CLI vs with systemd:
2add this line to your service file:
[Service]
User=rootThis worked for me!