Skip to content

Instantly share code, notes, and snippets.

@kneerunjun
Last active September 2, 2017 05:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kneerunjun/547545211abf4c2ff55c793a7c0433ae to your computer and use it in GitHub Desktop.
Save kneerunjun/547545211abf4c2ff55c793a7c0433ae to your computer and use it in GitHub Desktop.

What is the need of getting such upstarted services on linux ?


I have tested a code working fine on my Raspbery. While much of effort has gone into hardware debugging and getting the ports to work as expected, It still is a challenge to run this program evrytime I have to showcase things to people.Fo each instance I have to connect the hardware along with the peripherals and then carefull issue commands to start the desired program

  1. Logistics of the peripherals is cumbersome
  2. Peripherals sdo consume more amperage

Would it not be good if there was a way th program upstarts with the machine itself ?

Or one up - wait for me in suspension to get started and sensing.

Systemctl to the rescue


Here are 5 ways to get your program upstarted on RPi

What I though prefer is making systemctl units and then making them functional for upstart. Create new systemctl units

It’s obvious that your computer is useful due to the services it offers and in order to have this functionality, many services have to be called as the computer boots and reaches different levels.Other services are called to be executed when the computer reaches for example the rescue level (runlevel 0) and others when it reaches the multi-user level (runlevel 3). You can imagine these levels as targets.

If you want to have a look at all the service units that are running @ graphical.target

# systemctl --type=service
UNIT                               LOAD   ACTIVE SUB     DESCRIPTION
autologin@tty1.service             loaded active running Getty on tty1
avahi-daemon.service               loaded active running Avahi mDNS/DNS-SD Stack
bluetooth.service                  loaded active running Bluetooth service
console-setup.service              loaded active exited  LSB: Set console font and keymap
cpufrequtils.service               loaded active exited  LSB: set CPUFreq kernel parameters
cron.service                       loaded active running Regular background program processing daemon
dbus.service                       loaded active running D-Bus System Message Bus
dhcpcd.service                     loaded active running dhcpcd on all interfaces
dphys-swapfile.service             loaded active exited  LSB: Autogenerate and use a swap file
fake-hwclock.service               loaded active exited  Restore / save the current clock
hciuart.service                    loaded active running Configure Bluetooth Modems connected by UART
hdparm.service                     loaded active exited  LSB: Tune IDE hard disks
ifup@wlan0.service                 loaded active exited  ifup for wlan0
kbd.service                        loaded active exited  LSB: Prepare console
keyboard-setup.service             loaded active exited  LSB: Set preliminary keymap
kmod-static-nodes.service          loaded active exited  Create list of required static device nodes for the current kernel
lightdm.service                    loaded active running Light Display Manager
loadcpufreq.service                loaded active exited  LSB: Load kernel modules needed to enable cpufreq scaling
mongodb.service                    loaded active running An object/document-oriented database
networking.service                 loaded active running LSB: Raise network interfaces.

As you can notice here there are some services that are running, active, and there are some some which have exited / terminated. If you want to check the status of a service here is the command you need to type in

systemctl status firewalld.service
bluetooth.service - Bluetooth service
   Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled)
   Active: active (running) since Thu 2017-08-31 16:13:44 UTC; 1 day 12h ago
     Docs: man:bluetoothd(8)
 Main PID: 936 (bluetoothd)
   Status: "Running"
   CGroup: /system.slice/bluetooth.service
           └─936 /usr/lib/bluetooth/bluetoothd

Here are some commands that can help you get the systemctl units started and stopped

systemctl start servicename.service
systemctl stop servicename.service

Stopping the service would implicate that it stops the service for this session , nothing stops it from upstarting on reboot.

To see what all services are enabled for your machine, here are the commands you can fire fromt he command line. You can clearly see some

  1. file links
  2. .wants directories

ex: All the services that need to be loaded when the boot reaches the level multi-user.target are all available under the directory multi-user.target.wants. remember .target is the run level and the levels shows the maturity in the boot process. To be more simple , how far in the boot process are we is what is .target

cd /etc/systemd/system
ls -l
-rw-r--r-- 1 root root 1551 Sep 18  2015 autologin@.service
drwxr-xr-x 2 root root 4096 May 27  2016 bluetooth.target.wants
lrwxrwxrwx 1 root root   37 May 27  2016 dbus-org.bluez.service -> /lib/systemd/system/bluetooth.service
lrwxrwxrwx 1 root root   40 May 27  2016 dbus-org.freedesktop.Avahi.service -> /lib/systemd/system/avahi-daemon.service
lrwxrwxrwx 1 root root   34 May 27  2016 dhcpcd5 -> /lib/systemd/system/dhcpcd.service
drwxr-xr-x 2 root root 4096 May 27  2016 dhcpcd.service.d
lrwxrwxrwx 1 root root   35 Jun 12 04:44 display-manager.service -> /lib/systemd/system/lightdm.service
drwxr-xr-x 2 root root 4096 May 27  2016 getty.target.wants
drwxr-xr-x 2 root root 4096 May 27  2016 getty@tty1.service.d
drwxr-xr-x 2 root root 4096 May 27  2016 halt.target.wants
drwxr-xr-x 2 root root 4096 Jun 12 04:56 multi-user.target.wants
drwxr-xr-x 2 root root 4096 May 27  2016 poweroff.target.wants
drwxr-xr-x 2 root root 4096 May 27  2016 rc-local.service.d
drwxr-xr-x 2 root root 4096 May 27  2016 reboot.target.wants
drwxr-xr-x 2 root root 4096 May 27  2016 sockets.target.wants
lrwxrwxrwx 1 root root   31 May 27  2016 sshd.service -> /lib/systemd/system/ssh.service
drwxr-xr-x 2 root root 4096 May 27  2016 sysinit.target.wants
lrwxrwxrwx 1 root root   35 May 27  2016 syslog.service -> /lib/systemd/system/rsyslog.service

ls  multi-user.target.wants

avahi-daemon.service  dhcpcd.service   mongodb.service   rsyslog.service  sshswitch.service
cron.service          hciuart.service  remote-fs.target  ssh.service

You shall notice it not only contains services but also other targets !

Here is how you can go ahead to make your own service , - an upstarting program

[Unit]
Description = making network connection up
After = network.target
[Service]
ExecStart = /root/scripts/conup.sh
[Install]
WantedBy = multi-user.target

Being in the folder /etc/systemd/system you would have to enable the service as a systemctl unit

systemctl enable servicename.sevice
systemctl start servicename.service

On this note you can expect this service to also start when the machine restarts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment