Lab 2 - Process Management

Alexandru Calcatinge bio photo By Alexandru Calcatinge

In this laboratory you will exercise with systemd and process monitoring.

Contents:

Lab 2.1 Working with systemd

Lab objective 1: Viewing running services and configure and troubleshoot them
Lab objective 2: Adding a new startup service with systemd

Lab 2.2 Process monitoring

Lab objective 3: System monitoring using stress
Lab objective 4: Process monitoring


Laboratory objective 1: Viewing running services and configure and troubleshoot them

Commands used: systemctl

 

Solution:

  1. to view the status of all processes that are running on your system, just run the command:
systemctl

The list will be long, and thus will be shown in a manner similar to using the less command, page by page. In my case there were 167 (units) processes listed, while using openSUSE as a default distro.

  1. check the status of a unit using the command
systemctl status [unit]

alexandru@linux-vje9:~> systemctl status apparmor.service
● apparmor.service - Load AppArmor profiles
 Loaded: loaded (/usr/lib/systemd/system/apparmor.service; enabled; vendor preset: enabled)
 Active: active (exited) since Sat 2017-09-02 12:25:55 EEST; 44min ago
 Process: 375 ExecStart=/etc/init.d/boot.apparmor start (code=exited, status=0/SUCCESS)
 Main PID: 375 (code=exited, status=0/SUCCESS)
 Tasks: 0 (limit: 512)
 CGroup: /system.slice/apparmor.service

Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.

  1. to start or stop a service use:
systemctl start bluetooth.service

systemctl status bluetooth.service

systemctl stop bluetooth.service

systemctl restart bluetooth.service

systemctl status bluetooth.service
  1. to enable or disable a service to start during boot, use the commands:
systemctl enable bluetooth.service

systemctl disable bluetooth.service

Adding or disabling a service to load during boot is basically the process of adding or removing symbolic links in the /etc/systemd/system/multi-user.target.wants directory.

check for a systems status using the command:

systemctl is-enabled postfix.service
  1. reboot or shutdown your system using systemd commands:
systemctl reboot

systemctl shutdown

 

↑Top


Laboratory objective 2: Adding a new startup service with systemd

Commands used: touch, ls, cd, vim, chmod, systemctl

 

Solution:

This exercise addresses some more advanced solutions and uses commands that will be discussed later on. Nevertheless, it is of great importance as you will exercise how to work with systemd commands and services/processes.

  1. create as root a file called fake2.service under /etc/systemd/system

First let us explore the /etc/systemd/system directory, by moving inside it and do a listing there:

alexandru@linux-vje9:~> cd /etc/systemd/system/ alexandru@linux-vje9:/etc/systemd/system> ls bluetooth.target.wants getty.target.wants dbus-org.bluez.service graphical.target.wants dbus-org.freedesktop.Avahi.service multi-user.target.wants dbus-org.freedesktop.ModemManager1.service network-online.target.wants dbus-org.freedesktop.nm-dispatcher.service network.service dbus-org.opensuse.Network.AUTO4.service remote-fs.target.wants dbus-org.opensuse.Network.DHCP4.service sockets.target.wants dbus-org.opensuse.Network.DHCP6.service SuSEfirewall2_setup.service dbus-org.opensuse.Network.Nanny.service sysinit.target.wants default.target timers.target.wants default.target.wants xdm.service
  1. now create the file fake2.service using the command (as root):
sudo touch fake2.service
  1. check if the file was created by doing a listing of the directory with the ls command

  2. open a text editor, like vim (which is installed by default in openSUSE) and start typing content:

[Unit]
Description=fake2 After=network.target

[Service]
ExecStart=/bin/echo I am starting the fake2 service
ExecStop=/bin/echo I am stopping the fake2 service

[Install]
WantedBy=multi-user.target

Now save the contents in vim and exit.

  1. change the permissions on the file to make it executable:
chmod 755 /etc/systemd/system/fake2.service
  1. now start, stop and check status of the service with systemctl
sudo systemctl start fake2.service 

sudo systemctl status fake2.service 

sudo systemctl stop fake2.service
  1. to make the service run at system boot:
sudo systemctl enable fake2.service 

sudo systemctl disable fake2.service
  1. reboot your system for the changes to take effect

 

↑Top


Laboratory objective 3: System monitoring using stress

Commands used: stress-ng

 

Solution:

Stress is a utility program that will put stress on your system’s CPU, RAM, I/O etc. You can install it in your system by looking for the stress or stress-ng into your repositories. Under openSUSE you can install the stress-ng instance:

sudo zypper in stress-ng

then you can run it

stress-ng -c 2 -i 2 -m 2 -t 10s

This will fork off 2 CPU intensive processes, each spinning on a sqrt() calculation, fork off 2 I/O intensive processes, each spinning on sync(), fork off 2 memory intensive processes, each spinning malloc() allocating 256MB by default (you can change the size with –vm-bytes 128M for example), and the processes are run for 10 seconds.

You can watch stress working by opening your system monitor.

 

↑Top


Laboratory objective 4: Process monitoring

Commands used: ps, nice, renice, top

 

Solution:

  1. run ps with -ef options first, and then with aux option and notice the differences
ps -ef

ps aux

The output will be LONG, so perhaps you would like to pipe this to less.

  1. run ps so that only the process ID, priority, nice value and the process command line are displayed
alexandru@linux-vje9:~> ps -o pid,pri,ni,cmd
 PID PRI NI CMD
 4789 19 0 bash
 5273 19 0 ps -o pid,pri,ni,cmd
  1. start a new bash session, then start another bash session with the nice command with a nice value of 10. After that, run ps like in the last exercise and notice the differences in priority and nice value, and note the process ID of the two bash sessions
alexandru@linux-vje9:~> bash
alexandru@linux-vje9:~> nice -n 10 bash
alexandru@linux-vje9:~> ps -o pid,pri,ni,cmd
 PID PRI NI CMD
 5346 19 0 bash
 5363 19 0 bash
 5380 9 10 bash
 5397 9 10 ps -o pid,pri,ni,cmd
  1. change the nice value of one of the bash sessions to 15 using renice command. Observe the values.
alexandru@linux-vje9:~> renice 15 -p 5363
5363 (process ID) old priority 0, new priority 15
alexandru@linux-vje9:~> ps -o pid,pri,ni,cmd
 PID PRI NI CMD
 5346 19 0 bash
 5363 4 15 bash
 5380 9 10 bash
 5408 9 10 ps -o pid,pri,ni,cmd
  1. run the top command and watch the output as it changes.
top

top - 23:35:55 up 1:26, 2 users, load average: 0.33, 0.28, 0.27
Tasks: 207 total, 1 running, 206 sleeping, 0 stopped, 0 zombie
%Cpu(s): 1.3 us, 0.7 sy, 0.0 ni, 97.8 id, 0.2 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem: 3928816 total, 3793096 used, 135720 free, 5200 buffers
KiB Swap: 4192252 total, 1552 used, 4190700 free. 1192920 cached Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 
 3329 alexand+ 20 0 1643120 151776 67632 S 2.649 3.863 1:28.19 gnome-she+
 5076 alexand+ 20 0 3672540 351960 278216 S 1.656 8.958 1:37.56 VirtualBox
 1911 root 20 0 338436 95116 57904 S 0.993 2.421 1:06.75 Xorg 
 3735 alexand+ 20 0 2518836 626588 119200 S 0.993 15.95 5:07.03 firefox 
 5340 alexand+ 20 0 612128 35300 27272 S 0.993 0.898 0:01.13 gnome-ter+
 4945 alexand+ 20 0 1404536 76696 57052 S 0.662 1.952 0:06.88 VirtualBox
 3502 alexand+ 20 0 2958720 173996 44984 S 0.331 4.429 0:10.44 dropbox 
 4956 alexand+ 20 0 810256 19676 15648 S 0.331 0.501 0:05.04 VBoxSVC 
 5082 alexand+ 20 0 88316 8888 6944 S 0.331 0.226 0:00.67 VBoxXPCOM+
 5087 alexand+ 20 0 678152 17844 14936 S 0.331 0.454 0:01.89 VBoxSVC 
 1 root 20 0 185440 5988 3908 S 0.000 0.152 0:02.62 systemd 
 2 root 20 0 0 0 0 S 0.000 0.000 0:00.00 kthreadd 
 3 root 20 0 0 0 0 S 0.000 0.000 0:00.02 ksoftirqd+
 5 root 0 -20 0 0 0 S 0.000 0.000 0:00.00 kworker/0+
 7 root 20 0 0 0 0 S 0.000 0.000 0:00.72 rcu_sched 
 8 root 20 0 0 0 0 S 0.000 0.000 0:00.00 rcu_bh 
 9 root rt 0 0 0 0 S 0.000 0.000 0:00.01 migration+

 

↑Top