Index

services

Currently there are actually three different ways for software to be started as a service in Ubuntu: SysV, Upstart and systemd.

sysv

The traditional way to start services in Linux was to place a script in /etc/init.d, and then use the update-rc.d command to enable or disable it.One major issue with SysV was that when booting the system, everything had to be done in serial, making system boot times really slow.This was the main reason that Upstart was created.

upstart (initctl)

Upstart was designed to be backwards compatible with sysv.Upstart uses job definition files in /etc/init to define on what events a service should be started.So, while the system is booting, upstart processes various events, and then can start multiple services in parallel.

systemd (systemctl)

Starting with Ubuntu 15.04, Upstart will be deprecated in favor of Systemd

running code at startup

sysv / upstart

On a sysv / upstart based system anything linked to from /etc/rc2.d will run (as root).
sudo vi /etc/rc2.d/S01autorun
sudo vi /etc/rc2.d/S99vautorun
The proper way would be to add a line to rc.local
sudo vi /etc/rc.local
End with an ampersand to background the process and allows the rc.local to continue executing

systemd

sudo vi /etc/systemd/system/[name].service

crontab

create a crontab entry with crontab expression @reboot
sudo vi /etc/cron.d/[name]

bash

/etc/profile is system wide, will run on boot for root, for user on any login
sudo vi /etc/profile
/etc/bash.bashrc is system wide, will run when logging in to console or opening a terminal
sudo vi /etc/bash.bashrc
~/.bash_profile is run when logging in using the conole
vi ~/.bash_profile
~/.profile is run on a gui login
vi ~/.profile
~/.bashrc is run when a (non login) terminal is opened in the gui
vi ~/.bashrc

window manager

lastly the window manager (gnome, kde, etc.) will likely have its own config files

for xfce: when you configure startup scripts via the UI, then a file will be created in

~/.config/autostart
can of course create such a file manually
sudo vi ~/.config/autostart/[name].desktop
example
[Desktop Entry]
Encoding=UTF-8
Version=0.9.4
Type=Application
Name=autostart
Comment=autostart
Exec=/data/startlog ~/.config/autostart/autostart.desktop
OnlyShowIn=XFCE;
StartupNotify=false
Terminal=false
Hidden=false

mint 17.3 test to see which of these options is executed when

boot only
1529255041.066567422 0 [root] rc2.d/1
1529255041.141205223 0 [root] /etc/profile
1529255044.191935107 0 [root] /etc/rc.local
1529255044.196742127 0 [root] /etc/init.d/rc.local
1529255044.201023892 0 [root] rc2.d/99
console login
1529255084.079145027 1000 [user] /etc/bash.bashrc
1529255084.122304976 1000 [user] /etc/profile
1529255084.129987458 1000 [user] /home/[user]/.bash_profile
gui login
1529255119.076380681 1000 [user] /etc/profile
1529255119.091203871 1000 [user] /home/[user]/.profile
1529255120.036253493 1000 [user] ~/.config/autostart/autostart.desktop
open terminal in gui
1529255172.997879773 1000 [user] /etc/bash.bashrc
1529255172.998091990 1000 [user] /home/[user]/.bashrc

sysv

This command uses link naming [sk][0-9]{2}[script] to determine if a service should be started or killed in a run mode and in which order.

creating a new service

sudo vi /etc/init.d/<service name>
sudo chmod 700 /etc/init.d/<service name>
sudo update-rc.d <service name> defaults
sudo update-rc.d <service name> enable

upstart

show services
initctl list
service --status-all
create a new script file in /etc/init.d
#!/bin/sh
 # Default-Start:     2 3 4 5
 # Default-Stop:      0 1 6

DESC="test script"
NAME="test one"

start() {
   echo "starting!";
}

stop() {
   echo "stopping!"
}

case "$1" in
   start)
     start
     ;;
   stop)
     stop
     ;;
   restart)
      stop
      start
      ;;
  *)
    echo "usage: \n service @identifier@ (start|stop|restart)"
esac

exit 0
apply changes
initctl reload-configuration

further reading

http://upstart.ubuntu.com/cookbook/
http://manpages.ubuntu.com/manpages/precise/man5/init.8.html
http://manpages.ubuntu.com/manpages/precise/man5/init.5.html

systemd

To run a (short-lived)1 command at startup using systemd, you can use a systemd unit of type OneShot.
sudo vi /etc/systemd/system/[name].service

[Unit]
Description=Job that runs your user script

[Service]
ExecStart=/some/command
Type=oneshot
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
apply changes
sudo systemctl daemon-reload
sudo systemctl enable foo.service
You can run multiple commands from the same service file
[Service]
ExecStart=/some/command
ExecStart=/another/command some args
ExecStart=-/a/third/command ignore failure
WantedBy used here, for example, makes it start when the multi-user.target is reached. You can use Before, After, Requires, etc.

further reading

https://wiki.ubuntu.com/SystemdForUpstartUsers
http://manpages.ubuntu.com/manpages/zesty/en/man5/systemd.unit.5.html
https://www.freedesktop.org/software/systemd/man/systemd.exec.html
https://www.freedesktop.org/software/systemd/man/systemd.service.html
https://www.freedesktop.org/software/systemd/man/systemd.unit.html
https://www.freedesktop.org/software/systemd/man/systemctl.html
https://www.freedesktop.org/software/systemd/man/journalctl.html