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.
Related
I have some PHP scripts that run every minute via CRON. I don't want to receive emails every time they run correctly but I do want to receive an email if they throw an error (e.g. if they use too much memory).
To run the CRON:
php myscript.php >/dev/null
This should send an email only when the result of stderr is not null, i.e. when there is a standard error.
However, PHP errors don't create a stderr, instead they are included in the stdout so no email is sent.
I have tried setting display_errors to stderr as shown in this question but it's a different use case as the scripts are not run by CRON. I have tried the following which have not worked:
including this in the script itself doesn't work because a fatal errors mean the script doesn't run:
ini_set ('display_errors', 'stderr');
including this in an .htaccess file (note PHP FPM is used) didn't work - I'm not sure why:
php_flag display_errors stderr
Changing the php.ini file is not an option because I don't want the change to affect the whole site, just a few files.
So my question is - how can the PHP errors be sent to stderr so that I can receive them via email?
The php executable takes command line options - you can either specify which php.ini it should use, or set specific config options via the -d parameter.
https://www.php.net/manual/en/features.commandline.options.php
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
I am migrating a set of php scripts from Window 2003 / PHP 5.2 to Windows 2012 R2 / PHP 5.6.7
In the scripts exec commands are used, for example to copy files. Commands could look like this:
exec ('copy "C:\ftp\suppliername\upload\*.*" c:\somefolder\ >> output.log');
You could argue that there are better ways to copy files, but I rather wouldn't like to rebuild these scripts right now.
The problem I have is that when there are no files to copy, the error "The system cannot find the file specified." is now shown, while this wan't the case on the old server.
If you just execute the copy command on the command line, the output looks like this:
The system cannot find the file specified.
0 file(s) copied.
This is the same as what gets written to output.log
So apparently the "0 file(s) copied" is getting suppressed somewhere, but not the error.
So my question is, how do I get rid of the error on the commandline? I thought it would be some configuration in php.ini, but after comparing the php.ini from the old and the new server, I couldn't find any essential differences.
I've tried a few things to suppress the error, but with no success:
Adding a # to the exec command
Adding ob_start() and ob_end_clean() before and after the command
Edit: please do not flag as a duplicate. I did see that question before asking my question. the answers given there do not solve my issue. The main question is, why was it working before, you would think that it should still be possible to get it working the same way without modifications to the scripts.
Try adding "2>&1" to your command.
exec ('copy "C:\ftp\suppliername\upload\*.*" c:\somefolder\ >> output.log 2>&1');
This should redirect the error output to output.log
I suppose that this string is being printed to stderr.
You could try to replace your STDERR as described in the post https://stackoverflow.com/a/3823015/146003.
The essential part of the post consists of the lines:
fclose(STDERR);
(...)
$STDERR = fopen('error.log', 'wb');
You can then get rid of error.log. Although I didn't try, you could try to open NIL (see https://stackoverflow.com/a/313115/146003)
First of all, you are redirecting the output of the copy command to a text file, so it will not be ouput to console.
Do this way instead:
exec ('copy "C:\ftp\suppliername\upload\*.*" c:\somefolder\', $output);
var_dump ($output);
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.
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.