Run PHP script in Windows task scheduler

Asked

Viewed 6,419 times

4

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

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

Despite the mémoria 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 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

Switching 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

  • Just put php -f "c:\wamp\www\reservas\EnviarEmail.php" at prompt does not work?

  • No, it gives the error I put in the question, in any script I run...

  • What’s on that line? Maybe it has some invisible character. If you have Notepad++ open the file go to the menu view>show symblos>show all symbols.

  • With the textpad it only shows blank spaces, has nothing on this line...

  • Gambiarra: chrome.exe "c:\wamp\www\reservas\EnviarEmail.php"

  • Removing space from a different result?

  • David, it worked that way, but then he called the browser and it worked fine, but then I come back to the memory problem. @rray I removed the spaces, but nothing changed.

  • Not to be boring :D, could do two more tests, maybe you have an environment problem. 1 - create a simple script a echo 'ola '. mt_rand(1,10000) and run it via prompt and see if the same error happens. 2 - If possible try running the original script in another php installation. I say this because another saw a user question q could not install a lib(Intl) had done the right process and was not going at all, so I suggested that he swapped the wamp for the xampp and it worked. The wamp and easyPHP seem to be half zicados ...

Show 3 more comments

2 answers

6


The most usual solution is to schedule this way:

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

This way, you’ll be getting the right executable and the php.ini correct for what you want to do.

Documentation of PHP boot parameters:
http://php.net/manual/en/features.commandline.options.php

The only precaution is to understand that since you are not using the web server (Apache, etc), no settings will work that are in files .htaccess and other typical things webserver, as the variables $_SERVER[].

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

  • 2

    +1 now yes a beautiful answer :)

5

The problem is yes, is that in apache your configuration must be one way and the moment it runs directly C:\wamp\bin\php\php5.5.12>php it must be using another configuration of the php.ini, in case you are using blocks so:

<?

?>

But depending on the settings in php.ini you will need to use it like this:

<?php

?>

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

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

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

CLI

Then just put it in the Windows task scheduler.

  • Type in cmd taskschd.msc and click on basic task:

    task

  • Type and title and next:

    inserir a descrição da imagem aqui

  • Select daily:

    inserir a descrição da imagem aqui

  • Schedule for when you want to start and the number of days break (if you want daily leave 1):

    inserir a descrição da imagem aqui

  • Select Start Program:

    inserir a descrição da imagem aqui

  • Add the php program and in the arguments add the php script path:

    inserir a descrição da imagem aqui

  • Click Finish.

  • 2

    +1, She looks good with Edit!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.