view raw
Leon Claassen Leon Claassen - 9 months ago 51
MySQL Question

Automate PHP Script

As part of a data import I have created a PHP file that runs on a webserver / localhost to input data into a database from a CSV source file. The problem is this page needs to be opened manually so in essence is just like running the actual MySQL script behind it.

How can I add this file to run at a specific day everyday on the server without "Human" interaction.

I tried some of the other solutions such as:
Starting PHP in Task Schedular and then running it with the argument
-f C:\Apache24\htdocs\my_phpfile.php

However, this did not seem to execute the PHP file because when looking at the info in the table it was not updated. I know the PHP file works as I have run this manually through a browser.

Please could I get some advice / help? What is the best way to do this?

I am currently testing the environment on my windows 10 laptop, but ultimately this will run on a Windows 2012 Server.



You can run php scripts in Task Scheduler (I do it a lot) but be aware of the following points:

You run it as php C:\Apache24\htdocs\my_phpfile.php

PHP must be in your PATH, else you will need to path directly to it:

c:\php\php C:\Apache24\htdocs\my_phpfile.php

Running it like this is running it CLI mode, not Web. When you hit the script in Apache the environment is quite different from CLI. Your script may not be running as you expect due to the difference in environment. e.g.

  • the current-directory is different,
  • there are no $_SERVER variables,
  • the $_ENV will be different,
  • there is no .htaccess,
  • the user will be different and therefore the file-access permissions
  • the loaded modules may be different
  • the PHP.ini may even be different

To test it in CLI, simply open a PowerShell, or a CMD prompt and enter the same command-line you're putting into WTS. You may see some output that informs you of the problem.

Look in your php_errors.log to see why the Task-Scheduler run instance failed.

If you cannot get the script working in CLI mode and must run it in Web mode, you can hit the web script using wget, curl or similar, or even using Internet Explorer's command line options.

Or use or similar service.

There are a lot of options here - the "best" in my mind is the simplest (KISS principe): running PHP in CLI directly. There's less to go wrong, you're not booting Apache just to load PHP (a massive waste), you're not relying on a network connection, you're not relying on 3rd party (paid) web-services, you're not at risk of IE being updated! WTS is also reliable (on Windows Server anyway).