Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Must raise my old question again - script seems NOT to run in background. What could be wrong?

Posted on 2006-05-18
Medium Priority
Last Modified: 2013-12-27
I run remote bash script from my PHP script:
system("nohup /blabla/myscript send@results.here 'Mail subject' results@from.this_person http://www/result_folder /blabla/slow_script param1 param2 param3 param4 &")

'myscript' waits for result, generated by 'slow_script' (it's a link to file) and sends it to the given e-mail address. If slow_script get data, that does not require computation (wrong input data, etc) - everything works ok.
BUT if it works slow - I can see, that BACKGROUND doesn't work here, because PHP script 'hangs' - i.e. waits until 'myscript' completes execution :(

For the information: 'myscript' contains such string: filename=`${*:5}`, filename in the given case is the result of 'slow_script'. Maybe it's wrong to write so? Otherwise I can't see the problem.

Please, help.
Question by:Nafigator
  • 4
  • 3
LVL 27

Expert Comment

ID: 16714409
Hello, Nafigator, I remember your question.
I've just tested very simple script:

sleep 6000

system("nohup /tmp/slow.sh &");
print "OK";

php /tmp/runme.php

that works fine, runme.php exited and slow.sh is  still running.
I see no problems, php shouldn't 'hang' on that command, but myscript may hang as long as it likes.

You may have a problem, if you are using stdin or stdout inside your scripts,  stdin/out are the same as in php script.

Also I see no problems with `${*:5}`. It may be problem if any of 'params' have spaces.

How to debug? Add '-x' flag to your myscript header:
#!/bin/bash -x

then read nohup.out file


Author Comment

ID: 16723869
Arty, thanks a lot for advice, I suppose that answer is very close, but still not here. I now have nohup.out file, while my web script 'hangs' on execution and...
nohup says that

/blabla/slow_script: line XX: 16465 Terminated
and next line gives such line:
$GMC_DIR/gmc -n1$file1 $ch1_arg -n2$file2 $ch2_arg >out_table 2>err

where gmc === "myscript" from my previous letter

I wonder, why slow script was actually terminated... It creates folder inside of resulf folder (that has write access set for all users), where it places the results, and even writes one zero-length file ("out_table") there now. But it must create a set of result files there, filled with data (if input files are "valid" - and they are).

Author Comment

ID: 16724939
Sorry, I misinformed you.
nohup.out was an old one - it was created when i tried to run my PHP line from ssh console yesterday.
So, after I run this line I got message saying that process has been sent to background, but I didn't know how to get back to command-line again, so I CTRL-C'ed it. That's where the message '...Terminated' is from.

About testing today -
ok, later, after out_table, all rest needed files have been created... And link has been received. But I had to terminate my web-script as it hung up. That means, that execution is NOT performed in background, everything the rest is allright...
BTW - I deleted nohup.out, run web script again now - it hangs as usually, but new nohup.out is not being created, although I added -x flag to myscript...
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 27

Expert Comment

ID: 16731075
But I had to terminate my web-script as it hung up.

What is a 'web-script':
- php?
- that script that was called from php?
- slow_script?

nohup.out is created in current working directory if you have permissions to write to. When you go to different directory and run your php script, you may not see nohup.out there.

Author Comment

ID: 16731895
web-script is written in php, it uses line: system("nohup /.../gmc send@results.here 'Mail subject' results@from.this_person http://www/result_folder /blabla/slow_script param1 param2 param3 param4 &")
to run my bash script "gmc" in background mode.

"slow_script" is bash script or perl script (sorry, not sure. It's written by another person and seems to work correct itself).
It takes 4 arguments (param1...param4) to transfer them to another thing (Java or something) - I may investigate, what's exactly there: I just thought that problem was on my side and thus was never interested - and sends me the result, that is URL to result file.

"gmc" script calls slow_script with param1...param4 by:
Then it calls "sendmailer" bash script (written by me - it's almost a usual sendmail, but takes parameters of sender mail, addressee mail, body in HTML format), that sends "http://www/result_folder/filename" URL to the "send@results.here"
LVL 27

Accepted Solution

Nopius earned 1000 total points
ID: 16732187
I found an error. Your php keeps running until there are open file descriptors passed to your shell scripts.
Try this sintax:
system("nohup /blabla/myscript send@results.here 'Mail subject' results@from.this_person http://www/result_folder /blabla/slow_script param1 param2 param3 param4 0<&- 1<&- 2<&- &")

Now your shell script will be called without stdin/strout/stderr, however you will not see nohup.out there.

Author Comment

ID: 16744814
Works flawlessly. Thanks a lot!

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question