LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Bash script hangs upon starting another script in the background (https://www.linuxquestions.org/questions/linux-software-2/bash-script-hangs-upon-starting-another-script-in-the-background-500761/)

masea2 11-11-2006 11:16 PM

Bash script hangs upon starting another script in the background
 
Hi,

I am trying to create a program that I will use to start a vendor application, but once I kick off that application my script just hangs...

here is what the relevant part of my script looks like:
pid=`/usr/bin/nohup./hawkagent_$bwdom &`
exit 0

When executing this with the -x option I see my script is just hanging on this line:
++ ./hawkagent_BWDEXPMGMT

Any ideas?

blackhole54 11-12-2006 12:55 AM

I think it has to do with the fact that you are assigning its output to a variable (assuming those are backticks in your post). A quickie experiment I just ran (using ls and sleep) seems to indicate that in this case, even though the process does get run in the background, the script won't proceed until it can assign the value.

matthewg42 11-12-2006 06:12 AM

Looks like you're working with some batch system. BWDEXPMGMT looks like a maestro job name to me! MGMT = management? EXP = export? BWD? Hmm. I've spent way too long in the past trying to work out the purpose of some undocumented job from the job name. *sigh*

ANYhow... There are a few things which are wrong here.
  • I think you want a space between /usr/bin/nohup and the command name "./hawkagent_$bwdom". If this is a typo, please please please - when putting failing example code into the forums copy-paste it. If that's not possible for some reason, at least copy it carefully.
  • You can't do backgrounding with `command substitution`. Well, you can but only in order to run multiple commands within that command substitution. The `` statement will wait until all processes spawned by the `` have completed before returning. This makes sense because the `` evaluates to the output of the commands in the back-quotes, so it can't know what the output is until the command is complete.
If you want to get the PID of the backgrounded process, try this. Note it will only work if you only have one backgrounded process from the script:
Code:

nohup /usr/bin/nohup ./hawkagent_$bwdom &
pid=`jobs -p`
...

By the way, when trying to make daemon processes, please consider following the advice in this document. There's a right way of making daemon processes, and "nohup command &" isn't it!

masea2 11-12-2006 10:51 PM

Found a solution
 
The way to resolve it is as follows:

(./hawkagent_BWDEXPMGMT > $Logs/Hawk.log 2>&1 &) &

Double backgrounding of process seems to work.

matthewg42 11-13-2006 05:18 AM

Quote:

Originally Posted by masea2
The way to resolve it is as follows:

(./hawkagent_BWDEXPMGMT > $Logs/Hawk.log 2>&1 &) &

Double backgrounding of process seems to work.

That's really ugly, and you don't have the PID of the backgrounded process. Also, it is not a proper daemon process - the process still has a filehandle open to the pwd (this should be changed to /) etc...


All times are GMT -5. The time now is 04:36 AM.