LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   I want to run php file in background on fedora terminal in background. (https://www.linuxquestions.org/questions/linux-newbie-8/i-want-to-run-php-file-in-background-on-fedora-terminal-in-background-924352/)

gauravwcities 01-17-2012 11:44 PM

I want to run php file in background on fedora terminal in background.
 
Hello,

I want to run an php file on fedora terminal in background. When I am executing the file on terminal, it shows proper output. Whereas when I am trying to run the same file on terminal in background, there is no ouput in the file & the php file gets stopped.

Please help us.

I am using php with fedora 14.

Regards,
Gaurav.

xylos 01-17-2012 11:47 PM

Can you give us more information on exactly how you are running it in the background and what the output is when you run it normally in a terminal?

gauravwcities 01-17-2012 11:51 PM

php issue..
 
I am running the below command as root user on fedora machine.

php test.php > test.txt //works

whereas when I am trying below command & it doesn't work.

php test.php > test.txt &


Gaurav,

xylos 01-17-2012 11:53 PM

Are you seeing the file test.txt being created, or is not seeing it your issue? How are you triggering the execution of the PHP script? As a first guess, if you're not seeing the file I would guess it's being dropped into the home directory of whatever account is creating it; have you tried executing the same command but with an explicit declaration of where the file should be placed, like this?

php test.php > /tmp/test.txt &

gauravwcities 01-17-2012 11:59 PM

php issue..
 
Yes I can see the test.txt is created in the same directory. However when I am trying to run the same file in background on terminal & view the test.txt by using cat command, then I am facing below error:-
Error :- "[4]+ Stopped php test.php > test.txt"

xylos 01-18-2012 12:05 AM

How exactly are you running the script? Are you firing it off as part of a window manager startup (such as the /etc/X11/gdm/PostLogin/Default script), as part of the user's shell startup (i.e. from a .bashrc file), as part of an init script (triggered by a runlevel change)... ?

gauravwcities 01-18-2012 12:11 AM

php issue..
 
Hi,
I am running the file as a part of an init script.

xylos 01-18-2012 12:21 AM

It looks like your init might be finishing execution before the called script does, forcing it to exit. Try modifying your init script to add the command "wait" immediately following the PHP script call, such as:

php test.php > test.txt &
wait

That will cause the process to wait until the test.php script finishes execution, after which the init script will continue.

xylos 01-18-2012 12:23 AM

Or to allow the entire init script to run (if there's other things that need to finish), try this:

php test.php > test.txt &
#
# MORE COMMANDS
#
# MORE COMMANDS
#
# LAST COMMAND IN SCRIPT
wait

That way everything in the init script can continue while your PHP script runs in the background, but the init won't kill it off and will wait for it to finish execution before closing out.


All times are GMT -5. The time now is 04:37 PM.