My notes regarding various Linux commands that manage processes – it helps me to remember things when I write them down; why not here!

& – Background Process

A process can be made to run in the background (thereby releasing the terminal) by suffixing it with an ampersand &

COMMAND &

This will assign a job number and PID
[JOB] PID

bg – background

Moves a job to the background and resumes it if paused (a foreground job can be paused with CTRL-Z)

bg JOB
#moves JOB to the background and resumes if paused

fg – foreground

Moves a job to the foreground (so it can be exited, for example)

fg JOB
#moves JOB to the foreground

jobs

Display all jobs in the current terminal

+ denotes most recent job
- denotes jobs that started before most recent job

kill/pkill/killall – Signal Process

the kill command can be used to send a signal to a process

kill -l
#display LIST of signals

kill PID
#terminate process by PID

kill %JOB
#terminate process by JOB

kill SIGNAL PROCESS
#send SIGNAL to PROCESS

pkill essentially merges kill with pgrep

pkill SIGNAL PATTERN
#send SIGNAL to all processes that match PATTERN

killall -u mrnoobot
#terminate all processes belonging to mrnoobot

nice/renice – Prioritization of Processes

nice allows user to set a priority for a process on a scale ranging from -20 (HIGH PRIORiTY) to 19 (LOW PRIORITY). Default niceness is ZERO

nice -n 19 COMMAND OPTIONS
#run COMMAND as a lowest priority process

To adjust niceness of a process use renice

renice -n -20 -p PID
#set priority of process with PID to HIGHEST PRIORITY

nohup – No Hang Up

nohup prefixed to a command prevents it being "hung up" (stopped) when the user logs out

nohup COMMAND OPTIONS

pgrep

pgrep is essentially a fusion of ps and grep

pgrep -i sshd
#displays the PID process ID for the sshd process (if running)

pgrep -li
#displays PID and process name for the sshd process (if running)

pgrep -u mrnoobot -l
#displays PID and process names for processes owned by user mrnoobot

ps

ps lists processes

PID #Process ID
TTY #Name of terminal or pseudo-terminal running the process
TIME #Amount of processor time used by the process
CMD #The command that started the process

ps x
#replaces CMD with COMMAND which shows full command including arguments
#also adds STAT column to show status of the process
D #Uninterruptible sleep
R #Running
S #Interruptible sleep
T #Stopped
Z #Zombie

ps aux
#actually command with three options
x #as above
u #show processes by user UID
a #show all processes

ps -ef
e #show every process in every terminal
f #display full details

ps -ejH
j # use jobs format
H # show hierarchical tree

PIPING PS INTO GREP

ps aux | grep -i sshd
#search for process sshd running on the system.

screen – Multiple Processes

screen allows the operation and control of multiple processes which can be attached/reattached at will as SESSIONS

screen
#launches screen

screen -list
#displays current SESSIONS

CTRL-A #allows entry of a screen command
CTRL-A ? #lists screen commands
CTRL-A d #detach session

SESSION SYNTAX:
PID:TTY(TERMINAL):HOST

screen -r PID
#RESUME detached process by PID

screen -S mrnoobot -d -m top
#start top in a DETACHED MODE SESSION name mrnoobot

tmux – Multiple Processes

tmux is similar to screen in that allows operation and control of multiple processes which can be attached/detached at will as SESSIONS

tmux
#start a tmux session

CTRL-B #allows entry of a tmux command
CTRL-B d #detaches current session
CTRL-B % #split sessions into multiple vertical 'windows'
CTRL-B " #split sessions into multiple horizontal windows
exit #exits a 'window'

tmux list-sessions | tmux ls
#lists tmux sessions

tmux new-session 'nano mrnoobot'
#creates nano mrnoobot as a tmux session

tmux attach -t SESSION
#attaches to SESSION

tmux kill-session -t SESSION
#terminates SESSION

top – Process Manager

top displays and manages processes

top
#enters top process manager which continues running
h #display help
k #KILL send signal (prompts for PID)
r #RENICE amend priority (prompts for PID)
q #quit

uptime

uptime provides information on uptime, number of logged in users, and CPU load over 1, 5 and 15 minute intervals

watch

watch allows the user to monitor a process. By default it will run a defined command every two seconds, and can be exited by typing CTRL+C

Examples:

watch ps aux
#monitor processes

watch -n 15 tail -20 nooblog
#monitors the last 20 lines of nooblog every 15 seconds

watch -d free
#monitor memory and highlights DIFFERENCES each iteration