We recently moved to AWS EC2 instances w/ the Amazon Linux distro.

On our old servers (RH Enterprise), we've been able to run a command in the background (&) and also redirect the output to a log file, such as the below:

php /path/to/script > log.txt &

However, this no longer works on AWS. log.txt is created, but is empty.

If I leave off the & so the task runs in the foreground (php /path/to/script > log.txt), it works and log.txt contains data.

What am I doing wrong here and/or what can I do to get the output of my scripts logged again?

Thanks in advance.

Here's a code sample of what is happening:

[ec2-user@ip /]$ cat logtest.php
<?php echo "It's big, it's heavy, it's wood!\n"; ?>
[ec2-user@ip /]$ php logtest.php > log.txt
[ec2-user@ip /]$ cat log.txt
It's big, it's heavy, it's wood!
[ec2-user@ip /]$ rm log.txt
[ec2-user@ip /]$ php logtest.php > log.txt &
[3] 6649
[ec2-user@ip /]$ <I waited 5 seconds and pressed enter here>
[3]+  Stopped                 php logtest.php > log.txt
[ec2-user@ip /]$ cat log.txt
[ec2-user@ip /]$ ll log.txt
-rw-rw-r-- 1 ec2-user ec2-user 0 Jan  3 00:30 log.txt
[ec2-user@ip /]$

I'm pretty sure it's related to your /etc/php.ini config or the way the php package is compiled for Amazon Linux. I tried on MacOS and it works fine too. Can you compare the one you have locally to the one you have in AWS.

Try the following on both machines. Create a script called logtest.sh

echo "It's big, it's heavy, it's wood!\n"

then run:

chmod 777 logtest.sh
./logtest.sh > log.txt &

If it works on both machines then it's definitely a php configuration issue.

  • You can also try running php -i on both machines and compare the output and see what's different on the configs. – Rico Jan 3 '14 at 1:07
  • This worked as well as Ronald's echo, so I guess it's a PHP configuration error. Now to find out what it could be... – DOOManiac Jan 3 '14 at 14:43
  • After more digging, it turns out the culprit is the readline extension, which was turned on in the copy of PHP that YUM supplies. Doh! php.net/manual/en/book.readline.php#107349 Anyone know how I can disable the extension without recompiling? It isn't in php.ini because it was compiled in... – DOOManiac Jan 3 '14 at 15:23
  • Upvote for the Ren & Stimpy reference – jcbwlkr May 6 '14 at 17:03

Try running it with nohup

nohup php /path/to/script > log.txt &
  • When I add nohup to my command, it just hangs there instead of automatically sending it to the background like & should do? Even after the process finished I was sitting there waiting on nohup until I hit CTRL+C... – DOOManiac Jan 3 '14 at 0:18
  • What is your php script doing ? – Rico Jan 3 '14 at 0:26
  • It echos "Hi\n" to the screen and quits. I'll update my question with a code sample. – DOOManiac Jan 3 '14 at 0:28
  • If I do nohup php /path/to/script > log.txt (no &) and then press CTRL+Z to suspend the job and bg to resume it, THAT works... but it's kind of a pain... – DOOManiac Jan 3 '14 at 0:35

Strange... it works for me, en amazon, but Debian.

root@domU-12-31-39-0F-31-10:/home/try$ php i.php
It's big, it's heavy, it's wood!

root@domU-12-31-39-0F-31-10:/home/try$ php i.php >loh.txt &
[1] 8928
[1]+  Done                    php i.php > loh.txt
root@domU-12-31-39-0F-31-10:/home/try$ cat loh.txt
It's big, it's heavy, it's wood!


Also you can use screen ?? man screen


can you try to do this, in the same directory where your log is ?

echo "test" > log.txt

It'll may disqualifie a file system full and read only file system options.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.