php script output from another terminal - php

I have a php (cli) script on one terminal. It is constantly echoing the mysql calls etc. Is there any way I can see that output from another terminal, so that I can monitor the progress of that script?
Thanks

You can
use a terminal multiplexer or
modify your PHP script to write all output to a log file, or
pipe stdout and/or stderr to a log file (ex: php myscript.php > output.log). If you use a log file, you can monitor it in real time with tail -f output.log.

Related

How do I log every output from a PHP script from the command line?

I'm running a PHP script and want to log all output from the program to a single log file via the command line. This is what I'm doing now when I execute the script.
php generate_production_persons.php 1 10000 2>&1 > log
Error/Notice/Etc. messages don't redirect to my log file. They still just output to the screen where the script is running.
I don't want to log from within my script. I just want to be able to write all of this to a log file whenever I run a PHP script from the command line. I want the output in the log file to look exactly as it looks from the CLI where it's running had I not redirected the output to a log file.
I think this will do the trick
php generate_production_persons.php 1 10000 > everything.log 2>&1

View output from a PHP script while it is running and log it to a file, and the script takes human input from STDIN

I have a php script I am running on the command line. It takes ~10 hours or so to complete, so I have it running on a screen so I can detach it, and check its progress throughout its run. I want to also log its output to a file. One solution is to run the script from a screen with
[chiliNUT#server ~]# php myscript.php > log.txt
and then view the live output with
[chiliNUT#server ~]# tail -f ./log.txt
But the only problem is, at points the script requires input from the user via STDIN, so I'm stumped at that part. Typically, the screened script just waits nicely for me to check in and provide input when needed.
How would I be able to
Log the script to a file
And be able to view live output as it is running
And provide input to STDIN when it requires it?
I do not want to modify the original script in any way.
Using php 5.4 and Centos 6.4 Final
Sounds like you might want tee. For example:
php myscript.php | tee log.txt
Basically, tee copies its standard input to the file given on the command line. So you see all the output scrolling by as normal, plus you get the redirection. If you want to append to the log file (rather than overwriting upon tee start), pass the -a flag.
linux tee command
php myscript.php | tee log.txt

PHP exec with nohup

I currently use nohup to run a long php script and redirect the live results to a file using this command
nohup php long_file.php >logs 2>&1 &
so i just go and visit logs file continuously to see the results
Now i want to do the exact same thing using another php file to execute the above command
i tried the above command with php exec and the redirect output doesn't seem to be working,
I know i can just retrive the output using php and store it using any file write function but the thing is .. the output is too long thats why i keep it running on server's background
a similar question :
Shell_exec php with nohup, but it had no answer
any solution ?
Please try with -q
nohup php -q long_file.php >logs 2>&1 &
http://ubuntuforums.org/showthread.php?t=977332
Did you try passthru instead of exec?
You are redirecting STDOUT to a file by using >
This will truncate the file each time the script is run. If two scripts simultaneously redirect their output to the same file, the one started last will truncate the output from the first script.
If you really want to properly append to a log file with multiple concurrent running scripts, consider using >> to avoid having the log file truncated.
Side effect however is that the log file never truncates and keeps expanding, so if the file gets really large you can consider including it in a logrotate scheme.

Capturing and displaying exec/shell_exec periodic output?

You can easily use exec() or shell_exec() to execute a system command, like ls -l /var/www/mysite and then output the result of the command.
How would one execute and display the result of a command that periodically prints information out to the console?
You have a simply Python script. If you run the script from the console, it simply prints out some type of information to the console every 10 seconds forever until you force-quit the script.
How could use PHP to execute this python command and somehow capture and stream the output into the browser in realtime? Is something like this possible? I'm thinking there would have to be some sort of Ajax involved but I'm not sure.
I tried doing something like this:
python myscript.py > output.txt
And then I was planning on maybe using Ajax to periodically tail or cat the content of the output.txt and display in the browser. But output.txt doesn't appear to have any content added to it until after the script has been force-quit.
You don't see any output to output.txt because it's being buffered. For python there's an option to make it line-buffered. From the manpage:
-u Force the binary I/O layers of stdin, stdout and stderr to be unbuffered. The text I/O layer will still be
line-buffered.
So your command would then become:
python -u myscript.py > output.txt
For PHP the flush function should help you.

bash exec stderror stdout to normal php error_log

Hi I'm running a PHP script via command line from a bash script. I am able to get the PHP errors appended to a single file with 2>> but I was wondering if I could send the errors to the standard PHP error_log file.
exec nohup php $PHP_SCRIPT_PATH 2>> $LOG_PATH & EPID=$!
Also when I do try to write to the php error_log file, I get write permission.
Thanks,
Steve
Even if you get your permission problem managed this will not work, because you write from two processes to the same file. This will most likely produce garbage in the error_log.
You have to prevent PHP writing to the error_log. Instead you have to configure PHP to write to STDERR. And than you can redirect the output to a single file.

Categories