How can I kill a process using the bash command prompt? Do I always need to kill the process using PID?
Q. I’d like to run a command or shell script immune to hangups, with output to a non-tty or file. How do I use nohup command to run script called ~/utils/backup upload?
A. nohup is a Unix command that is used to run another command while suppressing the action of the HUP (hangup) signal, enabling the command to keep running after the user who issues the command has logged out. It is most often used to run commands in background as daemons. Output that would normally go to the terminal goes to a file called nohup.out if it has not already been redirected.
Run a script with nohup command
Type the command as follows to run a script called ~/utils/backup with ‘upload’ as argument to script:
$ nohup ~/utils/backup "upload" &
Q. Sometime I need to stop a command or task under UNIX. I also noticed that some process will ignore my keystroke-generated signals such as CTRL + C or CTRL+D, so my question is â€“ How do I kill process in UNIX?
A. You need to use a command called kill. The kill utility sends a signal to the processes specified by the pid operand(s). Only the super-user (root) may send signals to other users’ processes.
kill command syntax
The kill command causes the specified signal to be sent to the specified process. The kill command has the general form as follows:
kill –N PID
- N is a signal number
- PID is the Process Identification Number. If you do not know the PID, it can be learned through the ps command.
Understanding signal numbers
The signal number 1 is a hangup signal. I recommended using 1 signal because it should kill the process and it can save the buffer (if supported). For example if it is an editor, save the buffer. This is the default if you do not specify a signal number. Signal number 9, a kill signal, is the surest way to kill a process.
Some of the more commonly used signals:
|1|| HUP (hang up)|
| 2|| INT (interrupt)|
| 3|| QUIT (quit)|
| 6|| ABRT (abort)|
| 9|| KILL (non-catchable, non-ignorable kill)|
| 14|| ALRM (alarm clock)|
| 15|| TERM (software termination signal)|
How do I use kill command?
Terminate the processes with pids 1412 and 1157:
$ kill 1412 1157
Send the hangup signal (SIGHUP) to the process with pid 5071:
# kill -s HUP 5071
Terminate the process group with pgid 12117:
# kill -- -12117
If you do not know the PID of a process, you can learn it by issuing a process status command, ps.
# ps | more
# ps | grep file.c
# ps | grep -i httpd
To send -9 (KILL) singal to the processo with pid 1234, enter:
# kill -9 1234