kill

Send signals to processes.

Source: src/execution/builtins.f90:1139-1283

Synopsis

kill [-s signal | -signal] pid|jobspec ...
kill -l [signal]

Description

The kill builtin sends signals to processes or jobs. Despite its name, it can send any signal, not just termination signals.

Options

OptionDescription
-s signalSpecify signal by name
-signalSpecify signal (name or number)
-lList signal names
-l signalConvert signal number to name

Usage

Send SIGTERM (default)

kill 1234
kill %1        # Job 1

Send Specific Signal

kill -9 1234           # SIGKILL
kill -KILL 1234        # Same
kill -s KILL 1234      # Same

kill -HUP 1234         # SIGHUP
kill -TERM 1234        # SIGTERM
kill -STOP 1234        # Suspend
kill -CONT 1234        # Resume

List Signals

kill -l
#  1) SIGHUP    2) SIGINT    3) SIGQUIT
#  9) SIGKILL  15) SIGTERM  ...

kill -l 9
# KILL

kill -l TERM
# 15

Kill Job

sleep 100 &
# [1] 12345

kill %1        # Kill job 1
kill %%        # Kill current job
kill %+        # Same as %%

Common Signals

SignalNumberActionUse
HUP1TerminateReload config
INT2TerminateInterrupt (Ctrl+C)
QUIT3Core dumpQuit
KILL9TerminateForce kill (can't catch)
TERM15TerminateGraceful shutdown
STOP19StopSuspend (can't catch)
CONT18ContinueResume
USR110User definedCustom
USR212User definedCustom

Examples

Graceful Then Force

pid=1234

# Try graceful first
kill $pid
sleep 2

# Check if still running
if kill -0 $pid 2>/dev/null; then
    kill -9 $pid
fi

Kill Process Group

# Negative PID kills entire group
kill -- -1234

Reload Configuration

# Send SIGHUP to daemon
kill -HUP $(cat /var/run/daemon.pid)

Check If Process Exists

if kill -0 $pid 2>/dev/null; then
    echo "Process $pid is running"
fi

Kill All Background Jobs

kill $(jobs -p)

Signal Script

# In script: handle USR1
trap 'reload_config' USR1

# From outside: send USR1
kill -USR1 $(cat /var/run/script.pid)

Process Groups

# Start process group
(
    trap '' INT   # Ignore in group
    ./process1 &
    ./process2 &
    wait
) &
pgid=$!

# Kill entire group
kill -- -$pgid

Implementation

Source: builtins.f90:1139-1283

Key features:

  • Signal name/number conversion
  • Job specification parsing (%n)
  • Process group handling (negative PID)
  • Uses c_kill() system call

Exit Status

StatusCondition
0Signal sent successfully
1Invalid signal, PID, or permission denied

Notes

  • kill -0 checks if process exists (no signal sent)
  • Root can kill any process
  • Non-root can only kill own processes
  • -9 should be last resort

Differences from /bin/kill

Featurebuiltin/bin/kill
Job specsYesNo
Shell signalsYesLimited
Exit statusShell-awareStandard

See Also