Linux/Unix Process Control Commands | Bash
In Bash, process control commands are used to manage and control running processes. Three commonly used commands for process control are kill, pkill, and killall. These commands allow you to send signals to processes, helping you manage their execution.
Understanding Processes and Jobs
- Processes:Instances of programs executing tasks on a system, represented by a unique process ID (PID). Processes can be in various states, such as running, stopped (suspended), waiting, or exiting.
- Jobs:Collections of one or more processes grouped together, typically initiated from the same command in the terminal. Job control allows you to manage both foreground (currently active) and background (running alongside other tasks) jobs.
Here's a detailed explanation of each command with examples:
kill Command
kill is a command that sends a signal to a process, asking it to terminate.
Syntax:- signal:is an optional argument specifying the signal to send. If not provided, the default signal is SIGTERM (terminate). You can also use signal numbers or names.
- PID:is the process ID of the target process.
In this example, the kill command sends the default SIGTERM signal to the process with PID 1234, asking it to terminate successfully.
Here, the command sends the SIGKILL signal to the process with PID 5678, forcing it to terminate immediately.
pkill Command
pkill is a command that allows you to send signals to processes based on their names or other attributes.
Syntax:options are additional parameters, and pattern is a regular expression used to match process names.
Example:This command sends the SIGTERM signal to all processes whose names match the regular expression 'firefox', effectively terminating them.
In this example, the -f option allows matching against the entire command line. It sends a signal to processes whose command lines contain "python script.py."
killall Command
killall is similar to pkill, but it matches processes based on their names.
Syntax:options are additional parameters, and process_name is the name of the target process.
Example:This command forcefully terminates all processes with the name 'chrome' by sending the SIGKILL signal.
Here, the command terminates all processes owned by the specified username.
Remember that using signals like SIGKILL (9) forcefully terminates a process without allowing it to clean up, so it should be used wisely. Always prefer using SIGTERM (15) first to give the process a chance to shut down.
Conclusion
Process control commands such as kill, pkill, and killall are used to manage running processes. The kill command terminates a specific process by sending a signal, while pkill and killall can match processes based on names or attributes and send signals for termination. These commands are essential for controlling and managing processes in a Linux environment.