bash - getting the pid of a subshell command -


i trying write init service script java program. have following in init script.

$user = awesomeuser  $program_cmd = "java -server com.test.testclass"  $program_log = "/var/log/awesome_log"  sudo -u $user nohup $program_cmd >> $program_log 2>&1 </dev/null & server_pid=$! echo $server_pid > $pidfile 

what happening getting pid of parent process want the pid of java process running within subshell.

is there anyway can structure command subshell command pid back?

thanks!

instead of

sudo -u $user nohup $program_cmd >> $program_log 2>&1 </dev/null & 

try this:

sudo -u $user bash -c "nohup $program_cmd >> $program_log 2>&1 & </dev/null; echo "'$!' 

sudo spawns child process , calls exec in it. fork-ing can avoided proper config of sudo (see sudo(1)). seems easier me set user , run shell.

this approach seems better me obtaining pid of process ps | grep | blabla avoids race condition: once shell has ran background process, correct way pid print $! variable. otherwise can pid of wrong program, or no pid @ if java terminates quickly.

by way, in order assign proper values variables, starting lines of script should be

user=awesomeuser program_cmd="java -server com.test.testclass" program_log="/var/log/awesome_log" 

instead of

$user = awesomeuser $program_cmd = "java -server com.test.testclass" $program_log = "/var/log/awesome_log" 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -