简体   繁体   中英

Scheduled task on windows to run a PHP file

We distribute a PHP application that requires a scheduled task to be created to run a PHP script. I know how to do this. What I am trying to work out is the way to make this as simple as possible for the admin installing the software to set this up (ideally almost one click).

The problems:

i) We don't know where PHP is installed ii) We don't know where they have installed the file that we need to run.

My initial plan was to create a .bat file contains the path to PHP (so they would need to edit this file) and this loads our PHP script so is what they would need to create a scheduled task to run. Then there would be a second .bat file that would create the scheduled task itself (not entirely sure how to do that part).

Does this seem like the best approach? If so, any pointers on how to create a scheduled task using a .bat file - I can't find this information.

There is command "at" you can use in your .bat files to create scheduled task however it needs to be run as an admin.

Edit: schtasks does the same and may be used without admin rights.

You may search for php.exe, but it's little tricky - basically you need to use dir with /s parameter.

Why not inno setup for eg?

To run PHP scripts from bat files you can simply create ".bat" files and add your file path. Below is example:

  1. create "your_file_name.bat"
  2. open in editplus or notepad and type:

rename your filename in below example

"D:\Program Files (x86)\EasyPHP-5.3.8.0\php\php.exe" "D:\Program Files (x86)\EasyPHP-5.3.8.0\www\project_name\your_script_name.php" >> log_new.txt
rem "D:\Program Files (x86)\EasyPHP-5.3.8.0\php\php.exe" your_script_name.php
pause
  • That's it, you are done.
  • log.txt will log whatever output or error generated by php file.

Hope this will solve your issue :)

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