Signal

Bash Shell

By default, the Bash Shell ignores the SIGQUIT(3) and SIGTERM(15) signals, so executing the following commands will not have any effect ($$ is the process ID of the current Shell).

kill -3 $$
kill -15 $$

If the SIGHUP(1) signal is received, the Bash Shell will exit, but before exiting, it will pass the SIGHUP signal to all processes and scripts started by that Shell.

kill -1 $$

TRAP

The trap command can be used to catch signals.

trap commands signals

Catch Signals

The SIGINT signal is the signal for Ctrl+C.

#!/usr/bin/env bash

trap "echo ' Trapped Ctrl-C'" SIGINT

count=1
while [ $count -le 5 ]
do
    echo "loop #$count"
    sleep 1
    count=$[ $count + 1 ]
done
loop #1
loop #2
^C Trapped Ctrl-C
loop #3
loop #4
^C Trapped Ctrl-C
loop #5

Exit Signals

You can catch the signals when the script exits.

trap "echo ' Trapped Ctrl-C'" EXIT

Remove Signals

Use -- to remove signals.

trap -- SIGINT

NOHUP

No Hang Up, processes or scripts started by this command will ignore the SIGHUP signal. In other words, even if the terminal exits, the processes or scripts started by that terminal will not exit.

run a command immune to hangups, with output to a non-tty
nohup ./foo

By default, STDOUT and STDERR are redirected to nohup.out.

-rw------- 1 kuga kuga   40 Sep  2 18:46 nohup.out

It’s better to redirect it yourself.

nohup ./foo > out.log 2>&1
-rw-rw-r-- 1 kuga kuga   62 Sep  2 18:50 out.log

Nice Value

A parameter that controls the priority of a process. The higher the nice value, the lower the priority of the process, and the less CPU resources it is allocated.

Range

The nice value ranges from -20 to 19:

  • -20: Highest priority.
  • 0: Default priority.
  • 19: Lowest priority.

Start Process Priority

nice -n VALUE command
nice -n 10 ./foo

Modify Process Priority

renice VALUE -p PID
renice 5 -p 404

Permissions

  • Regular users can only lower the priority of a process.
  • Only root users can raise the priority of a process.