I am attempting to backround a php script since it will take more than a minute to complete and I do not want the user to wait.
my exec command is as follows:
exec ('php -f path/to/file.php > path/to/output.log 2>&1 &');
first of all the script in the file didnt do what i programmed it to do however, the output file still recieves this output:
X-Powered-By: PHP/5.6.24
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
Pragma: no-cache
Content-Type: text/html; charset=UTF-8
Link: <https://example.com/wp-json/>;
rel="https://api.w.org/"
Link: <https://example.com/?p=687>; rel=shortlink
....
This output is not at all what my script is supposed to make, it makes no sense to me.
the rest of the output is a html document with differnet links to my website and such.
can anyone clue me into why this is happening and not simply running the script?
BTW
I have used different commands like /usr/bin/php with the same affect
UPDATE
I noticed that after changing the first path/to/file.php paremeter to gibberish i.e.
exec ('php -f asdfjaskldfj > path/to/output.log 2>&1 &');
that the output remains the same, not sure what this means but i believe it to be noteworthy
After must trial and error I found that
usr/bin/php
pointed to a php command that only outputted documentation on my current server, and when I changed it to
usr/bin/php5
it worked. Very hard to find documentation on the linux php command, and I still have yet to find anyone else with the same problem, but it has been resolved nonetheless.
Related
I've got a bash script which runs 4 commands and redirects to a file. This script works fine when I run it myself from the CLI on my server. It does not work when I let cron run the script.
my file looks something like this:
#!/bin/bash
command > file.csv
command >> file.csv
command >> file.csv
command >> file.csv
I get the expected result when I run it myself but when cron runs it I get this:
X-Powered-By: PHP/7.1.31
Content-type: text/html; charset=UTF-8
X-Powered-By: PHP/7.1.31
Content-type: text/html; charset=UTF-8
X-Powered-By: PHP/7.1.31
Content-type: text/html; charset=UTF-8
X-Powered-By: PHP/7.1.31
Content-type: text/html; charset=UTF-8
I execute the cron job like this:
/usr/bin/php -q /home/user/rest/of/path/script.sh >/dev/null
can someone please help? Driving me mad....
The solution was specifying a PATH property in the bash script.
I echoed the $PATH variable in my shell and copied this into the script and this seemed to solve things.
I am using HMVC with codeigntier and try to run a cron job.
I am using this for the cron:
php /home/onlblcm/dev.mytranslator.com/index.php mytranslator cron cronTranslations > aaa.txt 2>&1
When running this from cron job, in aaa.txt file I get
Set-Cookie: ci_session=e56ef3d7d05652937b3269852d8b36eaeebba9a2; expires=Wed, 05-Sep-2018 13:39:26 GMT; Max-Age=7200; path=/; HttpOnly
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Refresh:0;url=http/http/user/login
Content-type: text/html; charset=UTF-8
So it's super strange for me.
If I run the exact same command
php /home/onlblcm/dev.mytranslator.com/index.php mytranslator cron cronTranslations > aaa.txt 2>&1
From Putty (ssh) I get what I need. The file runs good. I get the desired output.
So it's the exact same command, but with CRON it fails.
I checked to see in SSH, and I am using user onlblcm
I checked the cron, and it's using the same user: onlblcm
So what can be the problem?
EDIT1:
I added -f in front of the index.php and I now get the error output:
A PHP Error was encountered
Severity: Warning
Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time
Filename: Session/Session.php
Line Number: 284
<p>Backtrace:</p>
<p style="margin-left:10px">
File: /home/onlblcm/dev.mytranslator.com/application/third_party/MX/Loader.php<br />
Line: 173<br />
Function: _ci_load_library </p>
<p style="margin-left:10px">
File: /home/onlblcm/dev.mytranslator.com/application/third_party/MX/Loader.php<br />
Line: 192<br />
Function: library </p>
<p style="margin-left:10px">
File: /home/onlblcm/dev.mytranslator.com/application/third_party/MX/Loader.php<br />
Line: 153<br />
Function: libraries </p>
<p style="margin-left:10px">
File: /home/onlblcm/dev.mytranslator.com/application/third_party/MX/Loader.php<br />
Line: 65<br />
Function: initialize </p>
<p style="margin-left:10px">
File: /home/onlblcm/dev.mytranslator.com/application/modules/admin/controllers/Admin.php<br />
Line: 8<br />
Function: __construct </p>
As you can see, it's going to: /home/onlblcm/dev.mytranslator.com/application/modules/admin/controllers/Admin.php
But that has no sense, since I am not calling my admin controller.
Again, if I call the command from SSH it's working, from CRON is messed up.
EDIT 2:
I added to cron jobs this command:
php -f /home/onlblcm/dev.blasteronline.com/index.php > az.txt 2>&1
I get the exact same thing (error) when calling from Cron Job.
If I call it from SSH I get no output.
if I call from cron, I get the error like above.
EDIT 3:
I now managed to get the same error when using Putty/sssh.
If I run the command (ssh) with
php ....
It runs good
If I run the command (ssh) with:
/usr/bin/php
I get the same error like in cron.
With cron I tested with php ... and /usr/bin/php ... and I get the same error.
So somehow php is different than /usr/bin/php .
But I outputed the phpinfo and it's the same version.
When I use php info (via ssh) and use php I get a 32KB file with information (not html formated). When I use /usr/bin/php calling a file with php info, I get a file with 94K info (it is html formatted).
What is happening? Any ideas?
EDIT 3: SOLVED
It seems somehow the cron was calling a different PHP (although same version).
From Putty (ssh) I did a "whereis php"
I found 2 folder: /usr/bin/php AND /usr/local/bin/php
It seems calling simple php or /usr/bin/php FAILED the cron
When calling the cron with /usr/local/bin/php IT WORKS
Thanks
server: Red Hat 4.4.7-18
When i launch php script with cli:
php myscript.php >> my.log
before the output 'echo', there is 2 more lines in the my.log file:
X-Powered-By: PHP/5.6.33 Content-type: text/html; charset=UTF-8
How to prevent this output ?
Tanks a lot and sory for my english.
I'm using this script to post some date using curl in a post.sh file
#!/bin/sh
var1=`base64 javascript_100.file`
# post it 3 times
for i in `seq 1 3`; do
/usr/bin/curl -i --verbose -d "jobTexterea=$var1" http://my_server_address/target_page.php
done
I don't have any problems executing the post.sh file using my Shell however when i invoke it inside my php script using shell_exec('sh /path_to_post_file/post.sh') I'm getting this error from my var_dump(shell_exec('sh /path_to_post_file/post.sh'));.
Error: () HTTP/1.1 302 Found Date: Sat, 24 Sep 2016 15:14:38 GMT Server: Apache/2.4.23 (Fedora) OpenSSL/1.0.2h-fips PHP/5.6.25 mod_perl/2.0.9 Perl/v5.22.2 X-Powered-By: PHP/5.6.25 Location: http://my_server_ ddress/ Content-Length: 401 Content-Type: text/html; charset=UTF-8 A MySQL error has occurred.
Your Query: INSERT INTO jobsNum_pro_batch (job_batch_id , jobs_in_batch , job_type ) VALUES ( '26' ,'1' ,'JavaScript' )
The code in the post.sh file posts the information to the target_page.php wich puts all the content into different tables in a database. The thing is that no information is being uploaded into the database with running shell_exec('sh /path_to_post_file/post.sh') from a PHP page. However, the command itself runs without errors, I checked it with
<?php
if (shell_exec('sh /path_to_post_file/post.sh')){
echo "<b> shell_exec was executed </b><br>";
var_dump(shell_exec('sh /path_to_post_file/post.sh'));
} else {
echo "<b> shell_exec was not executed </b><br>";
}
?>
I'm getting the message shell_exec was executed
Has anyone an idea what went wrong?
This did the trick! Thanks to #Jean-FrançoisFabre and a small change to his code and problem is gone!
#!/bin/sh
progdir=$(dirname $0)
var1=`base64 $progdir/javascript_100.file`
# post it 3 times
for i in `seq 1 3`; do
/usr/bin/curl -i --verbose -d "jobTexterea=$var1" http://my_server_address/target_page.php
done
Running nginx 1.9.* / PHP 7.0.* (but exact same behavior in 5.6.* also)
Attempting to gracefully stop a PHP-FPM / nginx combo for node shutdown during maintenance. To do this, I'm sending the SIGQUIT to php-fpm, which should provide a graceful shutdown.
To test this, I made a dumb script
<?php sleep(5); echo 'done';
Tested locally with the following curl
curl -I x.x.x.x:8080
Which normally produces the output:
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 12 Apr 2016 04:48:00 GMT
Content-Type: text/html; charset=UTF-8
Connection: close
Desired: in the middle of any in-flight request, when a graceful shutdown is requested, the current requests should finish, but any additional requests should fail.
Unfortunately, when I try to trigger this behavior, by sending a SIGQUIT (http://manpages.ubuntu.com/manpages/precise/man8/php5-fpm.8.html) to the PHP-FPM master process:
kill -s SIGQUIT $FPMPID
The connection immediately drops, resulting in an ngnix 502
HTTP/1.1 502 Bad Gateway
Server: nginx
Date: Tue, 12 Apr 2016 04:48:07 GMT
Content-Type: text/html
Content-Length: 166
Connection: close
Any advice? I would love to make this piece of the system as seamless as possible. Thanks!
After struggling with this same situation for a while, I believe I've found the magical config setting to make child processes finish handling requests before dying.
http://php.net/manual/en/install.fpm.configuration.php#process-control-timeout
process_control_timeout
Time limit for child processes to wait for a reaction on signals from master
Basically, by setting this to something like 10s, the child process will wait that long, while handling existing requests before quitting.
Unfortunately, it seems that the php-fpm master process exits immediately, so, inspired by the code here, I wrote a wrapper script:
#!/bin/bash
PHP_FPM_PID='/php-fpm.pid'
wait_for_pid () {
try=0
while test $try -lt 35 ; do
if [ ! -f "$1" ] ; then
try=''
break
fi
echo -n .
try=`expr $try + 1`
sleep 1
done
}
function clean_up {
echo "Killing $(cat $PHP_FPM_PID)"
kill -QUIT `cat $PHP_FPM_PID`
wait_for_pid $PHP_FPM_PID
echo "Done!"
exit 0
}
trap clean_up EXIT
nohup php-fpm --daemonize --pid $PHP_FPM_PID 2>&1 &
while true; do sleep 1; done
# ^ do nothing forever
which waits 35 seconds or until that pid file has been removed (presumably by one of the child processes? I'm still unclear on how it's removed).
Regardless, this wrapper script works well as the CMD for our php-fpm docker container that we're running with Kubernetes.