Xi Yu
Xi Yu

Reputation: 11

How to terminate the daemon initialized by setsid?

I start a process (a websocket server) using setsid command:

setsid python mod_pywebsocket/standalone.py -p 12345

But how can I stop it? I am sure it is now running just don't know how to get the pid and kill it.

Upvotes: 1

Views: 1418

Answers (1)

pelya
pelya

Reputation: 4524

You may try this hack:

setsid sh -c 'python mod_pywebsocket/standalone.py -p 12345 > /dev/null & echo $!' |
  while read PID ; do
    echo $PID
    your stuff here
  done

This will start another shell, which will start your process in background, and print it's job ID using $! operator.

You cannot write PID=$(...) here, because the command inside will not return until it closes it's output stream, so here's a dummy 'while read X' loop, which will read first line and will not wait until the command closes stdout, so you will have to do whatever you need inside that loop, there's just one line of output so it will be executed only once.

Upvotes: 1

Related Questions