Managing Process and Jobs in Linux

Working with bash commands to manage jobs and processes in Linux

ยท

5 min read

๐Ÿ‘‹ 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.

Commands to Manage Process and Jobs in Linux

1. 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: image.png

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

2. UPTIME

uptime is generally used to see the system up and running time, the number of logged-in users, system load, and other details. image.png Some options available with uptime:

  • -p: to see system uptime in pretty format image.png
  • -s: system up since image.png

3. PS

ps is the command used to list processes in the system, ps has multiple options here are some most occasional used:

Command with optionsDescriptions
ps -efto list all process status
ps -auxwto get detail of processes
ps -eLfto get more columns
ps -o pid,cmd,nito get specific column details
ps -o pid,cmd,ni --pid process_idto get details of selected column of specific process
ps -eo ppid,pid,cmd,%mem,%cpuget memory and CPU info with specific column

image.png image.png image.png image.png image.png image.png Sorting the process output based on specific column, - means descending order sorting.

[root@izhar ~] ps -eo ppid,pid,cmd,%mem,%cpu --sort -%mem

image.png

For more detail on ps refer to doc

4. PGREP

pgrep is used to get all the process id of a specific process or program

[root@izhar ~] pgrep [options] <process_name>

image.png

For more options and usage refer to doc

5. PSTREE

pstree is used to display a tree of processes

image.png image.png

For more options and usage refer to doc

6. 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

image.png image.png

[root@izhar ~] journalctl -xe

7. 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

8. JOBS

jobs is used to list the running, stopped, or paused jobs

[root@izhar ~] jobs
[root@izhar ~] help jobs

9. 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]

10. KILL

Before using the kill command it is important to know about signals and interrupts.

Signals VS Interrupts

SignalInterrupts
Can stop execution to take some actionCan stop execution to take some action
Communication from Linux kernel or process to another processCommunication from CPU to Linux kernel
Stop (suspends) a process, terminate a process, or dump memoryHandle 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!

ย