Difference between revisions of "EBC Exercise 21b systemd"

From eLinux.org
Jump to: navigation, search
m (Administration)
(systemd: Removed)
Line 109: Line 109:
== Running your own service ==
== Running your own service ==
== systemd ==
beagle$ systemctl
UNIT                      LOAD  ACTIVE SUB      JOB DESCRIPTION
proc-sys...misc.automount loaded active running      Arbitrary Executable File Formats File System Automount Point
sys-devi...et-eth0.device loaded active plugged      /sys/devices/platform/cpsw.0/net/eth0
sys-devi...et-usb0.device loaded active plugged      /sys/devices/platform/omap/musb-ti81xx/musb-hdrc.0/gadget/net/usb0
sys-devi...cblk0p1.device loaded active plugged      /sys/devices/platform/omap/omap_hsmmc.0/mmc_host/mmc0/mmc0:1234/block/mmcblk0/mmcblk0p
sys-devi...cblk0p2.device loaded active plugged      /sys/devices/platform/omap/omap_hsmmc.0/mmc_host/mmc0/mmc0:1234/block/mmcblk0/mmcblk0p
sys-devi...mmcblk0.device loaded active plugged      /sys/devices/platform/omap/omap_hsmmc.0/mmc_host/mmc0/mmc0:1234/block/mmcblk0
sys-devi...y-ttyO0.device loaded active plugged      /sys/devices/platform/omap/omap_uart.0/tty/ttyO0
sys-devi...y-ttyO1.device loaded active plugged      /sys/devices/platform/omap/omap_uart.1/tty/ttyO1
sys-devi...y-ttyO2.device loaded active plugged      /sys/devices/platform/omap/omap_uart.2/tty/ttyO2
sys-devi...y-ttyO3.device loaded active plugged      /sys/devices/platform/omap/omap_uart.3/tty/ttyO3
sys-devi...y-ttyO4.device loaded active plugged      /sys/devices/platform/omap/omap_uart.4/tty/ttyO4
sys-devi...y-ttyO5.device loaded active plugged      /sys/devices/platform/omap/omap_uart.5/tty/ttyO5
sys-devi...y-ttyS0.device loaded active plugged      /sys/devices/platform/serial8250/tty/ttyS0
sys-devi...y-ttyS1.device loaded active plugged      /sys/devices/platform/serial8250/tty/ttyS1
sys-devi...y-ttyS2.device loaded active plugged      /sys/devices/platform/serial8250/tty/ttyS2
sys-devi...y-ttyS3.device loaded active plugged      /sys/devices/platform/serial8250/tty/ttyS3
sys-devi...et-sit0.device loaded active plugged      /sys/devices/virtual/net/sit0
sys-devi...ty-tty0.device loaded active plugged      /sys/devices/virtual/tty/tty0
sys-devi...ty-tty1.device loaded active plugged      /sys/devices/virtual/tty/tty1
sys-devi...y-tty10.device loaded active plugged      /sys/devices/virtual/tty/tty10
sys-devi...y-tty11.device loaded active plugged      /sys/devices/virtual/tty/tty11
sys-devi...y-tty12.device loaded active plugged      /sys/devices/virtual/tty/tty12
sys-devi...ty-tty2.device loaded active plugged      /sys/devices/virtual/tty/tty2
sys-devi...ty-tty3.device loaded active plugged      /sys/devices/virtual/tty/tty3
sys-devi...ty-tty4.device loaded active plugged      /sys/devices/virtual/tty/tty4
sys-devi...ty-tty5.device loaded active plugged      /sys/devices/virtual/tty/tty5
sys-devi...ty-tty6.device loaded active plugged      /sys/devices/virtual/tty/tty6
sys-devi...ty-tty7.device loaded active plugged      /sys/devices/virtual/tty/tty7
sys-devi...ty-tty8.device loaded active plugged      /sys/devices/virtual/tty/tty8
sys-devi...ty-tty9.device loaded active plugged      /sys/devices/virtual/tty/tty9
-.mount                  loaded active mounted      /
dev-mqueue.mount          loaded active mounted      POSIX Message Queue File System
etc-machine\x2did.mount  loaded active mounted      /etc/machine-id
proc-sys...fmt_misc.mount loaded active mounted      Arbitrary Executable File Formats File System
sys-kernel-debug.mount    loaded active mounted      Debug File System
tmp.mount                loaded active mounted      Temporary Directory
systemd-...d-console.path loaded active waiting      Dispatch Password Requests to Console Directory Watch
systemd-...word-wall.path loaded active waiting      Forward Password Requests to Wall Directory Watch
avahi-daemon.service      loaded active running      Avahi mDNS/DNS-SD Stack
bone101.service          loaded active running      Beaglebone 101 presentation
cape.service              loaded active exited        Beaglebone cape support
cloud9.service            loaded active running      Cloud9 IDE
connman.service          loaded active running      Connection service
console-...-start.service loaded active exited        Console System Startup Logging
crond.service            loaded active running      Periodic Command Scheduler
dbus.service              loaded active running      D-Bus System Message Bus
dropbear...:59238.service loaded active running      SSH Per-Connection Server
gateone.service          loaded active running      GateOne daemon
gdm.service              loaded active running      Gnome Display Manager
getty@tty1.service        loaded active running      Getty on tty1
leds.service              loaded active exited        Angstrom LED config
network-...t-init.service loaded active exited        Start USB Ethernet gadget
pvr-init.service          loaded active exited        pvr-init.service
remount-rootfs.service    loaded active exited        Remount Root FS
serial-g...@ttyO0.service loaded active running      Serial Getty on ttyO0
systemd-journald.service  loaded active running      Journal Service
systemd-logind.service    loaded active running      Login Service
systemd-...s-load.service loaded active exited        Load Kernel Modules
systemd-...pi-vfs.service loaded active exited        Remount API VFS
systemd-sysctl.service    loaded active exited        Apply Kernel Variables
systemd-...-setup.service loaded active exited        Recreate Volatile Files and Directories
systemd-...ssions.service loaded active exited        Permit User Sessions
timestamp.service        loaded active exited        Timestamping service
udev-trigger.service      loaded active exited        udev Coldplug all Devices
udev.service              loaded active running      udev Kernel Device Manager
udhcpd.service            loaded active running      DHCP server for USB0 network gadget
xinetd.service            loaded active exited        xinetd.service
avahi-daemon.socket      loaded active listening    Avahi mDNS/DNS-SD Stack Activation Socket
dbus.socket              loaded active running      D-Bus System Message Bus Socket
dropbear.socket          loaded active listening    dropbear.socket
systemd-initctl.socket    loaded active listening    /dev/initctl Compatibility Named Pipe
systemd-journald.socket  loaded active running      Journal Socket
systemd-shutdownd.socket  loaded active listening    Delayed Shutdown Socket
udev-control.socket      loaded active listening    udev Control Socket
udev-kernel.socket        loaded active running      udev Kernel Socket
basic.target              loaded active active        Basic System
getty.target              loaded active active        Login Prompts
graphical.target          loaded active active        Graphical Interface
local-fs-pre.target      loaded active active        Local File Systems (Pre)
local-fs.target          loaded active active        Local File Systems
multi-user.target        loaded active active        Multi-User
remote-fs.target          loaded active active        Remote File Systems
sockets.target            loaded active active        Sockets
swap.target              loaded active active        Swap
sysinit.target            loaded active active        System Initialization
systemd-...es-clean.timer loaded active waiting      Daily Cleanup of Temporary Directories
LOAD  = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.
JOB    = Pending job for the unit.
86 units listed. Pass --all to see inactive units, too.

Revision as of 09:07, 10 October 2013

thumb‎ Embedded Linux Class by Mark A. Yoder

Traditionally user space initialization has been done using init.d, however recently many distributions have been switching to systemd

systemd is a system and service manager for Linux, compatible with SysV and LSB init scripts. systemd provides aggressive parallelization capabilities, uses socket and D-Bus activation for starting services, offers on-demand starting of daemons, keeps track of processes using Linux control groups, supports snapshotting and restoring of the system state, maintains mount and automount points and implements an elaborate transactional dependency-based service control logic.

Here we'll see how to use systemd as an administration and how to create a simple service.


There are a few simple commands that show what's running under systemd and how to stop and start them. The examples here were inspired by the twenty part series on systemd administration and the three part intro by Carla Schroder

To see what's running, run

beagle$ systemctl
UNIT                        LOAD   ACTIVE SUB       DESCRIPTION
proc-sys...t_misc.automount loaded active waiting   Arbitrary Executable File Fo
sys-devi...tty-ttyO0.device loaded active plugged   /sys/devices/ocp.2/44e09000.
sys-devi...ty-ttyGS0.device loaded active plugged   /sys/devices/ocp.2/47400000.
sys-devi...-net-eth0.device loaded active plugged   /sys/devices/ocp.2/4a100000.
sys-devi...blk0boot0.device loaded active plugged   /sys/devices/ocp.2/mmc.10/mm
sys-devi...blk0boot1.device loaded active plugged   /sys/devices/ocp.2/mmc.10/mm
sys-devi...mmcblk0p1.device loaded active plugged   /sys/devices/ocp.2/mmc.10/mm
sys-devi...mmcblk0p2.device loaded active plugged   /sys/devices/ocp.2/mmc.10/mm
sys-devi...k-mmcblk0.device loaded active plugged   /sys/devices/ocp.2/mmc.10/mm
sys-devi...tty-ttyS0.device loaded active plugged   /sys/devices/platform/serial
sys-devi...tty-ttyS1.device loaded active plugged   /sys/devices/platform/serial
sys-devi...tty-ttyS2.device loaded active plugged   /sys/devices/platform/serial
sys-devi...tty-ttyS3.device loaded active plugged   /sys/devices/platform/serial
sys-module-fuse.device      loaded active plugged   /sys/module/fuse
sys-subs...ices-eth0.device loaded active plugged   /sys/subsystem/net/devices/e
-.mount                     loaded active mounted   /
dev-mqueue.mount            loaded active mounted   POSIX Message Queue File Sys
sys-fs-f...onnections.mount loaded active mounted   FUSE Control File System
sys-kernel-debug.mount      loaded active mounted   Debug File System
tmp.mount                   loaded active mounted   /tmp
systemd-...ord-console.path loaded active waiting   Dispatch Password Requests t
systemd-...ssword-wall.path loaded active waiting   Forward Password Requests to
avahi-daemon.service        loaded active running   Avahi mDNS/DNS-SD Stack
bonescript-autorun.service  loaded active running   Bonescript autorun
bonescript.service          loaded active running   Bonescript server

and so on. Look through the list and see what you recognize. If you make your window bigger you will see more information. Here's how to find more details about a given process.

beagle$ systemctl status bonescript.service
bonescript.service - Bonescript server
         Loaded: loaded (/lib/systemd/system/bonescript.service; static)
         Active: active (running) since Tue 2013-10-08 15:30:10 EDT; 1 day 20h ago
       Main PID: 346 (node)
         CGroup: name=systemd:/system/bonescript.service
       	  `-346 /usr/bin/node server.js

Oct 08 15:30:10 yoder-black-bone systemd[1]: Started Bonescript server.
Oct 08 15:30:16 yoder-black-bone bonescript[346]: [35B blob data]
Oct 08 15:30:16 yoder-black-bone bonescript[346]: - - - [Tue, 08 Oct 2013 19:...
Oct 08 15:30:17 yoder-black-bone bonescript[346]: - - - [Tue, 08 Oct 2013 19:...
Oct 08 15:30:17 yoder-black-bone bonescript[346]: - - - [Tue, 08 Oct 2013 19:...

Stopping and Starting

You can stop a process with

beagle$ systemctl | grep cloud
cloud9.service              loaded active running   Cloud9 IDE
beagle$ systemctl status cloud9
cloud9.service - Cloud9 IDE
         Loaded: loaded (/lib/systemd/system/cloud9.service; enabled)
         Active: active (running) since Fri 2000-01-07 17:46:06 EST; 13 years 9 months ago
       Main PID: 130 (node4)
         CGroup: name=systemd:/system/cloud9.service
       	  `-130 /usr/bin/node4 /usr/share/cloud9/bin/cloud9.js -l 0.0...

Oct 08 15:30:15 yoder-black-bone node4[130]: publish state{"type":"state","p...}
Oct 10 10:51:10 yoder-black-bone node4[130]: uncaught exception:
Oct 10 10:51:10 yoder-black-bone node4[130]: Error: ENOENT, No such file or ...'
Oct 10 10:51:10 yoder-black-bone node4[130]: at Object.statSync (fs.js:400:18)
Oct 10 10:51:10 yoder-black-bone node4[130]: at /usr/share/cloud9/server/clo...7
Oct 10 10:51:10 yoder-black-bone node4[130]: at Array.forEach (native)
Oct 10 10:51:10 yoder-black-bone node4[130]: at StatWatcher.<anonymous> (/us...)
beagle$ systemctl stop cloud9

Now try accessing cloud9 from a web browser ( It isn't there. You can start it with

beagle$ systemctl start cloud9
beagle$ systemctl status cloud9
cloud9.service - Cloud9 IDE
         Loaded: loaded (/lib/systemd/system/cloud9.service; enabled)
         Active: active (running) since Thu 2013-10-10 11:53:25 EDT; 14s ago
       Main PID: 1470 (node4)
         CGroup: name=systemd:/system/cloud9.service
       	  `-1470 /usr/bin/node4 /usr/share/cloud9/bin/cloud9.js -l 0....

Oct 10 11:53:29 yoder-black-bone node4[1470]: .){2ooooonnnnvnvnvvvvvIIIIIIll...`
Oct 10 11:53:29 yoder-black-bone node4[1470]: -{2oooonnnnnvvvvvvvlIIlllllil=...-
Oct 10 11:53:29 yoder-black-bone node4[1470]: . -."11oonnvvvnvvIIlIlliliiiii....
Oct 10 11:53:29 yoder-black-bone node4[1470]: . -+~!lvvnvIvIIllliiiii|i|||i|....
Oct 10 11:53:29 yoder-black-bone node4[1470]: . ..--~++++++++~+~+~+~+-+-+~+~....
Oct 10 11:53:29 yoder-black-bone node4[1470]: . .  . . .... . . .... .. ... ....
Oct 10 11:53:29 yoder-black-bone node4[1470]: Ajax.org Cloud9 IDE
Oct 10 11:53:29 yoder-black-bone node4[1470]: version 0.6
Oct 10 11:53:29 yoder-black-bone node4[1470]: Project root is: /var/lib/cloud9
Oct 10 11:53:29 yoder-black-bone node4[1470]: Point your browser to http://l...0

Notice the log messages have changed.

Autostart at boot time

You can use enable and disable to make a service start (or not start) at boot time.

beagle$ systemctl disable cloud9
rm '/etc/systemd/system/multi-user.target.wants/cloud9.service'
beagle$ systemctl enable cloud9
ln -s '/lib/systemd/system/cloud9.service' '/etc/systemd/system/multi-user.target.wants/cloud9.service'

Watch out though, if some other service needs the service you disabled, it will start anyway. If you want to stop a service NO MATTER WHAT.

beagle$ ln -s /dev/null /etc/systemd/system/servicename.service
beagle$ systemctl daemon-reload

systemd first looks in /etc/systemd/system and then looks in /lib/systemd/system. The command above places an empty file in /etc/systemd/system, so the real file in /lib/systemd/system is never seen.

Running your own service

thumb‎ Embedded Linux Class by Mark A. Yoder