Use supervisor to start program once on system boot
Ubuntu 22.04 disabled rc.local by default, although we can enable it again by run some commands, since it is deprecated, need to find other ways to run command on system boot.
Supervisor is an easy alternative way to start a long-running service on boot, but if you just want to run a command and quit, supervisor can do it too. For example, if you just want to run a script and quit on system boot, like echo the date, you can do something ike this:
1 | [program:echodate] |
There are several key configs here:
- using
sh -cto run date andsleep. We need to putsleephere, otherwise, if the process quit too quickly, supervisor will be failed to get the running pid, and thought it failed to run, then it will keep trying to run the command several times, and at last still thought it failed to run the command. - set
autostarttotrue. This will run the command on system boot/reboot. - set
autorestarttofalse. Since we only want to run the command once on system boot/reboot, after it quits, we don’t want supervisor to auto restart the process again, we need to setautorestarttofalse.