I found this post here : Run Cron Job on PHP Script, on localhost in Windows which is working perfectly. Instead of a PHP file I want to run an URL instead, like http://localhost/test-cron-job
I tried to simply change this line here:
"C:\xampp\php\php.exe" -f "C:\Users\Matthew\Documents\Work\cronjob\my_script.php"
to this:
"C:\xampp\php\php.exe" -f "http://localhost/test-cron-job"
But it is not working at all. What can I do to make this work?
It doesn't work because php.exe is supposed to execute a PHP file. When you point it to a URL it doesn't know what to do.
What you need is something like curl for Windows and then you can make a HTTP request:
curl http://localhost/test-cron-job
Related
I'm using XAMPP on Windows 10, and trying to run a simple PHP script that uses the shell_exec function to call a simple R script in the same directory. Here's the PHP index.php file:
<?php
echo shell_exec('Rscript test.R');
And here's the test.R file:
print("Hello, World!")
From the command line (i.e., Git for Windows), when I run php index.php in the folder, I get the following output:
[1] "Hello, World!"
However, when I run index.php from the web browser via XAMPP and localhost, I don't see anything on the page. At first, I thought it might be a permissions issue, so I gave Full control access to all users on the folder, but it didn't seem to change anything.
Any ideas on how to get this working from the browser? Thank you.
Found the answer to the problem I was having. Apparently, even though Rscript is part of my Windows 10 PATH variable, when I try to execute an R script via shell_exec in PHP, I have to have the whole path to the Rscript executable (I guess because the PHP script / Apache server doesn't know about the path variable).
I changed the shell_exec function call as follows, and it worked:
echo shell_exec('"C:\Program Files\R\R-4.1.0\bin\Rscript" test.R');
Here's a link to the SO post that helped me figure this out:
shell_exec does not execute from browser requests
I am working on a batch-script that makes a SQL query and saves it to a file. The file will then be handled by PHP. Is it possible to POST a file from Windows CMD to a PHP site so it can be handled by php with $_FILES['someFile']?
Yes, you can use curl for this.
curl -F someFile=#localfile.sql http://example.org/upload
Or you can use wget.
wget --post-file=file.jpg http://yourdomain.com/target.php
I have to delete various files on the server using rm.
I tried using:
shell_exec(escapeshellarg(escapeshellcmd("rm -f ./io_cache/".$prob_id."/*")))
where $prob_id is a number as well as a name of a directory. Commands like ifconfig are working fine. When using the same command with exec it returns error code 127. If I use a variant :
exec("rm ".escapeshellarg(escapeshellcmd("-f ./io_cache/".$prob_id."/*")),$out,$ret)
Then it returns error code 1. shell_exec("pwd") returns /opt/lampp/htdocs/mat and the directory io_cache is present in mat.
I also tried doing the job using a python script. When I call python script without system arguments and deleting some random files the script is running fine.But when I am using something like:
shell_exec(escapeshellarg(escapeshellcmd('./rem.py ').$prob_id))
To pass the directory from where to delete files then also nothing happens. rem.py is in mat.
I am working on Ubuntu and using apache web server
I've a PHP script that uses cURL to perform certain tasks. At the moment, I have the script running every 10 minutes. This is what I'm running via Windows Task Scheduler.
C:\wamp\bin\php\php5.4.3\php.exe -f C:\wamp\www\autoscripts\index.php
However, for some reason, whenever the argument quoted above is run through the command line, I get the error "Fatal error: Call to undefined function curl_init()". The script works perfectly when I access it via the browser. Is there any reason why PHP isn't able to access the cURL extension via the command line?
Most likely running from command line does not use any ini file that loads the extensions. Open phpinfo() from the browser, copy path to loaded ini file and change your task to:
C:\wamp\bin\php\php5.4.3\php.exe -c "C:\path\to\php.ini" -f C:\wamp\www\autoscripts\index.php
Figured it out. Basically, on WampServer, there are TWO php.ini files that you need to be aware of.
C:\wamp\bin\php\php5.4.3\php.ini
C:\wamp\bin\apache\apache2.2.22\bin\php.ini
Forgot that the command line uses a different ini file than the web server. :(
I want to run a PHP script every 15 minutes using either CURL or WGET.
This PHP file is in a local folder:
/home/x/cron.php
How would I run this using CURL/WGET?
It doesn't work when I try to run
curl /home/x/cron.php
Thank you!
CURL and WGET are more adecuate for URLs like http://myhost.com/cron.php
When the script is offline, you would better run it using php CLI:
Ex:
php -q cron.php
Just do something like this:
/usr/bin/php /home/x/cron.php
cURL/wget is for HTTP actions. If your PHP script is on the same system, you don't want to load it over HTTP. (You can, of course, if it is accessible over HTTP, but I don't think that is what you want.) Just call it directly.
Alternatively, you can set the execute permission on your script and throw in a shebang line for PHP:
#!/usr/bin/php
Then, just put your PHP script in crontab directly.
If you're using CURL or WGET, I believe you'll need to pass in the path as a URL. If you want to run the php script on the command line, you'll need to use the the php CLI