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.
Related
I need to run a python script, compiled with pyinstaller via a PHP generated webpage.
I tried shell_exec(), exec() and system() without success.
I regularly run the script from terminal in background using:
temperature_sensor_code > /dev/null 2>&1
I've added www-data user to sudoers. I know it's not a good way but I need it in order to send killall temperature_sensor_code command (this is works).
This is my situation:
<?php
$run = escapeshellcmd('temperature_sensor_code > /dev/null 2>&1');
shell_exec($run);
header("Refresh: 0; URL=index.php");
?>
I've made a symlink in /usr/bin, also tried with the full path of the script with no luck.
UPDATE: to make it simpler, i've created a simple sh script run.sh and put in /var/www and make it RUN with
shell_exec("/var/www/run.sh");
this is working for me. So I put my script temperature_sensor_code in /var/www but this is not working. If I add var_dump(exec("/var/www/temperature_sensor_code/temperature_sensor_code"));
gives me: string(0) ""
I think there are problems with the compiled python script because the PHP side seems to be OK.
escapeshellcmd() does this:
Escape shell metacharacters
$run = escapeshellcmd('temperature_sensor_code > /dev/null 2>&1');
var_dump($run);
string(43) "temperature_sensor_code \> /dev/null 2\>\&1"
But you have shell metacharacters that you do want to behave as shell metacharacters:
temperature_sensor_code > /dev/null 2>&1
^ ^^^^
You're also doing no troubleshooting at all:
You discard all command output (that's what sending it to /dev/null does)
You don't get the return code
I suggest to:
Switch to exec() and make sure you use all its arguments and not just the mandatory ones
Get rid of > /dev/null until you diagnose the issue
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"
I'm trying to run a script in the background but it's not working, this is the script I'm running:
$ddd = "script.php?titulo=".$_REQUEST['titulo']."&descripcion=".$_REQUEST['descripcion']."&accion=".$encrypta->encode('comunicadoPadres')."";
exec("wget -qO- $ddd &> /dev/null &");
If I run the url directly on the browser the script works but from this exec() nothing happens
wegt command only access path location, you can't compile&run php script if you want use wget then use HTTP path of script
$ddd = "http://domain.com/script.php?titulo=".$_REQUEST['titulo']."&descripcion=".$_REQUEST['descripcion']."&accion=".$encrypta->encode('comunicadoPadres')."";
exec("wget -qO- $ddd &> /dev/null &");
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'
);
I am trying to run a program on swi prolog through php. I am running on windows
My command are as follow
$cmd = "C:\Program Files (x86)\pl\bin\swipl -f test1.pl -g test " halt;
exec( $cmd );
nothing happens when i try to run my php code it waits for while and returns to command prompt on windows/system32/exe
but when i execute the same command on cmd line i get the desired out put
C:\Program Files (x86)\pl\bin\swipl -f test1.pl -g test " halt;
i had been trying it for last two days, any help will be highly appreciated
Thanks in advance for all people who will give it a thought and help me
Try using double-backslashes (and including "halt" within the double-quotes):
$cmd = "C:\\Program Files (x86)\\pl\\bin\\swipl -f test1.pl -g test halt";
Program Files (x86) => progra~2
$cmd = 'C:\progra~2\swipl\bin\swipl.exe -s C:\prolog\web\base.pl -g test,halt';
it's work for me