Running a php script with a .bat file
Solution 1
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
Solution 2
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.
Solution 3
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.
Solution 4
How about this?
set php="C:\Program Files (x86)\PHP\v5.3\php.exe"
%php% -f C:\inetpub\wwwroot\sitename\crons\reminder-email.php
Comments
-
evilscary almost 2 years
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?
-
Waihon Yew almost 11 yearsI 'm not sure why one would choose to do things this way. For example, why CD to the cron script directory? Why add something to the PATH instead of just specifying the path in the command line? And finally, what's the problem with putting things in Program Files? I 've been doing it for years; as long as you properly quote the directory name whenever needed things of course work just fine.
-
evilscary almost 11 yearsI'm working with a pre-existing system, so the file structure is out of my hands.
-
a77icu5 about 10 yearsjust one problem, bat don't wait until the script ends
-
Waihon Yew about 10 years@a77icu5: Well, it was not a problem in the context of the question, and anyway it only happens if you use
START
(invoking directly waits until PHP executing ends). Even withSTART
, you can use/WAIT
to match the direct invocation behavior. -
Bob Nocraz over 8 yearsworked perfectly. pretty obvious answer as well. not sure why I didn't just try this in the first place!
-
AFwcxx about 8 yearsHow do I let the cmd window to remain open? let say i'm running a script for web socket indefinitely.
-
Mr Rubix almost 8 yearsWorked Perfectly. Good Idea!