Managing Process and Jobs in Linux
Working with bash commands to manage jobs and processes in Linux
๐ Hello!
In this article, I have focused on process management in Linux and converted the most necessary commands that will help you to manage processes efficiently. This article will be helpful for Cloud Engineer, DevOps Engineer, System Administrators, and Linux users.
PermalinkCommands to Manage Process and Jobs in Linux
Permalink1. TOP command
top command is the most helpful command to see the system resources stats, in simple terms, it's like a task manager.
[root@izhar ~] top
Running the above commands shows the following output:
b(batch mode) โ not to block console, 1 number of iteration
[root@izhar ~] top -b -n 1
There are multiple controlling options available, for usage refer to doc
Permalink2. UPTIME
uptime is generally used to see the system up and running time, the number of logged-in users, system load, and other details. Some options available with uptime:
- -p: to see system uptime in pretty format
- -s: system up since
Permalink3. PS
ps is the command used to list processes in the system, ps has multiple options here are some most occasional used:
Command with options | Descriptions |
ps -ef | to list all process status |
ps -auxw | to get detail of processes |
ps -eLf | to get more columns |
ps -o pid,cmd,ni | to get specific column details |
ps -o pid,cmd,ni --pid process_id | to get details of selected column of specific process |
ps -eo ppid,pid,cmd,%mem,%cpu | get memory and CPU info with specific column |
Sorting the process output based on specific column, - means descending order sorting.
[root@izhar ~] ps -eo ppid,pid,cmd,%mem,%cpu --sort -%mem
For more detail on ps refer to doc
Permalink4. PGREP
pgrep is used to get all the process id of a specific process or program
[root@izhar ~] pgrep [options] <process_name>
For more options and usage refer to doc
Permalink5. PSTREE
pstree is used to display a tree of processes
For more options and usage refer to doc
Permalink6. JOURNALCTL
journalctl is used to get detail of processes in the system, this is a very helpful command to get more details about the systems internal, here are a few usages of this command. To see available options with command use --help or read man journalctl
[root@izhar ~] journalctl --help
[root@izhar ~] journalctl -xe
Permalink7. NICE
nice is the command used to run a program with modified scheduling priority.
Before using the command first understand the process priority.
Process Priority
- Values -100 to 39
- Higher number yields to lower
- Negative numbers are considered "real-time" process
Using NICE for process
- Values ranges from -19 to 20
- Higher number yields to lower
- Maps to underlying priority
Nice to Priority Mapping
<--- Highest Priority Priority (PRI) Lowest Priority ---->
-100
(rt) -99 0 20 39
|_______|__ _ __|___________________|_______________|
_____________________________________
| | |
-20 0 19
NICE (NI)
Syntax
[root@izhar ~] nice -n 10 <cmd> &
Note: -n is number priority
<cmd> is command/process to run
& to run in background
Permalink8. JOBS
jobs is used to list the running, stopped, or paused jobs
[root@izhar ~] jobs
[root@izhar ~] help jobs
Permalink9. FG (Foreground)/BG (Background)
- bg is the command used to run the job behind the system
- fg is the command used to run the job on stdout
Syntax
[root@izhar ~] bg %[jobid]
[root@izhar ~] fg %[jobid]
Permalink10. KILL
Before using the kill command it is important to know about signals and interrupts.
Signals VS Interrupts
Signal | Interrupts |
Can stop execution to take some action | Can stop execution to take some action |
Communication from Linux kernel or process to another process | Communication from CPU to Linux kernel |
Stop (suspends) a process, terminate a process, or dump memory | Handle divide by 0, page faults or accept hardware input |
Common Signals
- HUP (Hang up)
- Sent to a background job, when the spawning foreground job is ended
- Related "nohup" utility
- INT (Interrupt)
- Ctrl + C
- Commonly used to stop runaway foreground jobs
- QUIT
- Commonly used for terminating a process, while dumping its memory
- KILL
- Terminating stubborn processes, cannot be ignored
- TERM
- Commonly issued by other software for terminating a process
- STOP
- Stop(suspend) a running process, cannot be ignored
- STP
- Referred as STOP
- Ctrl + Z
- Stop(suspend) a running process, cann be ignored
- CONT
- Continue
- Resume a stopped process
Sending a signal using kill command
[root@izhar ~] kill -s KILL <pid>
[root@izhar ~] kill -s STOP <pid>
[root@izhar ~] kill -s CONT <pid>
or
[root@izhar ~] kill -INT <pid>
[root@izhar ~] kill -QUIT <pid>
[root@izhar ~] kill -KILL <pid>
[root@izhar ~] kill -STOP <pid>
[root@izhar ~] kill -CONT <pid>
<! to kill all process with that name -- >
[root@izhar ~] killall <proces_name>
<!-- killing all process of a user -- >
[root@izhar ~] killall --user <username> --signal STOP
[root@izhar ~] pkill -f <process> --signal KILL
For more usage and info refer to kill doc, pkill doc, killall doc
Suspending Jobs Tactically
- The balancing act of interactive servers
- Some jobs require more resources
- Jobs being terminated causes frustration
- Disk filling up
- CPU is needed
- More RAM will be available later
Thank you for reading!