Index

Table of contents

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 console
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
[root] rc2.d/1
[root] /etc/profile
[root] /etc/rc.local
[root] /etc/init.d/rc.local
[root] rc2.d/99
console login
[user] /etc/bash.bashrc
[user] /etc/profile
[user] /home/[user]/.bash_profile
gui login
[user] /etc/profile
[user] /home/[user]/.profile
[user] ~/.config/autostart/autostart.desktop
open terminal in gui
[user] /etc/bash.bashrc
[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

linux path

viewing the current path
echo $PATH
echo $PATH | tr ':' '\n'
extending the path for this terminal session
PATH="$PATH:/tmp/bin"
store the line above in ~/.profile to make it permanent (requires restart)
echo 'PATH="$PATH:~/opt/bin"' >> ~/.profile
to use modified path this session without restarting
. ~/.profile

Power

reboot
shutdown -r now
sudo systemctl reboot
shutdown
shutdown -h now
sudo systemctl poweroff
suspend to RAM
sudo systemctl suspend
hibernate
sudo systemctl hibernate
suspend with hibernate as a backup (in case power runs out anyway)
hybrid-sleep

linux file hierarchy

https://i.stack.imgur.com/BlpRb.png
http://manpages.ubuntu.com/manpages/disco/en/man7/hier.7.html
https://www.tldp.org/LDP/Linux-Filesystem-Hierarchy/html/