I want to know if there is an equivalent cron in Windows and how I can use it programmatically using PHP.
Windows has the Scheduled Tasks control panel applet (or management console plug-in on later versions of Windows) but you can also access it via schtasks.exe if you want to automate it from the command line.
In addition, you can also use at from the command line to schedule a task.
Related
I have an XML database that I want to manage independently from users on my website. Looking into the matter it appears that I should write a daemon script to manage my database. That is all fine and dandy but, I feel like I'm opening a can of worms. I wanted to write my daemon script in PHP, so I looked into PCNTL. But I quickly learned that PCNTL is not suited for web servers. So now I am stumped. How can I get a daemon to run on my server? Do I need to learn another language? I only want to write my own scripts. But I feel lost. I would prefer to write my daemon in PHP as I am familiar with the language.
I have been researching everything from PCNTL, CLI, SO questions, numerous articles on daemon processes... etc
I am running PHP 5.6.32 (cli), windows 7, on Apache. XAMPP 5.6.32. Unix system.
EDIT: I also have windows setup to run PHP from command prompt.
There's nothing wrong in running a PHP daemon, however it's not the fastest thing, especially before the 7.0 version. You can proceed in two ways:
Using Cron Jobs, if you're under Unix systems crontab will be fine, in this way you can specify the interval within the system automatically executes the specified script and then exit.
The true daemon, firstly you need to change the max_execution_time in PHP.ini to 0 (infinite), then in your daemon call for first function set_time_limit(0);, remember to run it only once. However if there is some failure like a thrown error uncatched the script will exit and you need to open it again manually, and don't try...catch in a while loop because it will probably go into an endless loop. Execute the script with php -f daemon.php.
I want to follow this tutorial to learn about creating CRON jobs in PHP.
Two things:
I am working on a Windows Machine. But the tutorial says,
The cronTab, or "Cron Table", is a Linux system process / daemon which
facilitates the scheduling of repetitive tasks thereby easing up our
day to day routine.
So does this mean that it will Not work on Windows machines?
I have read that SSH2 is only available for PHP versions which are
older than PHP 7.0.
I currently have PHP 7.0 So the question is that should I downgrade
my PHP (that is uninstall this version and install PHP 5.6) to use
this extension.
My ultimate purpose to create a CRON job to keep monitoring a directory for the presence of a certain type of files. When a file is found, it is opened and useful data is extracted from it and loaded into the database.
cron is the Linux scheduled task. Windows has Task Scheduler you can use instead, to run your scripts on a schedule
SSH2 is a PECL extension. You'll note there is a precompiled DLL for Windows listed. Be sure to install 1.0
ssh2 1.0 and above is PHP 7 only
I'm looking for some advice.
Rignt now i've got a bunch of php scripts that i've scheduled through cron. They run on my local machine doing stuff like pulling stuff out of a mysql db and sending automated emails. To run them I just have something like this in crontab: 0 7 * * 1 /usr/bin/php /phpscripts/script.php
I need to migrate all of those scripts to a Windows machine. I'm planning to use the Windows Task Scheduler to run the scripts, but how can I run the actual php scripts locally? From what I understand you need something like xampp to run the apache server? I guess what I need is a Windows equivalent of /usr/bin/php in crontab.
Installing PHP
You don't have to install xammp, you can install PHP alone, have a look ate the windows PHP installation guide:
Windows Installer (PHP 5.1.0 and earlier)
Windows Installer (PHP 5.2 and later)
Manual Installation Steps
If you prefer installing XAMP, you can run PHP script after locating the php.exe with the -f flag:
C:\Xampp\php\php.exe -f C:\Xampp\htdocs\my_script.php
Running the PHP file
After you have PHP installed, check Command Line PHP on Microsoft Windows manual for information on how to run the script. On the page there is explanation of how to make the php file executable, so you could run it as:
"C:\PHP Scripts\script" -arg1 -arg2 -arg3
Make sure you are using an administrative account to run the command. Otherwise you might have permissions problems. more info at the Introduction to using PHP on the command line
Scheduling the task
Go to Start -> Programs -> Accessories -> System Tools -> Scheduled Tasks,
Right-click on an empty spot in the Scheduled Task window and select New -> Scheduled Task (Also accessible via File -> New -> Scheduled Task)
Name the new task (How about "Bill"? He looks like a Bill, doesn't he? "Mr. B. Evolution, II" It sounds so regal.)
Double-click the new task to open the properties window (or File -> Properties)
Under the Task tab, enter the same command that you used to test the script above. For instance, I would enter:
C:\PHP\php.exe "C:\Inetpub\wwwroot\blogs\cron\cron_exec.php"
Go to the Schedule tab and enter when and how often the task should run. The schedule defaults to run once daily and should be fine for basic usage, but feel free to tweak as needed.
The rest of the fields can be left as-is, unless you're an ace and know what you're doing.
Click OK and we're done!
for more info have a look at setting up a window scheduled task.
Set up your task to run when you want it (times and all that)
and pop this into the command:
C:\Path\to\php.exe -f "C:\Path\to\file.php"
Edit: you can also set a second php.ini to be run used when the CLI is used to run a file, which has no constraints on max execution time and the like. Very handy difference and better suited to running (potentially) long execution scripts.
You can do this by creating a php-cgi.ini file in your PHP folder where your php.ini file resides. This will be used automatically when a PHP file is executed from the CLI (this is how scheduled tasks are run).
Also note that Windows Scheduler will simply end on an error that causes your script to fall over, so running some extra logging might be a good idea in case your scripts exit early.
How to run a scheduled job scripted in php using windows? I'm using Windows XP sp3.
Use at or the task scheduler.
How To Use the AT Command to Schedule Tasks (seems to require login now - wtf?)
Alternative: Wikipedia on AT
How To Schedule Tasks in Windows XP
You would have to call the PHP interpreter from the command line. In Windows, it is called php.exe. You'll have to find out where it resides, usually in the Programs folder or a sub-directory of your server installation.
The command line for the call will then be something like
"C:\Program Files\Xampp\PHP\PHP.exe" -f "c:\htdocs\my_script_to_run.php"
Note that PHP will probably use a different php.ini file when called this way, and some variables (like $_SERVER["HTTP_HOST"]) are not available. This way of running PHP is called CLI (Command Line Interface).
To determine within a PHP script whether it is being run on the Web server or from the command line, use php_sapi_name().
Note that PHP will probably use a different php.ini file when called this way, and some variables (like $_SERVER["HTTP_HOST"]) are not available. This way of running PHP is called CLI (Command Line Interface).
I am developing a site locally using xampp. I would like to configure cron, so the dev environment is the same as the production environment. However, my system is windows vista. Is there any way to do this, or am I forced to accept that my dev and production environments will be forks?
I would highly recommend looking into Cygwin. I was using that when I wanted linux/unix utilities on Windows. It supports Cron as well.
Jacob
Btw this is a really good question...there are a lot of good scheduled task apps out there. The 1 I liked is Visual Cron at http://www.visualcron.com/
Running a scheduled task, is a very tough thing for web server/web apps to do...Because they have to check every x seconds or minutes to see if a task needs to be done, which can suck up cpu cycles for your web server/apps.
Which is why i prefer using an os based task scheduler or os task scheduling software, better performance I believe.
One trick that I had learned, was using php's -f command to create scheduled tasks without having to open a browser window, then it can run tasks...
On window's systems the only other way was opening up ie/firefox/opera whatever to run your web-based task..
good luck!
You could create a windows scheduled task to run cron.php (or call it in the browser).