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
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 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.
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.
Im using an ajax call to execute "shell_exec" on the server (centos).
The line that im executing is the following
echo shell_exec("php -q /websockets/timedactions.php");
This is the server response:
X-Powered-By: PHP/5.5.16
Access-Control-Allow-Origin: http://myIpAddress
Access-Control-Allow-Credentials: true
Content-type: text/html
0
After running this command it seems that the process that it supposed to activate is not running.
Calling the same command on shell with root access
php -q /websockets/timedactions.php
works perfectly.
How can i make the script work using shell_exec ?
There can be errors, while the command is executed. You can redirect STDERR to STDOUT, to see if there are any, like this(Sample 3.5).
echo shell_exec("php -q /websockets/timedactions.php 2>&1");
Also there could be something preventing shell_exec from printing (link).
I have problem with script that running screen session.
<?php exec("screen -m -d ffmpeg2theora -v 3 /path/to/video.mp4"); ?>
and see this on logs:
Wed Oct 17 16:17:39 2012] [error] [client 83.5.x.x] sh: 1: screen: not found, referer: http://xxxxxx.xx
It's stop working on some update (apache/php/suphp?), before that was working like charm
Any idea's what's happend, how configure suPhp/Apache?
If you want PHP to start a long-running command in the background, why not try the following:
<?php pclose(popen("/usr/bin/nohup /path/to/ffmpeg2theora -v 3 /path/to/video.mp4 >/tmp/result 2>&1", "r")); ?>
It's always wise to use explicit paths to commands in these sorts of situations.
Instead, simply do:
exec("ffmpeg2theora -v 3 /path/to/video.mp4 &");