Topic
PROCESSES, JOBS, SIGNALS AND TRAPS
5/10/2021
Linux Processes
Process – a piece of running code on your computer that’s executing some tasks ◦ Every process has a process ID (PID)
◦ System uses this number to keep track of the different processes and assign resources
◦ Every process has a parent process ID (PPID) because every process is started by a parent.
◦ init process is the only process that is not started by another process and has PID 1 (PPID 0)
We can take a look at the process table for the init process with the ps command: ps –f 1 UID PID PPID C STIME TTY STAT TIME CMD
root 1 0 0 Apr08 ? Ss 0:59 /sbin/init splash
TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS
1
Linux Processes
What calls the init process? ◦ The Linux Kernel does.
A system is broken up into two pieces ◦ kernel space
◦ user space
Booting up your machine calls the Linux kernel -> does some stuff, once it’s done with everything it does in kernel space, the only thing it does in user space is call the init process.
5/10/2021
Linux Processes Life Cycle
TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS
TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS
2
Foreground and background processes
• Foreground processes are started by typing a command. While a foreground process is running the shell waits.
• Background processes are started by appending a & to the command
• While a background process is running the shell continues.
• Output from the background process is mixed in.
• If a background process needs input, it gets suspended.
• Each process you start from the shell is known as a “job”, i.e., you have maximally one foreground job and can have multiple background jobs.
• Background processes can be controlled: jobs
kill %N kill PID fg %N
Foreground and background processes
• Foreground processes can be suspended and brought to the background Ctrl Z
Ctrl C bg
bg %N
fg %N
• A background process will be suspended automatically if it needs to read data from stdin. Some programs, e.g., perl check stdin when they start, so as background processes they hang. The solution is to redirect stdin to come from /dev/null (‘electronic sink’).
nohup disown
5/10/2021
lists background jobs and their process ID
terminates background job #N
terminates background job with process ID PID
brings job N to the foreground
TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS
suspends a foreground job
terminates foreground job
takes a suspended job to the background
takes suspended job N to the background
brings job N to the foreground
can be used to leave jobs running when you logout (“hangup”).
TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS
3
Sequences and commands
• Simple commands are commands
• Pipelines of simple commands are commands
• Sequences of simple commands with & are commands, and will
be executed concurrently.
• Sequences of simple commands with ; are commands, and will
be executed sequentially.
• Commands in ()s are simple commands.
(ls -l | wc) & ls -l ; ls –a
ls –l & ls –&
5/10/2021
Foreground/Background Processing
Performance Objectives:
1. Manipulate Processes and PIDs (ps, ^c, ^z, kill)
2. Run Commands in the Background (fg, bg, &)
3. Move a foreground command to background (^z, bg) 4. Check the status of background jobs (jobs)
5. Terminate a job (^c, ^d, kill)
6. Keep a job alive after logoff (nohup)
7. Suspend a running job
lists files and directories and counts lines as one job in the background
lists files and directories consecutively
lists files and directories sequentially
TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS
TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS
4
Processes and PIDs – ps
Each command generates an independent process.
Each process has a unique process identification number (PID).
5/10/2021
Processes and PIDs – ps
Use the ps command to see what processes are running. $ ps
PID TT STAT TIME COMMAND 3835 p2 S 0:00 -csh (csh) 3838 p2 R 0:00 ps
◦ PID is the process identifier
◦ TT is the control terminal
◦ STAT is the status of the process
TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS
TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS
5
Processes and PIDs – ps (Con’t)
STAT is the status of the process:
◦ R
◦ T
◦ P
◦ D
◦ S
◦ I
Runnable processes. Stopped processes. In page wait.
Non-interruptible wait (waiting for disk etc). Sleeping for less than about 20 seconds. Idle, sleeping longer than about 20 seconds. Terminated; waiting for parent (“zombie”).
◦ Z
(W) indicates process is swapped out.
Interrupting a Command
To “stop” a running interactive process use ^Z.
The message “stopped” is displayed.
A ps command will reveal the status: $ ps
PID TT STAT TIME COMMAND
5/10/2021
TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS
3835 p2 S
3839 p2 T
3840 p2 R
0:00 -csh (csh) 0:00 cat
0:00 ps
TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS
6
Foreground Processing – fg
Type fg to continue running the stopped job in the foreground.
Use jobs to list job numbers and status. $ jobs
[1] – Stopped cat > file4 [2] +Running vi file.c
5/10/2021
TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS
Background Processing – bg
To continue the job in the background use: $ bg%n
◦ Where n is the job number.
To initiate a command in the background use the ampersand: ◦ $ command & (ex. $ ./myscript.sh &)
TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS
7
Terminating Jobs
Terminate a stopped command with kill. $ kill pid
Use -1 or -9 option to forcefully terminate. $ kill -9 1234
Terminate a running job with ^C
Terminate a program waiting for input with a ^d
5/10/2021
TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS
Keeping a Job Active – nohup
On logout, the shell removes child processes running in background.
You can terminate the shell before it sends signal 1.
Children continue to run and you will be logged off because your login shell has terminated.
TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS
8
Keeping a Job After Logout
Start a foreground process and then logoff. Allow the process to continue to run.
5/10/2021
◦ ◦ ◦ ◦
Enter
Execute bg to place process in background,
Run ps -u userid to obtain the pid of your login shell,
Enter kill -9 shellpid to kill the login shell and log off; but leave the process running detached from the terminal.
The process must not do any terminal I/O.
TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS
Keeping a Job Active – nohup
Include the command nohup (no arguments) in your .logout file.
No need to use kill, logout normally.
Executed on logout, nohup directs the login shell to not stop child processes. Processes survive because no signal is sent.
$ nohup command-name &
$ exit
Or
$nohup /path/to/command-name arg1 arg2 > myoutput.log &
$exit
TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS
9
Monitoring for Unwanted Jobs
Unwanted processes can consume valuable computer time.
Consider including the following command in the .login and .logout files to spot runaway or unwanted processes.
◦ ps -u userid
Signals on Linux
7) SIGBUS
12) SIGUSR2
17) SIGCHLD
22) SIGTTOU
27) SIGPROF
34) SIGRTMIN 39) SIGRTMIN+5 44) SIGRTMIN+10 49) SIGRTMIN+15 54) SIGRTMAX-10 59) SIGRTMAX-5 64) SIGRTMAX
TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS
5/10/2021
(base) $ kill -l 1) SIGHUP
6) SIGABRT
11) SIGSEGV
16) SIGSTKFLT 21) SIGTTIN
26) SIGVTALRM 31) SIGSYS
38) SIGRTMIN+4 43) SIGRTMIN+9 48) SIGRTMIN+14 53) SIGRTMAX-11 58) SIGRTMAX-6 63) SIGRTMAX-1
2) SIGINT
3) SIGQUIT
8) SIGFPE
13) SIGPIPE
18) SIGCONT
23) SIGURG
28) SIGWINCH 35) SIGRTMIN+1 40) SIGRTMIN+6 45) SIGRTMIN+11 50) SIGRTMAX-14 55) SIGRTMAX-9 60) SIGRTMAX-4
4) SIGILL
9) SIGKILL
14) SIGALRM
19) SIGSTOP
24) SIGXCPU
29) SIGIO
36) SIGRTMIN+2 41) SIGRTMIN+7 46) SIGRTMIN+12 51) SIGRTMAX-13 56) SIGRTMAX-8 61) SIGRTMAX-3
5) SIGTRAP
10) SIGUSR1
15) SIGTERM
20) SIGTSTP
25) SIGXFSZ
30) SIGPWR
37) SIGRTMIN+3 42) SIGRTMIN+8 47) SIGRTMIN+13 52) SIGRTMAX-12 57) SIGRTMAX-7 62) SIGRTMAX-2
CTRL+C is 2) SIGINT
TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS
10
Handling signals
Unix allows you to send a signal to any process – you can use the number identifier for each signal or the abbreviation (ex. kill -1 1234 or kill –HUP 1234)
-1 = hangup kill -HUP 1234 -2 = interrupt with ^C kill -2 1235 no argument = terminate kill 1235
-9 = kill kill -9 1236
◦ -9 cannot be blocked
list your processes with
ps -u userid
Handling signals
Default action for most signals is to end process ◦ term: signal handler
Bash allows to install custom signal handler Syntax:
trap ‘handler commands’ signals
Example:
trap ‘echo do not hangup’ 1 2
5/10/2021
TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS
TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS
11
Example: trap hangup
#! /bin/bash
# kill -1 won’t kill this process
# kill -2 will
trap ‘echo dont hang up’ 1
while true do
echo “try to hang up”
sleep 1 done
Example: trap multiple signals
#! /bin/sh
# plain kill or kill -9 will kill this trap ‘echo 1’ 1
trap ‘echo 2’ 2
while true; do
echo -n .
sleep 1 done
5/10/2021
TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS
TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS
12
Example: removing temp files
#! /bin/bash
trap ‘cleanup; exit’ 2
cleanup () {
/bin/rm -f /tmp/tempfile.$$.?
}
for i in 1 2 3 4 5 6 7 8 do
echo “$i.iteration”
touch /tmp/tempfile.$$.$i
sleep 1
done cleanup
Restoring default handlers
trap without a command list will remove a signal handler Use this to run a signal handler once only
5/10/2021
TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS
#! /bin/sh
trap ‘justonce’ 2
justonce() {
echo “not yet”
trap 2
while true; do
echo -n “.”
sleep 1 done
# now reset it
}
TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS
13