I've been struggling with shell_exec PHP function and at linux command for 2 days.
To make it short, this works:
shell_exec('/usr/bin/at 09:32 <<EOF
touch /var/www/website/hello.txt
EOF'
);
this doesn't:
shell_exec('/usr/bin/at 09:32 <<EOF
wget -O - -q -t 1 "http://192.168.56.101/website/test.php?param=hello" >/dev/null 2>&1
EOF'
);
Why ?
(note: the code above does work in console)
Thanks in advance.
Ok I've got it at last !!
For those who are interested the pb comes that the wget command also need to be invoked with the full path (ie: /usr/bin/wget).
What misleaded me is that the touch command doesn't need it. It's weird but anyway here's the working code:
shell_exec('/usr/bin/at 09:32 <<EOF
/usr/bin/wget -O - -q -t 1 "http://192.168.56.101/website/test.php?param=hello" >/dev/null 2>&1
EOF'
);
Related
I need to execute in one command two php files.
the second file need to run right after the first finish.
This is what i did, not sure if it's ok:
/usr/bin/wget -O /dev/null -o /dev/null https://example.com/scripts/cron.php; sleep 2; wget -q -O - https://example.com/cron2.php > /dev/null 2>&1
I added sleep between the commands, it will work?
You can use && for sequential execution of command,
check https://www.steveroot.co.uk/2010/07/05/cron-multiple/ And What is the purpose of "&&" in a shell command?
In Your case You can try :
01 00 * * * //usr/bin/wget -O /dev/null -o /dev/null https://example.com/scripts/cron.php && wget -q -O - https://example.com/cron2.php > /dev/null 2>&1
Hope these will Help.
I have main php script on web-server nginx+php-fpm and try to run another php script in background using GET request from web-browser. Line code in main.php to call detect.php:
exec("/usr/bin/php -f /var/www/detect.php 6 > /dev/null 2>&1 &");
detect.php does not start. I don't have any errors.
If to remove "&":
exec("/usr/bin/php -f /var/www/detect.php 6 > /dev/null 2>&1 ");
detect.php starts successfully.
From shell bash with "&" :
sudo -u www-data /usr/bin/php -f /var/www/detect.php 6 > /dev/null 2>&1 &
Script detect.php starts successfully.
try this and make sure your php path are correct
$dir=dirname(__FILE__);
$file_name="detect.php";
$php_path="/usr/bin/php";
$args = array(6);
$command1="cd $dir";
$command2="$php_path $file_name ".implode(" ",$args) ." > /dev/null 2>/dev/null &";
$final_command=$command1."; ".$command2;
shell_exec($final_command);
I have set up a line of code in my class to run a function in the background using the following line on my linux server:
$cmd = "nohup wget -q <-url here-> /dev/null 2>&1 &")";
$exec = exec( 'bash -c "'.$cmd.'"' );
(with the url just pointing to my script)
This is working fine and doing what I want it do, however everytime this runs it is creating a new file in my root (public_html) and they are all numbered like 10, 10.1, 10.2, 10.3 etc.
Does anybody know how I can stop these being created?
Also if this is bad practice and anybody wants to suggest a better way of doing something like this, any advice is greatly appreciated.
$cmd = "nohup wget -q <-url here-> -O /dev/null 2>&1 &")";
$exec = exec( 'bash -c "'.$cmd.'"' );
note the -O flag, with which you tell wget where to save the downloaded file. with this, you direct it to /dev/null and therefore delete it.
I am trying to execute a command with exec() and redirecting stdout and stderr to a file.
exec("nohup python main.py -i 1 > /var/scripts/logs/1_out.log 2>&1 &");
It will create the file but it will not print anything to it.
If I run the command in a terminal everything outputs without a problem.
Got it working. Python does its own output buffering which kept it from writing to the file. Running it with the -u option disables this. Final code looks like this:
exec("nohup python -u main.py -i 1 > /var/scripts/logs/1_out.log 2>&1 </dev/null &");
Thanks.
I have looked at other answers they dont fit to this case.
I am using the full path to the file. Code I copied is simplified.
run.php contains:
shell_exec("php /var/www/html/sync/chourly.php $position $quotientx > /dev/null 2>/dev/null &");
if I use manually php run.php - it works great.
here is the line on crontab -e :
05 * * * * /usr/bin/wget -O /dev/null http://sync.eeeww.com/run.php
again the file run.php starts BUT chourly.php doesn't start. I am using centOS 6
any suggestions please?
Addition: I checked the permissions I am using ec2-user to run php run.php and crontab is using the same permission. it is able to run the file but shell_exec is where the issue occurs
Is /var/www/html/sync/chourly.php using $SERVER['DOCUMENT_ROOT'] ? Since you're explicitly calling the php interpreter (not mod_php), a `$SERVER['DOCUMENT_ROOT'] call will not work as you expect.
Try manually running the cron from shell to see where it's failing.
cd /
su - your_httpd_usersame -c "/usr/bin/wget -O /dev/null http://sync.bitpine.com/run.php"