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
- look log file, , create empty 1 if not exist
- open log file reading (at end avoid false messages previous invocations), watching log message indicates process has started,
- start process nohup,
- 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
Post a Comment