understanding linux processes and services
Linux is a computer operating system, that is designed to be more reliable, robust and efficiency for the end-user. Also Linux is a multitasking operating system, which means it can run more than one program (application at a time) and also Linux is a multiuser system, because it can support more than one user at the same time.
Linux is running multiple program at the system via processes, which are instances of a running program. Linux provides many sophisticated tools for monitoring system usage, that can stop, pause or kill processes when necessary. All these can be done from the Linux shell; we can launch a process, pause or kill it. Also we can choose to run them in background or bring them to run again in foreground.
A process is a running instance of a command; the process is identified on the system by what is referred to as a process id. The process id is unique from all other process that are currently running in the system. This means that no other process can use that process id while that process is still running, however after a process is ended or killed another process can use that process id.
Along with a process id, other attributes are also associated with a running process. These attributes can include the name of the user that start the process, when it started, which group does the user belong and other informations.
Linux provides tools that can be used to monitor running processes and services in the Linux system. These tools can be used to list all the running processes in the system. One of the tool that we can use to list all running processes is the command, ps. But we can also use the top command, which will show all currently running commands in more details than the ps command will show.
Listing processes with ps
We can use ps command to list process with several options that work for ps to show more processes and their details.
- ps u;
This above command will show all running process with the name of the user that invoke them. To see more detail on how you can use the ps command. type ps --help.
Listing and changing processes with top
The top command provides a screen oriented and live means of displaying processes running on the system. By default, the top command will display all running processes on the system and order them according how much CPU time they are currently consuming. However, this behaviour can be altered to order the running processes by other attributes like memory usage, user or other attributes.
These are actions that can be performed when top command is running:
- h; this key when pressed will bring help information about the command
- M; to sort by memory usage instead of CPU.
- P; to sort by CPU usage
- 1; this key is used to toggle CPU information if the system got more than one CPU
- R; to reverse sort your output
- u; when this key is pressed, top command is going to allow you to filter process by username
Actions that can be performed by Top command
Top command can be used to monitor running processes on a Linux system. These are actions that can be performed by using top command.
Renicing a process: this is the process of reduce the priority of the process that is running on the system, either to reduce CPU attention to this process or to give other process that running on the system a chance to execute.
To renice a process, first we must note the process iD of the process we want to renice and press r the message will appear, then we must type the process ID. We will be prompted to renice PID to value, the value range between -19 to 20.
Killing a process: the top command can be used to kill the process. To kill a process using a top command, first we need to note the process ID of the process we wish to kill. Then we press the k key, we will be prompted to enter process ID of the process we want to kill. In killing a process they are two options. One is for terminate cleanly (15) and another is for killing the process outright.
Managing background and foreground processes
We can run multiple process at the same time in a Linux system and we can work with a single process at a time. So we need a way to move process from background to foreground and from foreground to background using shell.
We can run an active program in the background in several ways. One is to add an ampersand (&) to the end of a command line when we first run the command.
To stop a running command and put in the background; press CTRL+Z. After the command is stopped, we can either bring it back into the foreground to run (the fg command) or to start it running in the background (the bg command). Many program that are running in the background can still spew they output into the console, to prevent this we can redirect all background process output into file or null.
Starting background processes
We can start a background process when we first run it in the shell. To run a process into background we use ampersand (&) at the end of the command, this will tell shell to run this process as the background process.
- find / -iname 'name' > ~/log/files.txt &
This command will run into background and direct all its output into the specified txt file. And when this command finish executing it will return the job number and the process id.
To check which command are currently running in the background we use the jobs command. This command will return details about jobs that are running in the background. Details like status either running or stopped, to see process id of the background we type the jobs command with the option -l.
Using foreground and background commands
We can bring any command back to the foreground simply by using this command, fg. Fg command can be used to bring any process either stopped or one that is running in the background to the foreground.
- fg %jobnumber
- fg %1
This above command will bring back job number one to be running in the foreground.
if a command is stopped, you can start it to be running in the background using the bg command. The bg command is similar to fg command in syntax.
- bg %1
This command will start the process with job number 1 and run it in the background.
kill process with kill and killall
Although usually used for ending a running process,the kill and killall commands can actually be used to send and any valid signal to a running process. Besides telling a process to end, a signal might tell a process to reread configuration files, pause, or continue after being paused.
Signal are represented by both numbers and names. Signals that you might send most commonly from a command include SIGKILL(9), SIGTERM(15), and SIGHUP. To kill a process immediately, you can use SIGKILL. The SIGHUP signal tells a process to reread its configuration files. SIGSTOP signal tells a process to pause, while SIGCONT continues a stopped process.