LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-03-2013, 04:51 PM   #1
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,001

Rep: Reputation: 67
Need to run PHP script in the background while redirecting STDOUT and STDERR to a fil


I have been checking the advice here but I've been unable to determine how I might run my script in the background such that all script output (STDERR and STDOUT) ends up in a file. I've tried this:
Code:
php /path/to/script.php 2>&1 > /path/to/file.txt
This seems to result in the script running in the background, but file.txt remains empty and the script -- which is supposed to create some other files -- doesn't appear to do anything. I can see it listed when I do "ps -aux" but it doesn't seem to do any work.

I then have to kill the script with a kill -9.

What am I doing wrong?

EDIT: my shell is bash.

Last edited by sneakyimp; 01-03-2013 at 04:53 PM.
 
Old 01-03-2013, 05:54 PM   #2
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,158
Blog Entries: 10

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
try
Code:
php -f /path/to/script.php 2>&1 > /path/to/file.txt
 
Old 01-03-2013, 06:48 PM   #3
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,001

Original Poster
Rep: Reputation: 67
Thanks for your response, but the command you offer does not run in the background -- I have to wait until it completes to do anything else.

I tried it with an ampersand (&) after it and that doesn't work either. The command returns instantly but almost immediately after (e.g., if i hit the return key), I get a notice that it has stopped:
Code:
$ php -f /path/to/script.php 2>&1 > /path/to/file.txt &
[1] 31003
$ 

[1]+  Stopped                 php -f /path/to/script.php 2>&1 > /path/to/file.txt
however, if I then do a ps -aux | grep php command then it looks like the process is still running:
Code:
505      31003  0.0  1.6 281852  8260 pts/0    T    17:40   0:00 php -f /path/to/script.php
505      31078  0.0  0.1 103232   824 pts/0    S+   17:42   0:00 grep php
However, nothing at all is being written to the output file:
Code:
tail: /path/to/file.txt: file truncated
 
Old 01-03-2013, 06:59 PM   #4
Celyr
Member
 
Registered: Mar 2012
Location: Italy
Distribution: Slackware+Debian
Posts: 319

Rep: Reputation: 80
Code:
$ php -f prova.php 2>&1 > file.txt&
[1] 3103
$
[1]+  Done                    php -f prova.php 2>&1 > file.txt
$
$ cat prova.php
<?php

 echo phpinfo();

?>
$ cat file.txt
(long truncated output)
$
Here is working as expected.
can you please post the full output and eventually try with another php script ?
(T in process state means stopped so it's not running)
Have you tried to issue a
Code:
$ bg

Last edited by Celyr; 01-03-2013 at 07:05 PM.
 
Old 01-03-2013, 07:06 PM   #5
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,001

Original Poster
Rep: Reputation: 67
Quote:
Originally Posted by Celyr View Post
Code:
$ php -f prova.php 2>&1 > file.txt&
[1] 3103
$
[1]+  Done                    php -f prova.php 2>&1 > file.txt
$
$ cat prova.php
<?php

 echo phpinfo();

?>
$ cat file.txt
(long truncated output)
$
Here is working as expected
That is not how my machine is behaving. My script will take approximately an hour to run. I believe one difference is that phpinfo returns almost immediately. Unfortunately, rather than running to completion, it STOPS:
Code:
[1]+  Stopped                 php -f /path/to/script.php 2>&1 > /path/to/file.txt
yours doesn't stop, it says "DONE":
Code:
[1]+  Done                    php -f prova.php 2>&1 > file.txt
And you only run your cat command once it has completed. My hope is that I might monitor file.txt during the length of a very long script. Perhaps you could try this php script as prova.php instead:
PHP Code:
<?php
for($i=0$i<3600$i++) {
  echo 
"iteration $i\n";
  
sleep(1);
}
?>
Perhaps then you will get similar behavior?
 
Old 01-04-2013, 01:13 PM   #6
Celyr
Member
 
Registered: Mar 2012
Location: Italy
Distribution: Slackware+Debian
Posts: 319

Rep: Reputation: 80
Code:
$ php -f prova.php 2>&1 > file.txt&
[1] 3579
$ jobs
[1]+  Running                 php -f prova.php 2>&1 > file.txt &
$ cat prova.php
<?php
for($i=0; $i<3600; $i++) {
  echo "iteration $i\n";
  sleep(1);
}
?>
$ ps aux | grep php
xxx      3579  0.6  0.3 231648 14468 pts/0    S    19:09   0:00 php -f prova.php
xxx      3582  0.0  0.0   7076   976 pts/0    S+   19:10   0:00 grep php
$ php --version
PHP 5.4.7 (cli) (built: Sep 14 2012 15:20:54)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
Still working.
and file.txt is updated every second as expected. Is this example working for you ?

Last edited by Celyr; 01-04-2013 at 01:17 PM.
 
Old 01-04-2013, 01:33 PM   #7
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Instead of searching "php" process, search process for script as:-
Code:
ps -aef | grep script.php
And kill that process using:-
Code:
kill -9 <pid>
OR 
kill <pid>
Note: This is because your script is still running in background and updating the file, so you have to stop the script, not php.
 
Old 01-04-2013, 02:26 PM   #8
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,001

Original Poster
Rep: Reputation: 67
I tried with the exact same script and the behavior is different:
Code:
sneakyimp@sneakyimp-ubuntu-64:~/$ php -f /home/sneakyimp/prova.php 2>&1 > /home/sneakyimp/file.txt&
[2] 19872
sneakyimp@sneakyimp-ubuntu-64:~/$

[2]+  Stopped                 php -f /home/sneakyimp/prova.php 2>&1 > /home/sneakyimp/file.txt
sneakyimp@sneakyimp-ubuntu-64:~/$ ls -al
total 12
drwxrwxr-x  2 sneakyimp sneakyimp 4096 2013-01-04 11:19 .
drwxr-xr-x 12 sneakyimp sneakyimp 4096 2013-01-04 11:14 ..
-rw-rw-r--  1 sneakyimp sneakyimp    0 2013-01-04 11:19 file.txt
-rw-rw-r--  1 sneakyimp sneakyimp   76 2013-01-04 11:15 prova.php
As soon as I hit <enter> or attempt any new command, I get the STOPPED notification. There still appears to be a process running though. I tried the above twice and so there are two instances of the process running:
Code:
sneakyimp@sneakyimp-ubuntu-64:~$ ps -aux | grep "prova.php"
Warning: bad ps syntax, perhaps a bogus '-'? See http://procps.sf.net/faq.html
sneakyimp    19869  0.0  0.1 240748 12312 pts/3    T    11:19   0:00 php -f /home/sneakyimp/prova.php
sneakyimp    19872  0.0  0.1 240748 12312 pts/3    T    11:19   0:00 php -f /home/sneakyimp/prova.php
sneakyimp    19884  0.0  0.0  14576   896 pts/3    S+   11:24   0:00 grep --color=auto prova.php
These process do create the file file.txt as you can see from the ls -l output above, but that file never fills up, never has anything in it.

I'm using ubuntu 11.10:
Code:
$ cat /etc/issue
Ubuntu 11.10 \n \l
What the heck??
 
Old 01-04-2013, 02:39 PM   #9
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
(1) If you want to stop the background process i.e. script, then kill PIDs 19869, 19872, and 19884.
(2) In your command, reason of empty file is improper re-direction i.e. 2>&1 >:
Code:
php -f /home/sneakyimp/prova.php 2>&1 > /home/sneakyimp/file.txt
Just remove error and output re-direction i.e. 2>&1 and instead use simple re-direction i.e. ">", as:-
Code:
php -f /home/sneakyimp/prova.php > /home/sneakyimp/file.txt &
Then check the file /home/sneakyimp/file.txt, it will contain all output that generated before you kill the script.

Last edited by shivaa; 01-04-2013 at 02:44 PM. Reason: Typo
 
Old 01-04-2013, 02:51 PM   #10
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,001

Original Poster
Rep: Reputation: 67
The problem is the same:
Code:
sneakyimp@sneakyimp-ubuntu-64:~$ php -f /home/sneakyimp/prova.php > /home/sneakyimp/file.txt &
[1] 19928
sneakyimp@sneakyimp-ubuntu-64:~$

[1]+  Stopped                 php -f /home/sneakyimp/prova.php > /home/sneakyimp/file.txt
sneakyimp@sneakyimp-ubuntu-64:~$
I have taken care to kill the previous processes each time I attempt this.
 
Old 01-04-2013, 06:55 PM   #11
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,158
Blog Entries: 10

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
It may be helpful to post
/home/sneakyimp/prova.php, sanitized if necessary...
 
Old 01-04-2013, 06:59 PM   #12
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,001

Original Poster
Rep: Reputation: 67
Quote:
Originally Posted by Habitual View Post
It may be helpful to post
/home/sneakyimp/prova.php, sanitized if necessary...
Code:
<?php
for($i=0; $i<3600; $i++) {
  echo "iteration $i\n";
  sleep(1);
}
?>
 
Old 01-05-2013, 03:23 PM   #13
Celyr
Member
 
Registered: Mar 2012
Location: Italy
Distribution: Slackware+Debian
Posts: 319

Rep: Reputation: 80
Code:
php --version
?
 
Old 01-05-2013, 03:25 PM   #14
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,001

Original Poster
Rep: Reputation: 67
Code:
$ php --version
PHP 5.3.6-13ubuntu3.9 with Suhosin-Patch (cli) (built: Sep 12 2012 19:00:27) 
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
    with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans
    with Zend Debugger v5.3, Copyright (c) 1999-2010, by Zend Technologies
I don't think that has anything to do with it, does it?
 
Old 01-05-2013, 04:16 PM   #15
Celyr
Member
 
Registered: Mar 2012
Location: Italy
Distribution: Slackware+Debian
Posts: 319

Rep: Reputation: 80
I think it's the only explantion, ubuntu messed up (also) with php.
Workaround:
Code:
$screen
$ php -f file.php 2>&1 > file.txt
^C
$
continue your work...
$
you can get back to the screen with screen -r
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Weird thing in redirecting stdout and stderr brubizu Linux - Software 2 07-01-2010 12:09 PM
redirecting BASH script stdout/stderr from the script itself Hewson Linux - General 4 04-18-2008 04:32 PM
Redirecting stdout, stderr to pty0? Rostfrei Linux - General 4 03-20-2007 04:15 AM
redirecting stdout to /dev/null and stderr to stdout? Thinking Programming 1 05-18-2006 03:36 AM
redirecting stdout and stderr to a file Avatar33 Programming 4 03-12-2005 08:55 AM


All times are GMT -5. The time now is 10:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration