Rectangle 27 55

You can kill processes by name with killall <name>

killall sends a signal to all processes running any of the specified commands. If no signal name is specified, SIGTERM is sent.

Signals can be specified either by name (e.g. -HUP or -SIGHUP ) or by number (e.g. -1) or by option -s.

If the command name is not regular expression (option -r) and contains a slash (/), processes executing that particular file will be selected for killing, independent of their name.

But if you don't see the process with ps aux, you probably won't have the right to kill it ...

Wait, wait. Doesn't ps aux list all the processes from all users running on the system? Which processes are not shown?

To force terminating a program that is not responding, send KILL instead of TERM: killall -s KILL firefox

linux - How can I kill a process by name instead of PID? - Stack Overf...

linux
Rectangle 27 200

When using nohup and you put the task in the background, the background operator (&) will give you the PID at the command prompt. If your plan is to manually manage the process, you can save that PID and use it later to kill the process if needed, via kill PID or kill -9 PID (if you need to force kill). Alternatively, you can find the PID later on by ps -ef | grep "command name" and locate the PID from there. Note that nohup keyword/command itself does not appear in the ps output for the command in question.

If you used a script, you could do something like:

nohup my_command > my.log 2>&1 &
echo $! > save_pid.txt

This will run my_command saving all output into my.log (in a script, $! represents the PID of the last process executed). The 2 is the file descriptor for standard error (stderr) and 2>&1 tells the shell to route standard error output to the standard output (file descriptor 1). It requires &1 so that the shell knows it's a file descriptor in that context instead of just a file named 1. The 2>&1 is needed to capture any error messages that normally are written to standard error into our my.log file (which is coming from standard output). See I/O Redirection for more details on handling I/O redirection with the shell.

If the command sends output on a regular basis, you can check the output occasionally with tail my.log, or if you want to follow it "live" you can use tail -f my.log. Finally, if you need to kill the process, you can do it via:

kill -9 `cat save_pid.txt`
rm save_pid.txt

A slight clarification, it's not nohup per se, that prints the PID, it's the final & which backgrounds it, e.g. ls & would show the PID for running ls

@Viraj 2 is the "standard error" file descriptor. > is the shell redirection, and &1 is the "standard output" file descriptor (the & is needed here so the shell won't think I am referring to a file named 1). So 2 > &1 redirects any standard error output to the standard input. Since the prior > my.log means to redirect standard output to my.log, we need a way to ensure that error messages also go to my.log. 2 > &1 ensures that such errors go to standard output, which in turn goes to my.log. See I/O Redirection.

echo $!

linux - How to get the process ID to kill a nohup process? - Stack Ove...

linux bash grep nohup
Rectangle 27 86

You can use the lsof command. Let port number like here is 8090

lsof -i:8090

This command returns a list of open processes on this port.

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ssh 75782 eoin 5u IPv6 0x01c1c234 0t0 TCP localhost:8090 (LISTEN)

To free the port, kill the process using it(the process id is 75782)

kill -9 75782

This one worked for me. here is the link from the original post: link

to kill process forcefully you will need to use -9 like kill -9 75782; as sometimes few processes aren't kill with just kill

unix - How to kill a process running on particular port in Linux? - St...

linux unix port kill-process
Rectangle 27 33

This prints to stdout the process ids of everything running on <port_number>:

fuser -n tcp <port_number>
fuser -n tcp <port_number> 2> /dev/null

We can then supply these process ids to the kill command:

sudo kill $(fuser -n tcp <port_number> 2> /dev/null)

You could also put this in a function if you do it a lot:

function killport() {
    sudo kill $(fuser -n tcp $1 2> /dev/null)
}

unix - How to kill a process running on particular port in Linux? - St...

linux unix port kill-process
Rectangle 27 135

CTRL+Z then use the command bg to put the process in the background.

To start a process where you can even kill the terminal and it still carries on running

nohup [command] [-args] > [filename] 2>&1 &
nohup /home/edheal/myprog -arg1 -arg2 > /home/edheal/output.txt 2>&1 &

To just ignore the output (not very wise) change the filename to /dev/null

To get the error message set to a different file change the &1 to a filename.

i tried it but as its outputing something , it again come at foreground with% showing how much data is uploaded

The process will write any output to the terminal as you instructed the process to do that at the start. You will have to restart it to get output to write to file or /dev/null. See edit above.

so it means , if the process is already started then there is no way redirect the ouput

Yes indeed. You cannot effect the process only the shell that it is running from. You could do a kill -9 <pid of your shell and then the process will still run and output will be thrown away - but you will have to login again to start a new shell.

You could also use the disown command if you need to log out of your terminal session, and you would like it to continue running.

bash - How can I put the current running linux process in background? ...

linux bash shell background
Rectangle 27 127

This looks like a good article on the subject: Taming the OOM killer.

The gist is that Linux overcommits memory. When a process asks for more space, Linux will give it that space, even if it is claimed by another process, under the assumption that nobody actually uses all of the memory they ask for. The process will get exclusive use of the memory it has allocated when it actually uses it, not when it asks for it. This makes allocation quick, and might allow you to "cheat" and allocate more memory than you really have. However, once processes start using this memory, Linux might realize that it has been too generous in allocating memory it doesn't have, and will have to kill off a process to free some up. The process to be killed is based on a score taking into account runtime (long-running processes are safer), memory usage (greedy processes are less safe), and a few other factors, including a value you can adjust to make a process less likely to be killed. It's all described in the article in a lot more detail.

Edit: And here is another article that explains pretty well how a process is chosen (annotated with some kernel code examples). The great thing about this is that it includes some commentary on the reasoning behind the various badness() rules.

I really love the article links. I'd suggest anyone who's interested in the topic to read them -- especially the comments on the lwn article.

"Linux will give it that space, even if it is claimed by another process" That's not quite how virtual memory works...

linux - Who "Killed" my process and why? - Stack Overflow

linux process kill signals
Rectangle 27 2

Resume and kill the process:

sudo ...
pkill -9 node

or, if you don't have pkill, this may work:

killall node
kill $(ps -e | grep node | awk '{print $1}')

sometimes the process will list its own grep, in which case you'll need:

kill $(ps -e | grep dmn | awk '{print $2}')

h/t @ruffin from the comments on the question itself. I had the same issue and his comment helped me solve it myself.

Stop node.js program from command line - Stack Overflow

node.js command
Rectangle 27 247

You don't say if the tree you want to kill is a single process group. (This is often the case if the tree is the result of forking from a server start or a shell command line.) You can discover process groups using GNU ps as follows:

ps x -o  "%p %r %y %x %c "

If it is a process group you want to kill, just use the kill(1) command but instead of giving it a process number, give it the negation of the group number. For example to kill every process in group 5112, use kill -TERM -- -5112.

kill -74313 -bash: kill: 74313: invalid signal specification If i add the kill -15 -GPID it worked perfectly.

As usual with almost any command, if you want a normal argument that starts with a - to not be interpreted as a switch, precede it with --: kill -- -GPID

pgrep can offer an easier way to find the process group ID. For example, to kill my-script.sh's process group, run kill -TERM -$(pgrep -o my-script.sh).

ps -o pid --no-headers --ppid $PARENT_PID

And if you modify the format slightly and sort, you get to see all processes nicely grouped and beginning with (potentially) the group parent in each group: ps x -o "%r %p %y %x %c" | sort -nk1,2

linux - Best way to kill all child processes - Stack Overflow

linux bash shell process
Rectangle 27 55

You can clean up a zombie process by killing its parent process with the following command:

kill -HUP $(ps -A -ostat,ppid | grep -e '[zZ]'| awk '{ print $2 }')

This command clears the zombie from the process table, but it does not 'kill' the zombie. The zombie is already dead.

grep
ps ... | awk '/[zZ]/{print $2}'

AFAICS, this command doesn't kill the zombie, but sends SIGHUP to its parent process (presumably killing the parent if it doesn't handle SIGHUP and causing the zombie to be reparented to init, as described in the previous answer). So be careful with this command, it might kill something you weren't expecting to...

This didn't work for me . i did "kill -HUP processID" and the process is still there as a zombie

@WilliamPursell when you answer a question, please describe the consequence of using the command line and what it does explicitly because it does kill all the programs running on the computer.

linux - How to kill zombie process - Stack Overflow

linux shell ubuntu debian zombie-process
Rectangle 27 54

You can clean up a zombie process by killing its parent process with the following command:

kill -HUP $(ps -A -ostat,ppid | grep -e '[zZ]'| awk '{ print $2 }')

This command clears the zombie from the process table, but it does not 'kill' the zombie. The zombie is already dead.

grep
ps ... | awk '/[zZ]/{print $2}'

AFAICS, this command doesn't kill the zombie, but sends SIGHUP to its parent process (presumably killing the parent if it doesn't handle SIGHUP and causing the zombie to be reparented to init, as described in the previous answer). So be careful with this command, it might kill something you weren't expecting to...

This didn't work for me . i did "kill -HUP processID" and the process is still there as a zombie

@WilliamPursell when you answer a question, please describe the consequence of using the command line and what it does explicitly because it does kill all the programs running on the computer.

linux - How to kill zombie process - Stack Overflow

linux shell ubuntu debian zombie-process
Rectangle 27 43

I am using red hat linux on a VPS server (and via SSH - putty), for me the following worked:

First, you list all the running processes:

ps -ef

Then in the first column you find your user name; I found it the following three times:

  • One was the SSH connection
  • The second was an FTP connection
  • The last one was the nohup process

Then in the second column you can find the PID of the nohup process and you only type:

kill PID

(replacing the PID with the nohup process's PID of course)

I hope this answer will be useful for someone I'm also very new to bash and SSH, but found 95% of the knowledge I need here :)

I think you meant PID instead of UID here?

I agree with @wprins. Killing the UID didn't work for me, but killing the PID did.

linux - How to get the process ID to kill a nohup process? - Stack Ove...

linux bash grep nohup
Rectangle 27 157

Kill all the processes belonging to the same process tree using the Process Group ID (PGID)

kill -- -$PGID
  • kill -9 -$PGID Use the signal KILL (9)
PGID
PID
kill -- -$(ps -o pgid= $PID | grep -o '[0-9]*')
TERM
kill -9 -$(ps -o pgid= $PID | grep -o '[0-9]*')
KILL
$PID
  • kill -9 -"$PGID" => Send signal 9 (KILL) to all child and grandchild...
PGID=$(ps opgid= "$PID")
ps opgid= $PID
ps -o pgid --no-headers $PID
pgid
pgrp
  • ps inserts leading spaces when PID is less than five digits and right aligned as noticed by tanager. You can use:PGID=$(ps opgid= "$PID" | tr -d ' ')
  • ps from OSX always print the header, therefore Speakus proposes:PGID="$( ps -o pgid "$PID" | grep [0-9] | tr -d ' ' )"
  • grep -o [0-9]* prints successive digits only (does not print spaces or alphabetical headers).
PGID=$(ps -o pgid= $PID | grep -o [0-9]*)
kill -TERM -"$PGID"  # kill -15
kill -INT  -"$PGID"  # correspond to [CRTL+C] from keyboard
kill -QUIT -"$PGID"  # correspond to [CRTL+\] from keyboard
kill -CONT -"$PGID"  # restart a stopped process (above signals do not kill it)
sleep 2              # wait terminate process (more time if required)
kill -KILL -"$PGID"  # kill -9 if it does not intercept signals (or buggy)
  • As noticed by davide and Hubert Kario, when kill is invoked by a process belonging to the same tree, kill risks to kill itself before terminating the whole tree killing.
  • Therefore, be sure to run the command using a process having a different Process-Group-ID.
> cat run-many-processes.sh
#!/bin/sh
echo "ProcessID=$$ begins ($0)"
./child.sh background &
./child.sh foreground
echo "ProcessID=$$ ends ($0)"

> cat child.sh
#!/bin/sh
echo "ProcessID=$$ begins ($0)"
./grandchild.sh background &
./grandchild.sh foreground
echo "ProcessID=$$ ends ($0)"

> cat grandchild.sh
#!/bin/sh
echo "ProcessID=$$ begins ($0)"
sleep 9999
echo "ProcessID=$$ ends ($0)"

Run the process tree in background using '&'

> ./run-many-processes.sh &    
ProcessID=28957 begins (./run-many-processes.sh)
ProcessID=28959 begins (./child.sh)
ProcessID=28958 begins (./child.sh)
ProcessID=28960 begins (./grandchild.sh)
ProcessID=28961 begins (./grandchild.sh)
ProcessID=28962 begins (./grandchild.sh)
ProcessID=28963 begins (./grandchild.sh)

> PID=$!                    # get the Parent Process ID
> PGID=$(ps opgid= "$PID")  # get the Process Group ID

> ps fj
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
28348 28349 28349 28349 pts/3    28969 Ss   33021   0:00 -bash
28349 28957 28957 28349 pts/3    28969 S    33021   0:00  \_ /bin/sh ./run-many-processes.sh
28957 28958 28957 28349 pts/3    28969 S    33021   0:00  |   \_ /bin/sh ./child.sh background
28958 28961 28957 28349 pts/3    28969 S    33021   0:00  |   |   \_ /bin/sh ./grandchild.sh background
28961 28965 28957 28349 pts/3    28969 S    33021   0:00  |   |   |   \_ sleep 9999
28958 28963 28957 28349 pts/3    28969 S    33021   0:00  |   |   \_ /bin/sh ./grandchild.sh foreground
28963 28967 28957 28349 pts/3    28969 S    33021   0:00  |   |       \_ sleep 9999
28957 28959 28957 28349 pts/3    28969 S    33021   0:00  |   \_ /bin/sh ./child.sh foreground
28959 28960 28957 28349 pts/3    28969 S    33021   0:00  |       \_ /bin/sh ./grandchild.sh background
28960 28964 28957 28349 pts/3    28969 S    33021   0:00  |       |   \_ sleep 9999
28959 28962 28957 28349 pts/3    28969 S    33021   0:00  |       \_ /bin/sh ./grandchild.sh foreground
28962 28966 28957 28349 pts/3    28969 S    33021   0:00  |           \_ sleep 9999
28349 28969 28969 28349 pts/3    28969 R+   33021   0:00  \_ ps fj

The command pkill -P $PID does not kill the grandchild:

> pkill -P "$PID"
./run-many-processes.sh: line 4: 28958 Terminated              ./child.sh background
./run-many-processes.sh: line 4: 28959 Terminated              ./child.sh foreground
ProcessID=28957 ends (./run-many-processes.sh)
[1]+  Done                    ./run-many-processes.sh

> ps fj
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
28348 28349 28349 28349 pts/3    28987 Ss   33021   0:00 -bash
28349 28987 28987 28349 pts/3    28987 R+   33021   0:00  \_ ps fj
    1 28963 28957 28349 pts/3    28987 S    33021   0:00 /bin/sh ./grandchild.sh foreground
28963 28967 28957 28349 pts/3    28987 S    33021   0:00  \_ sleep 9999
    1 28962 28957 28349 pts/3    28987 S    33021   0:00 /bin/sh ./grandchild.sh foreground
28962 28966 28957 28349 pts/3    28987 S    33021   0:00  \_ sleep 9999
    1 28961 28957 28349 pts/3    28987 S    33021   0:00 /bin/sh ./grandchild.sh background
28961 28965 28957 28349 pts/3    28987 S    33021   0:00  \_ sleep 9999
    1 28960 28957 28349 pts/3    28987 S    33021   0:00 /bin/sh ./grandchild.sh background
28960 28964 28957 28349 pts/3    28987 S    33021   0:00  \_ sleep 9999

The command kill -- -$PGID kills all processes including the grandchild.

> kill --    -"$PGID"  # default signal is TERM (kill -15)
> kill -CONT -"$PGID"  # awake stopped processes
> kill -KILL -"$PGID"  # kill -9 to be sure

> ps fj
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
28348 28349 28349 28349 pts/3    29039 Ss   33021   0:00 -bash
28349 29039 29039 28349 pts/3    29039 R+   33021   0:00  \_ ps fj

I notice in this example PID and PGID are equal (28957). This is why I originally thought kill -- -$PID was enough. But in the case the process is spawn within a Makefile the Process ID is different from the Group ID.

I think kill -- -$(ps -o pgid= $PID | grep -o [0-9]*) is the best simple trick to kill a whole process tree when called from a different Group ID (another process tree).

Hi @davide. Good question. I think kill should always send the signal to the whole tree before receiving its own signal. But in some specific circumstances/implementations, kill may send to itself the signal, be interrupted, and then receive its own signal. However the risk should be enough minimal, and may be ignored in most of cases because other bugs should occur before this one. Can this risk be ignored in your case? Moreover other answers have this common bug (kill part of the process tree being killed). Hope this help.. Cheers ;)

This only works if the sub commands themselves don't become group leaders. Even such simple tools like man do that. On the other hand, if you want to kill gandchild process from child process, kill -- -$pid won't work. So it is not generic solution.

The example is the "child" trying to kill its children (so grandchildren of the user initiated command). IOW, try to kill the background process hierarchy in child.sh.

> kill -QUIT -"$PGID" # same signal as [CRTL+C] from keyboard ---- QUIT should be replaced to INT to be true

for OSX option --no-headers is not supported so code should be updated to: PGID="$( ps -o pgid "$PID" | grep [0-9] | tr -d ' ' )"

linux - Best way to kill all child processes - Stack Overflow

linux bash shell process
Rectangle 27 20

This still doesn't work for me with the current accepted answer. A work around on windows you can use is to call upon the windows taskkill program to kill the child process for you. Not quite as nice but it works. When you spawn the child you get a ProcessID (pid) stored in the child object returned when spawning, you can use with taskkill to kill the process.

var spawn = require('child_process').spawn;    
spawn("taskkill", ["/pid", child.pid, '/f', '/t']);

THANK YOU! I spent literally the whole day trying to figure out how to kill off a process and this was the only thing that worked. I tried literally every other option. Thank you so much!

node.js - Why can I not kill my child process in nodejs on windows? - ...

node.js winapi process signals
Rectangle 27 20

This still doesn't work for me with the current accepted answer. A work around on windows you can use is to call upon the windows taskkill program to kill the child process for you. Not quite as nice but it works. When you spawn the child you get a ProcessID (pid) stored in the child object returned when spawning, you can use with taskkill to kill the process.

var spawn = require('child_process').spawn;    
spawn("taskkill", ["/pid", child.pid, '/f', '/t']);

THANK YOU! I spent literally the whole day trying to figure out how to kill off a process and this was the only thing that worked. I tried literally every other option. Thank you so much!

node.js - Why can I not kill my child process in nodejs on windows? - ...

node.js winapi process signals
Rectangle 27 12

The standard way is to use the system functions fork (to duplicate the current process), exec (to replace the current process by an executable file), and kill (to send a signal to a process to terminate it).

pid = fork do
  # this code is run in the child process
  # you can do anything here, like changing current directory or reopening STDOUT
  exec "/path/to/executable"
end

# this code is run in the parent process
# do your stuffs

# kill it (other signals than TERM may be used, depending on the program you want
# to kill. The signal KILL will always work but the process won't be allowed
# to cleanup anything)
Process.kill "TERM", pid

# you have to wait for its termination, otherwise it will become a zombie process
# (or you can use Process.detach)
Process.wait pid

This should work on any Unix like system. Windows creates process in a different way.

great. thank you very much. while trying things out i had an almost identical "solution" which almost worked, but unfornately i can't figure out where it was different. also i needed to add a sleep after forking the process to wait until the server was available. thanks!

i was able to reproduce the difference. i ran Kernel#system instead of Kernel#exec. Kernel#exec "replaces the current process by running the given external command" where Kernel#system "executes the command in a subshell".

Can the child process stay alive even if the ruby script exits?

how to controller (start/kill) a background process (server app) in ru...

ruby process webrick
Rectangle 27 1

One way is to run the program in a subshell, and communicate with the subshell through a named pipe with the read command. This way you can check the exit status of the process being run and communicate this back through the pipe.

Here's an example of timing out the yes command after 3 seconds. It gets the PID of the process using pgrep (possibly only works on Linux). There is also some problem with using a pipe in that a process opening a pipe for read will hang until it is also opened for write, and vice versa. So to prevent the read command hanging, I've "wedged" open the pipe for read with a background subshell. (Another way to prevent a freeze to open the pipe read-write, i.e. read -t 5 <>finished.pipe - however, that also may not work except with Linux.)

rm -f finished.pipe
mkfifo finished.pipe

{ yes >/dev/null; echo finished >finished.pipe ; } &
SUBSHELL=$!

# Get command PID
while : ; do
    PID=$( pgrep -P $SUBSHELL yes )
    test "$PID" = "" || break
    sleep 1
done

# Open pipe for writing
{ exec 4>finished.pipe ; while : ; do sleep 1000; done } &  

read -t 3 FINISHED <finished.pipe

if [ "$FINISHED" = finished ] ; then
  echo 'Subprocess finished'
else
  echo 'Subprocess timed out'
  kill $PID
fi

rm finished.pipe

linux - How to kill a child process after a given timeout in Bash? - S...

linux bash unix
Rectangle 27 1

One way is to run the program in a subshell, and communicate with the subshell through a named pipe with the read command. This way you can check the exit status of the process being run and communicate this back through the pipe.

Here's an example of timing out the yes command after 3 seconds. It gets the PID of the process using pgrep (possibly only works on Linux). There is also some problem with using a pipe in that a process opening a pipe for read will hang until it is also opened for write, and vice versa. So to prevent the read command hanging, I've "wedged" open the pipe for read with a background subshell. (Another way to prevent a freeze to open the pipe read-write, i.e. read -t 5 <>finished.pipe - however, that also may not work except with Linux.)

rm -f finished.pipe
mkfifo finished.pipe

{ yes >/dev/null; echo finished >finished.pipe ; } &
SUBSHELL=$!

# Get command PID
while : ; do
    PID=$( pgrep -P $SUBSHELL yes )
    test "$PID" = "" || break
    sleep 1
done

# Open pipe for writing
{ exec 4>finished.pipe ; while : ; do sleep 1000; done } &  

read -t 3 FINISHED <finished.pipe

if [ "$FINISHED" = finished ] ; then
  echo 'Subprocess finished'
else
  echo 'Subprocess timed out'
  kill $PID
fi

rm finished.pipe

linux - How to kill a child process after a given timeout in Bash? - S...

linux bash unix
Rectangle 27 9

^D (C-D) does not terminate the screen! It may possibly log out of your shell session, which closes the shell session, and thus your screen session, but that won't be the case if you are running more than one window, a foreground application, and other things.

linux - Cannot detach screen session - Stack Overflow

linux gnu-screen
Rectangle 27 9

kill(2)
#include <sys/types.h>
 #include <signal.h>

 int kill(pid_t pid, int sig);

If sig is 0, then no signal is sent, but error checking is still performed; this can be used to check for the existence of a process ID or process group ID.

So just call kill(pid, 0) on the process ID of the process that you want to check for and see if you get an error (ESRCH).

This is a good way to check if you have the process id, but he only has the process name.

@cegfault: That's true. A name doesn't really tell you "the process", though, since there may be many processes with the same name. I think the OP's question is lacking several important details.

execve(argv[0], NULL, NULL);

linux - How to find if a process is running in C? - Stack Overflow

c linux process
Rectangle 27 17

After starting each child process, you can get its id with

ID=$!

Then you can use the stored PIDs to find and kill all grandchild etc. processes as described here or here.

Just to add, if the monitors' subprocesses can spawn subsubprocesses etc., you'd need to use the technique described in the link recursively.

@David the 2nd page referred includes a solution for that.

Yes, but that second link hadn't yet appeared when I wrote my comment.

@David I thought so too. I didn't mean to counterargument or anything, sorry if it sounded like that.

Not to worry, it didn't :-) (by the way: +1)

bash - How to kill all subprocesses of shell? - Stack Overflow

bash shell process fork kill