Run PHP script in Windows Task Scheduler

2

I have a PHP script that I'm currently running with an open browser, with the command below:

<meta HTTP-EQUIV="refresh" CONTENT="1800">

Despite the memory problems that the browser consumes, it was running smoothly. Now I need to develop 3 more scripts that will also run from time to time. Is there any way to run the script via task manager? I did a test with a prompt, but I did not succeed, because regardless of the script, it always returns a parse error that does not exist.

Parse error: syntax error, unexpected end of file in C:\wamp\www\reservas\Enviar
EmailLembrete.php on line 281

C:\wamp\bin\php\php5.5.12>php c:\wamp\www\reservas\EnviarEmailLembrete.php

Changing to localhost:

C:\wamp\bin\php\php5.5.12>php -f http://localhost/reservas/EnviarEmailLembrete.p
hp
Could not open input file: http://localhost/reservas/EnviarEmailLembrete.php

Code JsFiddle

    
asked by anonymous 19.05.2016 / 22:21

2 answers

5

The most usual solution is to schedule this:

c:/caminhocorreto/php -c c:/caminho/para/o/php.ini [ -f ] c:/caminho/para/o/script.php

In this way, you will be getting the right executable and php.ini correct for what you want to do.

  

PHP startup parameter documentation:
link

The only caution is to understand that since you are not using the webserver (Apache, etc), settings that are in .htaccess and other typical webserver things like variables $_SERVER[] .

An alternative if the task is more recurring is to call a PHP with an infinite loop at system boot (with the same syntax above), but not to "choke" the CPU unnecessarily, at the end of each cycle you call the sleep , which in addition to giving the necessary pause between one execution and another, frees the CPU for other tasks.

    
19.05.2016 / 23:11
5

The problem is yes, in apache your configuration must be in a way and at the moment it runs directly C:\wamp\bin\php\php5.5.12>php it should be using another php.ini configuration, in case you is using blocks like this:

<?

?>

But depending on your php.ini settings you will need to use this:

<?php

?>

Your script failed because the php.ini that has short_open_tag disabled, the php.ini that apache uses is different sometimes, usually some servers do this because it runs by command line need different extensions and configurations of the HTTP layer, I recommend that you always use <?php , to avoid headaches, since it will work on all servers, other than short_open_tag that only works if enabled.

  

A note, you can use <?="oi" ?> even with short_open_tag disabled, this works in PHP5.4 + usually equivalent to <?php echo "oi"; ?> .

And then run via command line, I ran in the terminal and it worked:

ThenjustputitintheWindowsTaskScheduler.

  • Typecmdtaskschd.mscandclickBasicTask:

  • Typeandtitleandnext:

  • Selectdaily:

  • Schedulewhenyouwanttostartandthenumberofdaysoff(ifyouwanttoleave1daily):

  • SelectStartProgram:

  • Addthephpprogramandintheargumentsaddthephpscriptpath:

  • Click Finish.

19.05.2016 / 22:56