Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the ID of a process executed via setsid

Tags:

bash

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 !

like image 611
Antonio Ragagnin Avatar asked Oct 21 '22 11:10

Antonio Ragagnin


1 Answers

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
like image 184
Armali Avatar answered Nov 01 '22 12:11

Armali