I found a thread here from 2016 that talks about starting your app upon linux boot, but the script is limited:
#!/bin/bash
cd /path/to/your/app/
./yourApp
While this might start my service application, is does nothing for a controlled stop or shutdown. So I found this generic script and added the above code for start:
[code]case “$1” in
start)
echo "Starting Service "
cd /path/to/your/app/
./yourApp
;;
stop)
echo "Stopping Service "
?? ?? ?? ?? ??
;;
*)
echo “Usage: /etc/init.d/test {start|stop}”
exit 1
;;
esac
I prefer using systemd to manage this. Super simple to systemctl [start|stop|restart] MyWebApp, and start at boot is as easy as systemctl enable MyWebApp. Relaunches after crashes too.
Depending on your version of Linux, you may be able to take advantage of systemd to define your services. Systemd has options to restart on failure and mechanisms for starting on boot. On Ubuntu Server 16.04 and 18.04, that is the approach that we use.
In all honesty, I was not a fan of systemd when it first started gaining traction, but I will give credit where credit is due. For configuration of Xojo programs that need to run as services, it has been pretty stable and straightforward.
I just want to add one more thing for others who may come along:
Your application must have a “DontDaemonize” event and it must return TRUE.
I assume this is because the OS is doing the daemonizing for you. Before I added this, the app would start and immediately stop (without even running the RUN event). The “restart” option in my “service” file caused it to do this multiple times in rapid succession until ‘start-limit-hit’.
That is correct. Systemd is designed to allow ANY binary or script to run as a daemon/service. If you’re app is already starting up in daemon mode, systemctl gets confused since it received back a prompt instead of attaching to the app’s stdin/stdout/stderr.