How do I kill a process which is running in bash - for example, suppose I open a file:
$ gedit file.txt
is there any way within the command prompt to close it? This example is fairly trivial, since I could just close the window, but it seems to come up a bit, particularly when I mistype commands.
Also is there any way to escape an executable which is running? This probably has the same solution, but I thought I'd ask anyway.
Thanks
Using Pure Bash Features. First, we use the cut command to get the process's start time from the 22nd field in the /proc/PID/stat. Then, we sleep for the specified timeout and kill the process with the SIGTERM signal. Additionally, we ensure that the start time of the process has not changed before killing it.
“ kill -9” command sends a kill signal to terminate any process immediately when attached with a PID or a processname. It is a forceful way to kill/terminate a or set of processes. “ kill -9 <pid> / <processname>” sends SIGKILL (9) — Kill signal. This signal cannot be handled (caught), ignored or blocked.
killall Command – kill the processes by name. By default, it will send a TERM signal. The killall command can kill multiple processes with a single command. If more than one process runs with that name, all of them will be killed.
You have a multiple options:
First, you can use kill. But you need the pid of your process, which you can get by using ps, pidof or pgrep.
ps -A // to get the pid, can be combined with grep -or- pidof <name> -or- pgrep <name> kill <pid>
It is possible to kill a process by just knowing the name. Use pkill or killall.
pkill <name> -or- killall <name>
All commands send a signal to the process. If the process hung up, it might be neccessary to send a sigkill to the process (this is signal number 9, so the following examples do the same):
pkill -9 <name> pkill -SIGKILL <name>
You can use this option with kill
and killall
, too.
Read this article about controlling processes to get more informations about processes in general.
To interrupt it, you can try pressing ctrl c to send a SIGINT. If it doesn't stop it, you may try to kill it using kill -9 <pid>
, which sends a SIGKILL. The latter can't be ignored/intercepted by the process itself (the one being killed).
To move the active process to background, you can press ctrl z. The process is sent to background and you get back to the shell prompt. Use the fg
command to do the opposite.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With