If you carefully read the article and understood the principles detailed inside, you should be able to answer most of the questions.
Should you find any errors or imprecisions, feel free to leave a comment.
Good luck!
Processes Basics
Question 1 : on your Linux host, there are many processes running at a time. However, one information can uniquely identify a process.
How is it called?
Expand Me
On Linux, a process can be uniquely identified by a PID (or process ID), which can’t be assigned to two distinct processes at a time.
Question 2 : when your system boots, it starts the very first process on your instance.
How is it called?
Expand Me
It is called the init process and it is used in order to execute initialization scripts for network, jobs or modules. On recent distributions, it has been replaced by a systemd process.
Question 3 : you currently have a shell terminal open on your host and you execute the following command.
Internally, what are the system calls invoked to perform such a command?
Expand Me
First, the kernel will fork the current process (i.e the bash interpreter) into a new process. Next, the image of bash process will be replaced by the loaded image of the ls program. Finally, the command is executed.
Question 4 : you open a shell terminal on your host by clicking on “Terminal”.
In short, describe how the terminal works.
Expand Me
The terminal is a simple interactive process that waits perpetually for user input. When a command is issued, the command is executed by forking into a new process and executing the command in it. In the meantime, the parent process (i.e the terminal itself) waits for termination of the child process. When it has finished, the parent process resumes.
Processes Commands
Question 5 : you are asked by your system administrator to identify all processes that you own on the host.
Which command would you run to do that?
Expand Me
The easiest way to do that is to execute the ps command. By default, it won’t report tty devices, but you can choose to execute “ps u” to see all processes.
Question 6 : you are asked by your system administrator to identify all the processes on your system.
Can you provide two commands that display all processes on the host?
Expand Me
To display all processes on Linux, you can either use “ps aux” (which is a BSD syntax) or “ps -ef” (which is a POSIX syntax)
Question 7 : what command displays processes as a tree on Linux?
Expand Me
To display all the processes as a process tree, you have to use the “pstree” command.
Background & Foreground Processes
Question 8 : what syntax is used on Linux in order to execute a process in the background?
Expand Me
To execute a process in the background, you have to append a “&” sign at the end of the command.
Question 9 : what is the term that describes a process that was started in a terminal shell?
Expand Me
A process executed in a shell is called a “job” and the jobs command displays your current shell jobs.
Question 10 : you executed a command in the background, but you want to have your process executed in the foreground.
What command would you execute?
Expand Me
The job id is 1 so you would execute “fg %1”
Question 11 : your process is now executed in the foreground.
What controls would you hit on your keyboard in order to stop the process (and not kill it) ?
Expand Me
In order to stop a process, or to send a SIGSTOP signal to a process, you have to hit Ctrl + Z.
Question 12 : your process is now interrupted.
How would you resume the execution in the background?
Expand Me
In order to resume the execution, you can execute the “bg %1” command.
Question 13 : what keys can you hit on your keyboard in order to send a SIGINT to a process in the foreground?
Expand Me
In order to send a SIGINT to a signal in the foreground, you would have to hit Ctrl + C.
Signals & Processes
Question 14 : how would you define a signal when it comes to processes?
Expand Me
On Linux, signals are a form of interprocess communication (also called IPC) that creates and sends asynchronous notifications to running processes about the occurrence of a specific event. Signals can be informational (SIGUSR1, SIGUSR2 for example) or they can be used in order to convey a specific order to the process (interruption, stop, kill)
Question 15 : what signal is used on Linux in order to gently shutdown a process?
Expand Me
To shutdown gracefully a process, you have to use the SIGTERM signal (also known as signal number 15)
Question 16 : on the other hand, let’s say that you want to kill a process immediately, what signal would you use for that? What’s the number of this signal?
Expand Me
To kill a process without giving it a chance to shutdown gracefully, you have to issue a SIGKILL signal (also known as signal 9)
Question 17 : you executed the following command in your terminal shell.
The process takes a long time to execute, so you decide to leave your host and shutdown your current terminal.
What happened?
Expand Me
When you closed your terminal, a SIGHUP signal was sent to the process. As a consequence, the process was stopped immediately.
Question 18 : what solutions can you give in order to avoid what just happened?
Expand Me
To prevent a process from being killed with a SIGHUP signal, you can use the “nohup” command in the following way “nohup ./script &”
Question 19 : off the top of your head, can you provide other signals used on Linux systems?
Expand Me
SIGQUIT a signal invoked when you are exiting an interactive session. It can be used in terminal shells or in SSH sessions for example. SIGCONT is a signal used in order to resume a stopped process (after a SIGSTOP)
Advanced Processes Commands
Question 20 : what command in used on Linux in order to list all processes given a specific pattern?
Expand Me
To search for processes given a specific pattern, you can use the “pgrep” command with the following syntax “pgrep “
Question 21 : what command would you use in order to easily kill (SIGKILL) all processes starting with “fire” ?
Expand Me
To kill all processes starting with “fire”, you would execute “pkill fire*”
Question 22 : on Linux, what command is used in order to execute a process with a custom priority level?
Expand Me
“Nice” is the command used to execute a command with a custom priority, in order for it to use more or less CPU resources.
Question 23 : a process has a nice level of 19, is it going to use as much resources as possible?
Expand Me
No, the nicer the process, the more you are willing to share resources with others. As a consequence, the process has a very low priority level.
Question 24 : what is the default nice level when processes are created on Linux?
Expand Me
By default, processes are created with a nice level of 0.
Question 25 : as a non sudo-user, can you create a process with a nice level of -5?
Expand Me
No, non sudo users are not able to create processes with a nice level lower than the default one assigned. Moreover, when you created a process with a custom nice level, you are not able to lower it, even if it is greater than zero.
Question 26 : what command can be used in order to set the priority of a running process on Linux?
Expand Me
To customize the priority of a running process, you have to use the “renice” command with this syntax “renice -n “
Monitoring Processes
Question 27 : what command can be used on Linux in order to monitor processes in real time?
Expand Me
To monitor resources used by processes, one can use the “top” command or the “htop” command for a more friendly display.
Question 28 : what shortcut can be used in the top command to change the refresh rate?
Expand Me
To change the refresh rate, you can hit the “d” key and set it to 1.0 for example.
Question 29 : what option can be used in order to execute the top command for a custom number of iterations?
Expand Me
To execute “top” for a given number of iterations, you can use “top –n “
Question 30 : are you aware of any other solutions in order to monitor Linux processes effectively?
Expand Me
To monitor Linux systems, a system administrator can use Cockpit, Grafana, Prometheus, Nagios or Telegraf.
Conclusion
With those Linux processes exercises, you hopefully learnt more about processes, how you can handle them and monitor them effectively.