Trouble with autostart with systemd.service

Hello all,

I am rally not well versed in Linux, I am trying to get a console app to auto start using systemd.service. This is the script I am using:

[Unit]
Description=Axcys Embedded Engine
#After=NetworkManager-wait-online.service network.target network-online.target dbus.service
#Wants=NetworkManager-wait-online.service network-online.target
#systemctl enable axcys-embedded-engine.service

[Service]
Type=simple
ExecStart=/home/pi/Public/axcys/AxcysFacilitySecurityManager/AxcysFacilitySecurityManager
Restart=always
#Restart = on-abort
 
[Install]
WantedBy=multi-user.target

It will not start, there are errors in the log that are:


Sep  4 17:37:19 AxcysEmbeddedController /home/pi/Public/axcys/AxcysFacilitySecurityManager/AxcysFacilitySecurityManager[6149]: Trying to daemonize the Axcys application.
Sep  4 17:37:19 AxcysEmbeddedController systemd[1]: axcys-embedded-engine.service holdoff time over, scheduling restart.
Sep  4 17:37:19 AxcysEmbeddedController systemd[1]: Stopping Axcys Embedded Engine...
Sep  4 17:37:19 AxcysEmbeddedController systemd[1]: Starting Axcys Embedded Engine...
Sep  4 17:37:19 AxcysEmbeddedController systemd[1]: axcys-embedded-engine.service start request repeated too quickly, refusing to start.
Sep  4 17:37:19 AxcysEmbeddedController systemd[1]: Failed to start Axcys Embedded Engine.
Sep  4 17:37:19 AxcysEmbeddedController systemd[1]: Unit axcys-embedded-engine.service entered failed state.

However, when I use an alternate method to auto start, it does work without errors:

#!/bin/sh

### BEGIN INIT INFO
# Provides:          AxcysFacilitySecurityManager
# Required-Start:    $local_fs $syslog $time $network
# Required-Stop:     $local_fs $syslog $time $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Axcys Facility Security Manager
# Description:       Axcys Facility Security Manager
### END INIT INFO

case $1 in
    start)
        start-stop-daemon --quiet --chuid pi --start --exec /home/pi/Public/axcys/AxcysFacilitySecurityManager/AxcysFacilitySecurityManager
        ;;
    stop)
        start-stop-daemon --quiet --chuid pi --stop --exec /home/pi/Public/axcys/AxcysFacilitySecurityManager/AxcysFacilitySecurityManager
        ;;
    restart|force-reload)
        $0 stop && $0 start
        ;;
    status)
        ps u -U pi
        ;;
    *)
        echo "Usage: sudo service AxcysFacilitySecurityManager {start|stop|restart|force-reload|status}"
        ;;
esac

I am told that the systemd.system is the best practices/preferred method and newest to linux. My goal is not only to autostart, but if the app fails for any reason, it is automatically restarted.

Can anyone suggest what I am doing wrong and how to fix?
Thanks,
Tim

Tim,

[Unit]
Description=Axcys Embedded Engine
After=network-online.target dbus.service

[Service]
Type=simple
ExecStart=/home/pi/Public/axcys/AxcysFacilitySecurityManager/AxcysFacilitySecurityManager
Restart=always
 
[Install]
WantedBy=multi-user.target

Will do the trick, if it stops for whatever reason it’s restarted (instantly) by systemd.
“Restart=always” have it restart always.

is this (AxcysFacilitySecurityManager) the “required main” application ? to be kept running? if so you may want to set it to Type=oneshot (then after that service create another service that has "After=
if it fails and restart it might fail again, and again and eventually systemd will NOT start it again.

This line:
axcys-embedded-engine.service start request repeated too quickly, refusing to start.
Says your app is crashing (with error code 0) too many times, it won’t restart it again.

You have a bug in your code causing a Crash or Quit(0) ?
In your App.UnhandledException output the error message trough:

System.Log(System.LogLevelCritical, "AxcysFacilitySecurityManager has crashed with ....<error>")
then in ssh or whatever type:

sudo journalctl -u <serviceNameHere>.service

put the output on the forum here.

Or try this one:

[Unit]
Description=Axcys Embedded Engine
After=network-online.target dbus.service
RestartSec=5

[Service]
Type=simple
ExecStart=/home/pi/Public/axcys/AxcysFacilitySecurityManager/AxcysFacilitySecurityManager
Restart=always
 
[Install]
WantedBy=multi-user.target

“RestartSec=5” says if it crashes, restart after 5 seconds.

don’t forget when having edited the .service file to reload the daemon using:

sudo systemctl daemon-reload

make sure your service is in enabled state:

sudo systemctl status axcys-embedded-engine.service 

and enable (to make a link and use it):

sudo systemctl enable axcys-embedded-engine.service 

and start manually:

sudo systemctl start axcys-embedded-engine.service

Hi Derk,
I will try with your script; however the interesting thing is that it does start, and run fine both manually and using the alternate method #!/bin/sh. It also restarts fine with that method too. However, I was told that using systemd was the best practices method…

Further, I tried systemd both with and without the app being Daemonized. No difference. Should it make a difference?
Tim

Your .service file needs to be owned by root.
Yur .service will have sudo/root rights as it’s started by systemd and you havn’t added a User=pi keyval.

You need to make sure you set the right target.
If it’s graphical (has a desktop) then you set WantedBy=graphical.target.
You cannot daemonize with Type =simple only with Type=forking
You best bet is having a master app managing subprocesses using shell with mode=1 or mode=2 you then your subprocesses daemonize.

The main app remains running, monitoring if one crashed, exited or whatever may happen.

Thanks Derk!
Those changes/suggestions worked perfectly.
Much appreciated!

Tim