Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kill a process in bash [duplicate]

Tags:

bash

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

like image 891
wyatt Avatar asked May 05 '10 16:05

wyatt


People also ask

How do I kill a process in bash?

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.

What is kill 9 PID?

“ 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.

How do I kill multiple instances of process in Linux?

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.


2 Answers

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.

like image 66
tanascius Avatar answered Sep 20 '22 11:09

tanascius


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.

like image 20
jweyrich Avatar answered Sep 19 '22 11:09

jweyrich