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

Posted on 2006-05-18
Last Modified: 2013-12-27
I run remote bash script from my PHP script:
system("nohup /blabla/myscript '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
    LVL 27

    Expert Comment

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

    sleep 6000

    system("nohup /tmp/ &");
    print "OK";

    php /tmp/runme.php

    that works fine, runme.php exited and 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

    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

    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...
    LVL 27

    Expert Comment

    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

    web-script is written in php, it uses line: system("nohup /.../gmc '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 ""
    LVL 27

    Accepted Solution

    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 '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

    Works flawlessly. Thanks a lot!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
    My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (, discussed installing the Solaris Operating S…
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

    794 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now