===== Creating a systemd service ===== * create the config (?) for the service as ''/lib/systemd/system/.service'' * is it actually supposed to go in ''/etc/systemd/system'' not ''/lib/systemd/system''? * Inside the config: * enable the service: ''sudo systemctl enable application.service'' ==== Make the service restart always ==== ''Restart Always'' in service has limits. Kill 'em limits: * in ''[unit]'' add ''StartLimitIntervalSec=0'' so systemd will continue to try to restart the service forever * in ''[Service]'' add ''RestartSec=1'' to have systemd retry every second (instead of the default every 100ms) ==== Make a service start after a USB connection ==== * plug in the usb device look up the vender id and product id of the USB device. ''lsusb'' or ''usbreset'' will both give you this information * create a file in ''/etc/udev/rules.d'' for example ''/etc/udev/rules.d/80-local.rules'' and put in it something like this example: ACTION=="add", ATTR{idVendor}=="0582", ATTR{idProduct}=="01d6", TAG+="systemd", ENV{SYSTEMD_WANTS}="jackd.service" //[[https://blog.nostatic.org/2020/08/starting-jack-automatically-when-usb.html|source]]// ==== Make a service start after USB device initializes on bootup ==== //Example// * create ''/etc/udev/rules.d/20-usb-bus.rules'' with content: ''KERNEL=="usb[1-2]", TAG+="systemd"'' * reboot or udev rules reload and now voila: ''systemctl status dev-bus-usb-001-001.device'' * then in ''[Unit]'' you can do something like ''After=dev-bus-usb-001-001.device dev-bus-usb-002-001.device'' //[[https://superuser.com/questions/1322777/in-systemd-service-file-how-do-i-say-after-usb-is-ready|source]]// ===== References ===== * https:%%//%%www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-systemd-services-and-units * https:%%//%%wiki.archlinux.org/index.php/systemd#Handling_dependencies * [[https://wiki.ubuntu.com/SystemdForUpstartUsers#Behavioral_differences_.28aka_Gotchas.29|systemd for upstart users (16.04)]] * [[https://askubuntu.com/questions/795226/how-to-list-all-enabled-services-from-systemctl|getting lists of systemd services (enabled, running, etc.)]] * [[https://askubuntu.com/questions/815091/how-do-i-start-mosquitto-mqtt-broker-on-startup-in-ubuntu-16-04|how systemd runs an upstart service (example with Mosquitto)]] * [[https://unix.stackexchange.com/questions/219940/getting-a-service-started-automatically-when-another-gets-started|automatically start one service after another is started]]