Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to write a bash script which would relaunch my NodeJS server. This is how I'm launching the screen with the server:

screen -S web-server -d -m node web-server.js

which end up in this (which means it's working):

$ ps -Af | grep node
ubuntu  19504   1       0   10:20   ?       00:00:00 SCREEN -S web-server -d -m sudo node web-server.js
root    19506   19504   0   10:20   pts/2   00:00:00 sudo node web-server.js
root    19507   19506   8   10:20   pts/2   00:00:00 node web-server.js

And this is how I'm trying to kill that server:

screen -S web-server -X quit

Which actually end up in a zombie process (the server ppid changes to 1):

$ ps -Af | grep node
root    19506   1       0   10:20   ?   00:00:00 sudo node web-server.js
root    19507   19506   0   10:20   ?   00:00:00 node web-server.js

How do I kill that screen session including the running process inside?

To those of you who may concern - the reason why I need sudo in screen is that I'm running the server on port 80 for which node requires super user permissions.

share|improve this question
I don't fully get it... Why don't you kill the process holding the node application (in your case 19507) and simply remove the screen session? – Rob Jul 4 '13 at 10:53
You could as well put your server process behind a reverse proxy such as nginx which would itself listen on port 80 and forward connections to your node process listening on an unprivileged port (or even on a Unix socket etc). – kostix Jul 4 '13 at 11:45
@Rob Well, I could do something like find PID of screen, whose name is web-server in ps, then go again to ps and find all pids, whose ppid is the screen pid and finally kill all of those" Hopefully I'll not face race condition in any of those steps... Does it seem like a good idea to you? – Radek Simko Jul 4 '13 at 20:04
@kostix Good point, that might be the solution for my problem (thanks for that), but it doesn't answer original question. ;-) – Radek Simko Jul 4 '13 at 20:05
@RadekSimko Sounds like a good idea to me. Make a few example runs. – Rob Jul 5 '13 at 9:34

So this seems like one possible solution:

screen -list | grep Detached | \
awk '{ print $1 }' | awk -F. '{ if($2=="web-server") print $1 }' | \
xargs sudo pkill -P

If anyone would come up with better (more clean) solution (instead of looking for child processes of screen and killing them), I will be more than happy to accept rather that answer + upvote it.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .