Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
1  
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
1  
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
1  
@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
add comment

1 Answer

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.