shell - Starting unix background process maintaining the order -


i have script starts many processes in background , uses nohup make sure these processes keeps on running -

nohup "./$__service_script1.pl" $__service_args < /dev/null > /var/log/$__service_name.log 2>&1 & 

the problem important me make sure processes starts in order of invocation. way wait until process has started before attempting start process?

i tried wait, waits till process finished, want make sure process has started. simplest solution put sleep few seconds in between processes, there better solution ?

thanks

it depends on mean "definitely started". if mean fork(2) has completed , new process exists, each process started time nohup returns. new process has been created.

the problem running there no guarantee how long nohup'ed process gets run before shell returns. when process start "definitely started" depends on process initialization. if not have source of applications or not able modify them other reason, limited looking @ output. many daemons output log message @ various stages of initialization. can modify script

  1. look log file, , create empty 1 if not exist
  2. open log file reading (at end avoid false messages previous invocations), watching log message indicates process has started,
  3. start process nohup,
  4. wait log file watcher

in bash, might work (this code untested):

log=<path log file> msg=<message service prints when ready> svc=<path service>  # create log file if not exist if [ ! -f "$log" ] ;      echo > "$log" fi  # watch message appear on single line in log file  tail -0 -f "$log" | egrep "$msg" | head -1 & ready_pid=$!  # start service nohup "$svc"  < /dev/null >> "$log" 2>&1 &  # wait message wait $ready_pid 

you want start watching log file before forking service, because otherwise, message might go in log before script starting service can attach log file.


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -