5

I'm planning to control some programs (like a media player and a shell) via a webapp, since the webpage die everytime the user visits it, I decided that the webapp will opens the program with setsid and then the webapp will communicate with it through pipes.

Note: I can't use nohup becouse something like nohup bash -i <fifoin >fifoout 2>&1 & automatically stops.

With setsid everything works perfectly, but I cannot kill the process since I don't know the ID of the forked process! ..So, how can I retrive the ID of the setsided process?

I tried something like

setsid bash -i <fifoin >fifoout 2>&1
kill $!
kill $$

As result, both kill don't work, I wont search the ID with ps -e becouse I can't kill all the running bash -i shell !

Antonio Ragagnin
  • 2,278
  • 4
  • 24
  • 39

3 Answers3

3

Only somewhat intricate methods using strace come to my mind.

If you can do without redirecting standard error:

read _ _ sid < <(2>&1 strace -esetsid setsid bash -i <fifoin >fifoout)
echo $sid

If you need to redirect standard error:

strace -o/tmp/filename -esetsid setsid bash -i <fifoin >fifoout 2>&1 &
sleep 1 # Hope 1 s is long enough
read _ _ sid </tmp/filename
echo $sid
Armali
  • 18,255
  • 14
  • 57
  • 171
0

You still need to background the process with &, otherwise it'll be running in the foreground blocking the following lines from even attempting to execute.

setsid bash -i <fifoin >fifoout 2>&1 &
kill $!
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 2
    Actually, it happens to me that `setsid` does not block the shell. However also using the `&` does not do the job, infact If i first execute `setsid sleep 50 &`and then `kill $!` returns `bash: kill: (21724) - No such process` – Antonio Ragagnin Apr 30 '13 at 12:33
0

A start point may be:

setsid bash -c 'echo $$; sleep 100'

It will return the proc id of sleep

Manolo Mollar
  • 51
  • 2
  • 1