2

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.

5
  • 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, 2013 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, 2013 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? Jul 4, 2013 at 20:04
  • @kostix Good point, that might be the solution for my problem (thanks for that), but it doesn't answer original question. ;-) Jul 4, 2013 at 20:05
  • @RadekSimko Sounds like a good idea to me. Make a few example runs.
    – Rob
    Jul 5, 2013 at 9:34

1 Answer 1

0

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.

You must log in to answer this question.

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