简体   繁体   中英

running php scripts locally though task manager

I'm looking for some advice.

Rignt now i've got a bunch of php scripts that i've scheduled through cron. They run on my local machine doing stuff like pulling stuff out of a mysql db and sending automated emails. To run them I just have something like this in crontab: 0 7 * * 1 /usr/bin/php /phpscripts/script.php

I need to migrate all of those scripts to a Windows machine. I'm planning to use the Windows Task Scheduler to run the scripts, but how can I run the actual php scripts locally? From what I understand you need something like xampp to run the apache server? I guess what I need is a Windows equivalent of /usr/bin/php in crontab.

Set up your task to run when you want it (times and all that)

and pop this into the command :

C:\Path\to\php.exe -f "C:\Path\to\file.php"

Edit: you can also set a second php.ini to be run used when the CLI is used to run a file, which has no constraints on max execution time and the like. Very handy difference and better suited to running (potentially) long execution scripts.

You can do this by creating a php-cgi.ini file in your PHP folder where your php.ini file resides. This will be used automatically when a PHP file is executed from the CLI (this is how scheduled tasks are run).

Also note that Windows Scheduler will simply end on an error that causes your script to fall over, so running some extra logging might be a good idea in case your scripts exit early.

Installing PHP

You don't have to install xammp , you can install PHP alone, have a look ate the windows PHP installation guide:

If you prefer installing XAMP, you can run PHP script after locating the php.exe with the -f flag:

C:\Xampp\php\php.exe -f C:\Xampp\htdocs\my_script.php

Running the PHP file

After you have PHP installed, check Command Line PHP on Microsoft Windows manual for information on how to run the script. On the page there is explanation of how to make the php file executable, so you could run it as:

"C:\PHP Scripts\script" -arg1 -arg2 -arg3

Make sure you are using an administrative account to run the command. Otherwise you might have permissions problems. more info at the Introduction to using PHP on the command line

Scheduling the task

Go to Start -> Programs -> Accessories -> System Tools -> Scheduled Tasks ,

  1. Right-click on an empty spot in the Scheduled Task window and select New -> Scheduled Task (Also accessible via File -> New -> Scheduled Task)
  2. Name the new task (How about "Bill"? He looks like a Bill, doesn't he? "Mr. B. Evolution, II" It sounds so regal.)
  3. Double-click the new task to open the properties window (or File -> Properties)
  4. Under the Task tab, enter the same command that you used to test the script above. For instance, I would enter:

    C:\\PHP\\php.exe "C:\\Inetpub\\wwwroot\\blogs\\cron\\cron_exec.php"

  5. Go to the Schedule tab and enter when and how often the task should run. The schedule defaults to run once daily and should be fine for basic usage, but feel free to tweak as needed.
  6. The rest of the fields can be left as-is, unless you're an ace and know what you're doing.
  7. Click OK and we're done!
for more info have a look at setting up a window scheduled task .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM