After seven articles talking about System Storage, in which we killed the topic researching and discussing (as the Arabic expression says), it is time to knock another door: Managing Linux Processes.
Wish you nice reading.
Definition of Process
The logic says the first question somebody may ask would be: what process is.
A process can be defined as a program running in memory.
Normally, a typical Linux process has:
- A Name: which is the name of the running program.
- A Process ID (PID): this is a unique identifier that distinguishes this process from other processes.
- A Parent Process ID (PPID): as nobody creates or gives birth to himself/herself, a process also is invoked (started) by another process. The invoker process is considered a parent for the other child process. Each process keeps the process ID of its parent PPID.
- User ID (UID): the ID of the user who started the process (run the program).
Searching and Displaying Active Processes
Before manipulating processes, we need first to locate them. For that purpose, there are several commands that help us find and list the currently active processes.
The ps Command
The UNIX/Linux ps command is a system command line tool that displays information about the active processes.
Syntax for Common Uses
The following two forms have exactly the same output. They both display every process on the system with full format:
ps –Af ps -ef
The following two forms also display every process, but with extra full format:
ps –AF ps -eF
The following command displays processes started by a specific user:
ps –u USERNAME
- Display all processes running on the system:
- Display all processes started by the the user eduonix:
The top Command
The top command provides a dynamic real-time view of a running system.
The dynamic output displays and keeps updating the following info:
- The current system time.
- The uptime: how long the system has been running.
- The number of users currently logged in to the system.
- The average of CPU load.
- Memory usage statistics.
- Swap area statistics..
- Running processes ordered by percentage of CPU utilization, with the following information for each process:
- The process owner: USER.
- Process priority.
- Nice value.
- Virtual memory used by the process.
- Resident real memory used by the process.
- The status of the process.
- CPU usage percentage.
- The command that started the process.
The pgrep Command
The pgrep command looks through the currently running processes and lists the process IDs which matches the selection criteria to stdout.
pgrep [-d delimiter][-v] [–u USER,…] PATTERN
–d delimiter Sets a string that will be as a separator between resulting process IDs.
-u USER1,USER2,… Only match processes whose effective user ID is listed.
PATTERN Specifies a Regular Expression for matching against the process names or command lines.
-v Inverts the selection.
- To list process IDs for processes started by the user eduonix, separated by commas:
pgrep -u eduonix -d,
- To list the process IDs of bash processes:
The pidof Command
pidof returns the process ID of a running program. It prints that ID on the standard output.
The command is straightforward and takes one or more process names.
For example, to get the process ID(s) of process(es)with the name sshd:
To get the process IDs for a list of programs:
After locating processes, we need to decide what to do with them.
The kill Command
This command is used to terminate one or more processes given their PIDs.
The following two forms have exactly the same meaning, which is sending a signal to one or more processes:
kill –s SIGNAL PID… kill –SIGNAL PID…
To list the available signals supported:
- The following will print the list of signals:
- The following command will terminate immediately the rsyslogd process:
Note that after being terminated, the process no longer exists.
pkill and killall
We have seen that the kill command sends a signal to one or more processes, given the PID(s) of the process(es). The commands pkill and killall could kill processes by name. In other words, both commands looks for running processes matching some criteria, and kills them.
You can think of the pkill command as a combination between the functionalities of the commands pgrep and kill.
- A process is a program that is running in memory.
- Each process has a name, unique PID, Parent process, and User ID.
- ps displays information about a the active processes.
- top provides a dynamic real-time view of top active processes.
- pgrep searches the currently running processes for one or more search criteria, and returns the PID of the process(es) matching that search.
- pidof returns the PID of a running program.
- kill terminates one or more processes given their PIDs.
- pkill and killall both kill processes by name.