Running PHP script with .bat file

I need to run a php script at midnight every night on my server. On linux, I installed cron to work, but I am stuck on a Windows system.

I know that I need to configure the task using the Windows Task Scheduler and that the task will have to run the .bat file, which in turn will launch the php file, but I got stuck trying to write the .bat file.

I currently have:

@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 check it, I get a Windows warning saying

“Windows cannot find“ -f. ”Make sure you type the name correctly, then try again.

What did I miss?

+7
windows php batch-file
source share
5 answers

The START command additionally accepts the title for the created window as the first argument; in this case, he believes that C:\Program Files (x86)\PHP\v5.3\php.exe is the header to display, and -f (second argument) is the executable file that you want to run.

This way you can fix this by providing a 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 completely disable the START command (you are not using any unique features) and just start PHP directly:

 "C:\Program Files (x86)\PHP\v5.3\php.exe" -f C:\inetpub\wwwroot\sitename\crons\reminder-email.php 
+8
source share

In fact, you don’t even need a batch file. You can run php script from task scheduler.

Just let the task scheduler run php.exe and set the location of the php file as an argument to the task.

+3
source share

May 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. Do not use Windows folders with spaces in your names.

+2
source share

Unfortunately, it took about 10 hours to understand. Some protocols, such as WinRM and PSEXEC, must transfer a registered user account regardless of the credentials provided (this or PHP overrides everything you enter). In any case, in order to get PSEXEC, WinRM, or just let go of batch files that connect to remote computers, you will need to change the launch of IIS as a user account with rights to these resources (service account). I understand that this is a huge security hole, but it is by design. PHP is safe, so it can’t be easily cracked, you must redefine your security by specifying the run as an account. Not the same as your application pool account, although your IIS credentials may use your application pool account.

0
source share

Try these guys!

  cd E: \ xampp \ htdocs \ my-project
 E:
 php artisan schedule: run
0
source share

All Articles