EBC Exercise 21b systemd

From eLinux.org
Revision as of 09:46, 24 January 2023 by Yoder (talk | contribs) (Stopping and Starting)
Jump to: navigation, search

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.

Administration

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

bone$ systemctl
 UNIT                                                                    >
 proc-sys-fs-binfmt_misc.automount                                       >
 sys-devices-platform-ocp-44c00000.interconnect-44c00000.interconnect:seg>
 sys-devices-platform-ocp-44c00000.interconnect-44c00000.interconnect:seg>
 sys-devices-platform-ocp-47400000.target\x2dmodule-47401400.usb-musb\x2d>
 sys-devices-platform-ocp-47400000.target\x2dmodule-47401400.usb-musb\x2d>
 sys-devices-platform-ocp-47400000.target\x2dmodule-47401c00.usb-musb\x2d>
 sys-devices-platform-ocp-48000000.interconnect-48000000.interconnect:seg>
 sys-devices-platform-ocp-48000000.interconnect-48000000.interconnect:seg>
 sys-devices-platform-ocp-48000000.interconnect-48000000.interconnect:seg>
 sys-devices-platform-ocp-48000000.interconnect-48000000.interconnect:seg>
 sys-devices-platform-ocp-48000000.interconnect-48000000.interconnect:seg>
 sys-devices-platform-ocp-48000000.interconnect-48000000.interconnect:seg>
 sys-devices-platform-ocp-48000000.interconnect-48000000.interconnect:seg>
 sys-devices-platform-ocp-48000000.interconnect-48000000.interconnect:seg>
 sys-devices-platform-ocp-48000000.interconnect-48000000.interconnect:seg>
 sys-devices-platform-ocp-48000000.interconnect-48000000.interconnect:seg>
 sys-devices-platform-ocp-48000000.interconnect-48000000.interconnect:seg>
 sys-devices-platform-ocp-48000000.interconnect-48000000.interconnect:seg>
 sys-devices-platform-ocp-48000000.interconnect-48000000.interconnect:seg>
 sys-devices-platform-ocp-48000000.interconnect-48000000.interconnect:seg>
 sys-devices-platform-ocp-48000000.interconnect-48000000.interconnect:seg>
 sys-devices-platform-ocp-48000000.interconnect-48000000.interconnect:seg>
 sys-devices-platform-ocp-48000000.interconnect-48000000.interconnect:seg>
 sys-devices-platform-ocp-48000000.interconnect-48000000.interconnect:seg>
 sys-devices-platform-ocp-4a000000.interconnect-4a000000.interconnect:seg>
 sys-devices-virtual-misc-rfkill.device                                  >
 sys-devices-virtual-net-docker0.device                                  >
 sys-devices-virtual-tty-ttyGS0.device                                   >
 sys-module-configfs.device                                              >
 sys-module-fuse.device                                                  >
 sys-subsystem-net-devices-can0.device                                   >
 sys-subsystem-net-devices-can1.device                                   >
 sys-subsystem-net-devices-docker0.device                                >
 sys-subsystem-net-devices-eth0.device                                   >
 sys-subsystem-net-devices-usb0.device                                   >
 sys-subsystem-net-devices-usb1.device                                   >
 sys-subsystem-net-devices-wlan0.device                                  >
 -.mount                                                                 >
 dev-mqueue.mount                                                        >
 run-docker-netns-default.mount                                          >
 run-user-1000.mount                                                     >
 sys-fs-fuse-connections.mount

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.

bone$ systemctl status nginx.service
● nginx.service - A high performance web server and a reverse proxy server
    Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enab>
    Active: active (running) since Wed 2023-01-11 15:25:04 EST; 1 weeks 5 days ago
      Docs: man:nginx(8)
  Main PID: 750 (nginx)
     Tasks: 2 (limit: 1024)
    Memory: 3.7M
       CPU: 2.018s
    CGroup: /system.slice/nginx.service
            ├─750 nginx: master process /usr/sbin/nginx -g daemon on; master_proces>
            └─751 nginx: worker process

Jan 11 15:25:01 ece434 systemd[1]: Starting A high performance web server and a reve>
Jan 11 15:25:04 ece434 systemd[1]: Started A high performance web server and a rever>

Stopping and Starting

You can stop a process with

bone$ sudo systemctl stop nginx.service

Now try accessing the web server (192.168.7.2). It isn't there. You can start it with

bone$ systemctl start nginx.service
bone$ systemctl status nginx.service
● nginx.service - A high performance web server and a reverse proxy server
    Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
    Active: active (running) since Tue 2023-01-24 10:07:23 EST; 38s ago
      Docs: man:nginx(8)
   Process: 16409 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Process: 16410 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Main PID: 16411 (nginx)
     Tasks: 2 (limit: 1024)
    Memory: 5.7M
       CPU: 392ms
    CGroup: /system.slice/nginx.service
            ├─16411 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
            └─16412 nginx: worker process

Jan 24 10:07:12 ece434 systemd[1]: Starting A high performance web server and a reverse proxy server...
Jan 24 10:07:23 ece434 systemd[1]: Started A high performance web server and a reverse proxy server.

Notice the log messages have changed.

Now, when you browse to 192.168.7.2 you will get an answer.

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.

Stopping no matter what

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

If you check in exercises/realtime you find boneServer.js a server for some demos. You run it with:

beagle$ cd exercises/realtime
beagle$ ./boneServer.js
Listening on 8080
   info  - socket.io started

Now point a browser to 192.168.7.2:8080. You'll see a list of demos served up by boneServer.js. Suppose you want the boneServer to automatically start every time the the Beagle boots. Here is how to do it.

We need to create a service file and the quickest way is to find one that does similar things.

beagle$ systemctl | grep bone
bonescript-autorun.service  loaded active running   Bonescript autorun
bonescript.service          loaded active running   Bonescript server
bonescript.socket           loaded active running   bonescript.socket

I see a couple of bonescript servers that look promising.

beagle$ systemctl status bonescript
bonescript.service - Bonescript server
         Loaded: loaded (/lib/systemd/system/bonescript.service; static)
         Active: active (running) since Sun 2000-01-09 15:07:55 EST; 13 years 9 months ago
       Main PID: 357 (node)
         CGroup: name=systemd:/system/bonescript.service
       	  `-357 /usr/bin/node server.js

Jan 09 15:07:55 yoder-black-bone systemd[1]: Starting Bonescript server...
Jan 09 15:08:04 yoder-black-bone bonescript[357]: [35B blob data]
Jan 09 15:08:05 yoder-black-bone bonescript[357]: - - - [Sun, 09 Jan 2000 20:...

Looks like the file is in /lib/systemd/system/bonescript.service copy it to a handy place and take a look.

beagle$ cp /lib/systemd/system/bonescript.service boneServer.service
beagle$ cat boneServer.service
[Unit]
Description=Bonescript server

[Service]
WorkingDirectory=/usr/lib/node_modules/bonescript
ExecStart=/usr/bin/node server.js
SyslogIdentifier=bonescript

[Install]
WantedBy=multi-user.target

I copied the last two line from /lib/systemd/system/cloud9.service since they are needed to start at boot time. Modify the file so it will work for your server. Make sure to modify 'Description' as well or your service will confuse itself with the original bonescript.service.

There is one other thing you have to add to the file. When node.js runs it needs to know where to find its modules. There is an environment variable that says where

beagle$ echo $NODE_PATH
/usr/lib/node_modules

You need to add a line to the service file that set this environment variable. Look at the other files in /lib/systemd/system to see how this is done. (Hint: grep Env *).

Once your BoneServer.service file is ready, copy it to the right place

beagle$ cp boneServer.service /lib/systemd/system

and start the server

beagle$ systemctl start boneServer

Point your browser to 192.168.7.2:8080 and see if it works.

To make it work after rebooting

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

Notice it copies your file to another place. Try rebooting and see if it works.




thumb‎ Embedded Linux Class by Mark A. Yoder