How do I run a PHP script using windows schedule task? - php

I have installed localhost/server in my machine and I need to run a php script using windows schedule task. how do I add path in "Actions" tab in schedule task / cofigure the script to run for particular period?

Locate the php.exe executable on your system and pass it the name of the script file using the -f parameter.
Example:
C:\Xampp\php\php.exe -f C:\Xampp\htdocs\my_script.php
Reference:
Introduction to using PHP on the command line
PHP command line options

Here's how did it.
Windows scheduler -> create a new task -> action tab -> Edit

At least I tried out some suggestions but it doesn't work so I tried this.
Use a bat file and schedule to execute that bat file.
For example in the bat file executephp.bat, write this
c:\xampp\php\php.exe -f c:\xampp\htdocs\do_something.php
save that bat file that contains that line.
Go to windows scheduler and create a new task and in action tab, browse to point that executephp.bat and for start in -> direct to the directory u have that executephp.bat.
For example if u save the file under C:\xampp\htdocs put that C:\xampp\htdocs in the start in.
Remember to invoke the script even when the user is not logged on.
Everything is set and it will execute without problem.

You can use PHP Command Line to execute it rather then trying to load it through the browser.
Under the actions tab, create a new action and:
Program/Script: Point to your PHP.exe file
Add Arguments: -f /path/to/php/file.php
Optionally you can make it start in the script's directory as well.

create Schedule task
Scheduler->Actions->Edit Action
if you have php file
Program/script: powershell
Add arguments: curl http://localhost/demo/cron.php
if you have MVC/CMS URL
Program/script: powershell
Add arguments: curl http://localhost/demo/controller/method

I just wanted to leave what i had to do to get this working for server 2012. Which was what has previously been said but with added quotes and using the 'Add arguments' box. So in Task Scheduler->Actions->Edit Action.
Program/script: "C:\xampp\php\php.exe"
Add arguments: -f "<full path and filename>"
Start in: <Path to file>
(I had to use 'start in' as i referred to class's within the script)
Hope this helps.

Here's how I did it.
In the Run box: c:\location_of_my_php_installation\php.exe -f c:\location_of_my_php_file\php_file.php
In the Start in box: c:\location_of_my_php_installation\php.exe

you can directly call your local host url by using
explorer "http://localhost/yourFile.php"

If the answer given by Pekka does not work (C:\Xampp\php\php.exe -f C:\Xampp\htdocs\my_script.php), make sure that you have the correct PHP extensions enabled as well as the correct php.ini file being used for the PHP version you are using.
I ran into this issue recently and resolved it. I was using PHP v5.4 to run my script.php that was nested within the top level PHP folder (v5.3). When I ran the script.php, from within the v5.4 folder, it was using the v5.3 php.ini file with different extensions, which caused the script.php to fail.
To fix this, here is what I did within the Task Scheduler : Actions tab
Program Script:
"C:\Program Files (x86)\PHP\v5.4\php.exe"
Add Arguements:
-c "C:\Program Files (x86)\PHP\v5.4\php.ini" -f "C:\Xampp\htdocs\script.php"
Using the -c option, you can specify which php.ini file should be used (stackflow answer).
I verified which php was in use by following this stackflow answer.
In command line, type php -m to check which extensions are enabled.
Then, use php --ini to check which .ini file(s) is/are being read by PHP.
You may not be using the correct ini file for the PHP version you are using if you have multiple versions installed.

You dont need a .bat file just set the options in the task scheduler:
So here i've just pointed to the php.exe and then in the arguments the
-f fullpath2PHP file
The important bit is the Start in part - this should be the folder your php file (the one you want to run) sits in.
so for example:
File location
-f c:\inetpub\wwwroot\runthisplease\myFile.php
Start in
c:\inetpub\wwwroot\runthisplease\
You can test this by creating a windows shortcut (but you cannot schedule a shortcut link)

In the properties for your task in the Task Scheduler, change the account for which your script is running under to the SYSTEM account.
Beaware! You should ONLY do this for scripts you fully trust - Doing this elevates the privileges to of the script to beyond administrator.

To create a scheduler in Windows, you need to:
Create .bat file on your server or system;
Type the following command in your.bat file: “F:\xampp\php\php.exe” -f “F:/xampp/htdocs/sitefolder/test.php”;
Set the scheduler time and file in your task scheduler in Windows.

Related

Execute PHP routines on Windows Server [duplicate]

How can I create a scheduled task to run a PHP file?
Yes, I filled out everything in the scheduled task, but it still doesn't work.
Run: "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\WEB\4w_website\save.php"
Start in: "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\WEB\4w_website"
It just opens the PHP file in Notepad.
I gave the correct user name and pwd.
Please help me..
The Run command should be
C:\Path\to\php.exe -f "C:\Path\to\file.php"
From the command line help of php.exe:
-f Parse and execute <file>.
I just wrote a .bat file that does the work file.bat
#ECHO OFF
php.exe -f "C:\code\cust.php"
And put it in the Schedule Tasks like this:
Run: C:\code\file.bat
Start in: C:\code\
If you running php scripts, in most of cases script awaiting to be runned in current folder.
That mean you must set up folder each your action, use field "Start In".
Example:
Run: C:\php\php.exe
Arguments: -f C:\web\myscript.php
Do not forget:
Start in: C:\web\
Sounds like you don't have PHP files associated with the EXE.
You can do this My Computer > Tools > Folder Options > File Types. If nothing else, this can also help you verify your settings for them.
Otherwise, you can specify "C:\path\to\php.exe [file]" in the task.
This is what I did:
PHP file
<?php my code goes here ?>
*Note if you are using HTTP API/CURL in CLI use dl("php_curl.dll");
this loads curl into cli
Now I added PHP to windows path variable, this can be done from My computer, properties, advanced settings, environment variables, new
Next I created a .bat file, simply open a notepad & type code below and save as myfile.bat
#ECHO OFF
php -f d:\wamp\www\V3\task.php
This site might help you on .bat file syntax.
Now create a new scheduled task on windows & call the above .bat file as source,
For those people that can't able to make #Tomalak's answer work:
Check if there are spaces () in your directory. If it does, you need to enclose them with ".
"C:\Path\to php file\php.exe" -f "C:\Path\to php file\file.php"
And if it still did not work, check your file.php if there are included files in it.
include("file2.php"); /* OR */
include_once("file2.php");
Remove it. And if the included file in your file.php is really needed, try to move the code/script from that included file to your main file.
I think, you must execute your PHP script via URL.
you can write batch script for execute URL.
Why you don't write backend script in other language such as batch script, vbscript or etc.

Is it possible to run a php script without specifying the path to the script?

I need to run a php script from the command line. The problem is, this script will be in a different location on different PCs.
So I want to be able to enter something like this:
php myscript.php
where myscript.php will be in a folder which is in the windows path, but not in the current folder.
Is it possible to do something like this?
Please refer to this link in the php manual.
http://php.net/manual/en/install.windows.commandline.php
On any pc you have to setup the PATH and PATHEXT variable.
Append the location of the PHP executable (php.exe, php-win.exe or
php-cli.exe depending upon your PHP version and display preferences)
to the PATH environment variable. Read more about how to add your
PHP directory to PATH in the corresponding FAQ entry.
Append the .PHP extension to the PATHEXT environment variable. This
can be done at the same time as amending the PATH environment
variable. Follow the same steps as described in the FAQ but amend
the PATHEXT environment variable rather than the PATH environment
variable.
Associate the .PHP extension with a file type. This is done by
running the following command: assoc .php=phpfile
Associate the phpfile file type with the appropriate PHP executable.
This is done by running the following command: ftype phpfile="C:\PHP5\php.exe" -f "%1" -- %~2
This will allow your script to run as normal shell command, like a batch file.
You will run it as :
myscript.php
and not
php myscript.php

Double click and run the Php script on mac

I am sorry if this question was answered.
Why can't I run php code directly without using terminal on mac.What I mean when you double click on html file it automatically opens in the browser but not in the case of php.If I try to double click on php it opens with some text-editor.
Any help would be helpful.
Try this (for mac),
Open terminal
cd to folder
Start php server - php -S 127.0.0.1:8000
Open browser and enter - http://localhost:8000/file-name.php
I think you don't understand what PHP is ...
HTML is a markup-language, that can directly be understood by the browser. If the browser opens the file, it can do something with the content.
As PHP is a programming-language, you need a parser. This parser is your PHP executable. This program can understand PHP and does nothing more, than running the code and giving something as result. This result may be an HTML webpage, an image or whatever.
Since you said, you're using a mac, here's a quick introduction on how to set up your personal webserver:
On Mac OSX, PHP and Apache (that's what I use in this example) is already installed and pre-configured. You can just start using it like this:
Go into your system preferences and verify that Web Sharing is enabled.
Open the Finder and go to /Library/WebServer/Documents/localhost. All files that are in there are processed by the local webserver (Apache and PHP, if you want to know that). Place your file in there and open your webserver and call http://localhost/YourFile.php and it will call the file YourFile.php and show you what the output of the script is.
EDIT:
If you are using PHP for scripts, like bash-scripts, see the answer #andreas-baumgart provided.
To run PHP in MAC, one should start the built-in Apache Web server and also enable the PHP already installed.
This can be done with the following steps.
Go to /etc/apache2/httpd.conf and change the permission to sudo chmod 777 httpd.conf
Then open the above file to uncomment the line #LoadModule php5_module libexec/apache2/libphp5.so
To start the apache built-in server, use the command sudo apachectl start in the terminal.
Now .php files can be created and run from the terminal using php -f filename.php and it can also be run on a browser using http://localhost/filename.php
You cannot execute plain PHP scripts as they are no executable programs but source code. As such they contain just the receipt for an interpreter to create executable code. In order to run your PHP script you need to pass it to the PHP interpreter. In your scenario you can archive that by providing a shebang.
To run your script on double click try this:
Make the script executable using chmod +x yourscript.php
Prepending the according Shebang to the files content: #!/usr/bin/env php.
Select a PHP file in Finder, hit CMD-i and change "Open With" to "Terminal.app".
Late response, but was looking into doing this for myself, this coming up as one of the results in my searching wanted to provide 2 solutions since I ultimately came to both on my own.
Solution #1
The simple way is to go a round about way by writing a wrapper file to execute the script you're working on. Create a file with the following code:
#!/usr/bin/php
<?php
include('name-of-php-script.php');
?>
Save it as wrapper.command The name wrapper isn't important, but the command extension tells Finder that this is a shell script to open up in Terminal. The file itself just executes whatever php script is in the include.
Solution #2
The specific inquiry requires a bit of work.
First make sure that the 1st line of the php script is:
#!/usr/bin/php
This is where the preinstalled version of PHP is installed on Mac OS X. You can always verify by running this command in terminal:
whereis php
Once you've added the Shebang line to the php script you've readied it for automatic execution.
To make it double clickable executeable you have to do the following:
Right click on the PHP script and click Get Info. Click where it says Open With, click the default option to see all the available options. Select Other...
Switch where it says Enable: from Recommended Applications to All Applications, and click the checkbox for Always Open With. Choose Terminal as the application. Finally, you have to click the button that says Change All...
OS X will verify you want it to set Terminal as the default application to open .php files
This will make every php file open up in terminal by default, but unless they contain the #!/usr/bin/php line they won't actually run.
Try MAMP
MAMP 4 brings even more opportunities for web developers. We are now supporting MySQL 5.6 and Nginx is now fully integrated. Server starting times have been improved.
Because .php files are not 'executable' per se, instead they are just text files with a PHP extension.
You need to run the php interpreter against the file to execute on it's contents.

Run Python script from PHP

I want to run Python script from PHP. The script's syntax is good, when I run it from command line it generates an image file next to it. When I run the PHP file (contains Python exec) from command line it generates an image file. It's good. The problem is: when I use browser and run PHP it isn't generate any image file. I use Xubuntu OS.
Does the script that you are trying to run requires sudo privileges? If so, you will need to add the user "www-data" or web user with the command (full path) to the sudoers file (Ubuntu /etc/sudoers or execute visudo command).
More info here https://help.ubuntu.com/community/Sudoers
Another thing to look at is at memory_limit in your php.ini directive. You might be using all memory allowed to use.
The problem was the matplotlib backend setting. I changed to 'Agg' and the plot function worked fine.

Running a php script with a .bat file

I need to run a php script at midnight every night on my server. On a linux system I'd set up a cron job, but I'm stuck with a windows system.
I know I have to set up a task using the windows task scheduler, and that the task will need to run a .bat file which in turn will run the php file, but I'm stuck trying to write the .bat file.
What I currently have is:
#echo off
REM this command runs the nightly cron job
start "C:\Program Files (x86)\PHP\v5.3\php.exe" -f C:\inetpub\wwwroot\sitename\crons\reminder-email.php
But when I try to manually run the .bat file to test it, I get a windows alert saying
"Windows cannot find '-f'. Make sure you typed the name correctly, and then try again.
What have I missed?
The START command optionally accepts a title for the created window as its first argument; in this case, it thinks that C:\Program Files (x86)\PHP\v5.3\php.exe is the title to display and -f (the second argument) is the executable you want to run.
You can therefore fix this by providing a placeholder title, e.g.
start "email reminder task" "C:\Program Files (x86)\PHP\v5.3\php.exe" -f C:\inetpub\wwwroot\sitename\crons\reminder-email.php
Or, preferably, you can ditch the START command altogether (you aren't using any of its unique facilities) and just run PHP directly:
"C:\Program Files (x86)\PHP\v5.3\php.exe" -f C:\inetpub\wwwroot\sitename\crons\reminder-email.php
Actually, you don't even need a batch-file.
You can run the php-script from the task scheduler.
Just let the task scheduler run php.exe and set the location of the php-file as the argument of the task.
Can I suggest a small change.
echo off
REM This adds the folder containing php.exe to the path
PATH=%PATH%;C:\Program Files (x86)\PHP\v5.3
REM Change Directory to the folder containing your script
CD C:\inetpub\wwwroot\sitename\crons
REM Execute
php reminder-email.php
PS. Putting Apache,MySQL or PHP in Program Files is a bad idea. Dont use windows folders with spaces in their names.
How about this?
set php="C:\Program Files (x86)\PHP\v5.3\php.exe"
%php% -f C:\inetpub\wwwroot\sitename\crons\reminder-email.php
Sadly this took close to 10 hours to figure out. Some protocols such as WinRM and PSEXEC must pass the logged in user account regardless of the credentials supplied (that or PHP overrides whatever you type in). At any rate, to get PSEXEC, WinRM or just kicking off batch files that connect to remote computers, you will need to change the IIS run as account to a user with rights to those resources (service account). I realize this is a huge security hole, but that is by design. PHP is secure so that it can't be hacked easily, you have to override their security by specifying a run as account. Not the same thing as your app pool account - although your IIS credentials can use your app pool account.
Try like this guys!
cd E:\xampp\htdocs\my-project
E:
php artisan schedule:run

Categories