Advanced Process Termination Options
In this step, we'll explore some advanced options of the pkill
command that allow for more sophisticated process management.
Using Different Signal Types
By default, pkill
sends the SIGTERM signal (signal 15) to processes. This signal allows processes to terminate gracefully, closing files and performing cleanup operations. However, there are cases where you might want to use a different signal.
Let's create a script that handles signals:
cd ~/project
nano signal_handler.sh
Add the following content to the script:
#!/bin/bash
trap 'echo "Received SIGHUP (1)"; exit 0' SIGHUP
trap 'echo "Received SIGINT (2)"; exit 0' SIGINT
trap 'echo "Received SIGTERM (15)"; exit 0' SIGTERM
echo "Process started with PID $$"
echo "Use: pkill -[signal] -f signal_handler.sh to send signals"
while true; do
sleep 1
done
Make the script executable:
chmod +x ~/project/signal_handler.sh
Run the script in the background:
~/project/signal_handler.sh &
Now, let's try sending different signals to the process:
- Send a SIGHUP signal (signal 1):
pkill -HUP -f signal_handler.sh
- Start the script again and send SIGINT (signal 2):
~/project/signal_handler.sh &
pkill -INT -f signal_handler.sh
- Start the script again and send the default SIGTERM (signal 15):
~/project/signal_handler.sh &
pkill -f signal_handler.sh ## Default is SIGTERM
For each signal, you should see the corresponding message in the terminal output before the process exits.
Terminating Processes by Age
pkill
allows you to target processes based on their age using the --newer
and --older
options.
Let's create a few processes with different start times:
cd ~/project
nano age_test.sh
Add the following content to the script:
#!/bin/bash
while true; do
echo "Process running with PID $$"
sleep 5
done
Make the script executable:
chmod +x ~/project/age_test.sh
Start the first process and record the reference file:
~/project/age_test.sh &
touch ~/project/reference_time
Wait a few seconds, then start two more processes:
sleep 5
~/project/age_test.sh &
~/project/age_test.sh &
Now, let's terminate only the processes that were started after the reference file was created:
pkill -f --newer ~/project/reference_time age_test.sh
Verify which processes are still running:
ps aux | grep age_test.sh
You should see only the first process is still running, as it was started before the reference file was created.
Terminate the remaining process:
pkill -f age_test.sh
Limiting pkill with Process Owner
You can also limit the actions of pkill
to processes owned by a specific user. In a multi-user system, this is particularly useful.
For demonstration purposes, let's run a few processes as the current user:
~/project/rogue_app.sh &
~/project/rogue_app.sh &
Now, let's terminate these processes, but only those owned by your current user:
pkill -f -u $(whoami) rogue_app.sh
The -u
option specifies the username of the process owner. The $(whoami)
command substitution gets your current username.
Verify that all processes have been terminated:
ps aux | grep rogue_app.sh
You should only see the grep
command itself in the output.
This ability to target processes by owner is particularly useful in multi-user environments where you want to avoid affecting processes belonging to other users.