WAMP - Do a PHP task every 1 minute - php

I have a script that sends a mail if a condition is true. In order to run the script I have to visit localhost/index.php. Every time I refresh the webpage the script checks if the condition is true, and if it is, it sends me an email.
How do I make this script run every minute without having to have my browser on refreshing the page every minute?
My current solution is to put this in my PHP file:
<meta http-equiv="refresh" content="60;url=index.php" />

Use Windows Task Scheduler. It allows you to run scheduled tasks like Cron for *nix.
update
PHP works from the command line like many other server side languages. This means if parameters are required you can pass them as such:
php C:\Path\to\script\script.php param1 param2
param1 and param2 are passed to your script in a similar way that $_POST and $_GET variables are. See PHP command line usage for more details on how this works. But it would mean very little change to your script which is nice.

You can run a PHP script without the browser by to following line put into
Start -> run
c:\program files\xampp\path\to\php\bin\php.exe -f c:\htdocs\script\script.php
Put this into the Windows Task Scheduler run input.
EDIT:
As the OP said they were using WAMP, PHP is actually in this folder structure
\wamp\bin\php\phpx.y.z\php.exe

In windows you need Task Scheduler for that.
you need to run php.exe file in some regular interval and that file will execute your php script for example,
C:\Path\to\php.exe -f "C:\Path\to\file.php"
thus, with Windows Task Schedular you can run php.exe file in some regular interval with specified condition. Here are the steps,
1. Open Task Scheduler
2. In Task Scheduler, Click on "Action -> Create Task" from menubar.
3. In "General" tab write "Name"- Name of Task and "Description" - Description for Task
4. In "Triggers" tab click on "New" and give triggering time as per requirement.
5. In "Actions" tab click on "New" and select Action - Start Program (which is Default).
And from browse button select your php.exe file from your php installed files.
And in "argument" give file path with Option ex. -f c:\Path_To_Htdocs\fileName.php (here -f is option).
6. In "Conditions" tab you can set condition when to run task means about the your pc status like idle/on AC power, etc. I suggest you to leave it as default.
7. In "Settings" tab you can specify some extra settings. I suggest you to leave it as default.
8. Finally As you done, Click on "Run" from "Action" to run it manually.

Related

Task Scheduler not working, (php script not executed) Windows 7

I have a local WAMP install.. and my understanding is that you can set up CRON jobs here (being Windows and all)..
I wanted to test out calling a .php script at a schedule time, using the Windows Task Scheduler.
I set up a simple .php script that sends an email (nothing more).
I tested that it works but going directly to the script in my browser (email received, no issues)
So I set about trying to configure a Scheduled Task on my Windows 7 machine:
Opened up Task Scheduler
Right clicked Task Scheduler Library > Create Task
General Tab:
Name: Email_Test
Run whether user is logged in or not (checked)
Actions Tab:
Action: Start a program
Program Script: C:\wamp\bin\php\php5.4.12\php.exe (I know I know.. I'll update to PHP 7 soon!)
Arguments: -f C:\wamp\www\cron_jobs/email_test.php
Start in: C:\wamp\www\cron_jobs\
Triggers:
Begin task: On a schedule
Daily: (setting time for a minute or two ahead of current time to test)
Repeat: every 5 minutes
Expire: Tomorrow
I only need it go off once a day, but wanted to put those settings in to keep testing.
I have yet to receive an email using this as a Schedule Task..
I originally had a .bat file set up as well.. but read some different tutorials that showed to do it this way? But I have no problem changing some path to a .bat file instead somewhere if that is my error/issue?
I also enabled History.. and all entries say things were successful? (Task/Action Started/Completed)
What am I doing wrong? and more so, how can I fix it? :)
Updated information:
I forgot to mention that for my WAMP/PHP install.. I am using SENDMAIL as my mailer.. perhaps this is part of the problem? DO I need to somehow allow the sendmail.exe to execute or something as well?
I do NOT want to directly use sendmail by itself.. I would prefer to use the PHP script.. eventually I want to add in a DB log, before I sent the email out.. but for now.. the PHP script ONLY has mail() function in it.
So upon reading.. I guess the php.ini file in APACHE is not the same as used in the CLI PHP execution?
I couldnt find php.ini # C:\wamp\bin\php\php5.x.x\php.ini
But I did find and update:
C:\wamp\bin\php\php5.x.x\php.ini-development
&
C:\wamp\bin\php\php5.x.x\php.ini-production
it didnt say.. but are these supposed to be renamed to be php.ini?
(although I'm not sure what those files are for.. but I made them match my php.ini file here:
C:\wamp\bin\apache\Apache2.X\bin\php.ini
Yet still.... no email arrives!
If I open script in browser.. I get email always?
(as that works fine for sending mail without issues?)

How do I convert Command-line script to a windows schedule task?

I have installed localhost/server in my machine and I need to run a php script using windows schedule task.
I am able to successfully run the php script from the command line prompt by exectuing the following commands:
cd C:/xampp/htdocs/Version/I7
php index.php cli/reminders
I have tried the following for the scheduled task but it is not working.
Program Script:
‪C:/xampp/php/php.exe
Arguments:
-f C:/xampp/htdocs/Version/I7/application/controllers/cli/Reminders.php
I also have a Scheduled task that triggers on 1:30 PM every if System is Active and below i described how i did it..
First Create a .bat file having code bellow
start /wait C:\xampp\php\php.exe -f C:/xampp/htdocs/Version/I7/application/controllers/cli/Reminders.php
then go to Windows Task Scheduler and From right side pane click Create Task then fill simple attributes like name and other things in Action Tab Create a new Action and then select run a program then select the batch file we created before..
go to Triggers tab create new trigger according to your specification fill out all the fields and save the task.

run a php page once a day using NAS synology

have a php page called cronEmail in the web folder. It incudes the code to end an email to specific users on the website. I want it to open the page once a day and send the email. The page has only php and MySql code to read the recipients of the email.
I am trying to use Task scheduler in the control panel to run the page. I create a user defined script and in schedule I set the time to a certain time and to only run daily once a day.
In the rum command i have tried numerous ways to run it on the time but every time it just passes and does nothing. an example of what I put in for the script is
/web/cronEmail.php OR
chmod 755 /volume1/web/cronEmail.php
There are only two of a many can anyone point me in the right direction
Thanks a million
Seems like you want a cronjob, in the terminal open cron with:
crontab -e
then at the bottom of the file place this
0 4 * * * php /url/to/folder/cronEmail.php
Not sure if you found another solution, but this is what worked for me. If you put the following in the "Run Command" section, it should work:
php /volume1/web/cronEmail.php
You can also create an error log by doing the following:
php /volume1/web/cronEmail.php>> /volume1/web/errors.log 2>&1
If you don't use the php at the beginning, and open up the error.log file that is created, what you'll see is that the Task Scheduler doesn't seem to know that it's looking for PHP, and doesn't recognize the script.

Run Cron Job on PHP Script, on localhost in Windows

I have a php script and want to run it on an schedule. I am using local web server on windows (WAMP server) and need a way to run my_script.php every 10 min.
How to run a cron job on a PHP script, on localhost in windows?
recently I had sort of problems to run a cron job on a php script on localhost (WAMP server) in windows 7, when I was on a test to chronically fetch some links from www out there.
By the way I am sharing this for anyone that is on the same thing.
You will need a shellscript to run chronically, using Windows Task Scheduler. Also you will need a batch script (script.bat) to call the php.exe and run your php script (here called as my_process.php)
shellscript.vbs
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\path\to\script\script.bat" & Chr(34), 0
Set WinScriptHost = Nothing
script.bat
"C:\wamp\bin\php\php5.4.12\php.exe" -f "C:\wamp\www\website\my_process.php"
Now, we are ready to set the Windows Task Scheduler to run shellscript.vbs at the required time interval:
Open Task Scheduler from windows Start menu
Go to Action menu and hit Create Task...
in General tab, fill the Name and Description fields as you want
in Triggers tab, hit New button.
from Begin the Task dropdown, select On a schedule and choose Daily
from Advanced settings section, select Repeat task every as you want and set for a duration on Indefinitely.
on Actions tab, from Action dropdown, select Start a program.
on the Program\script box, enter path to shellscript.vbs like C:\path\to\shellscript.vbs.
leave Add argumentts (optional) section empty.
in Start in (optional) box, enter parent directory of shellscript.vbs like C:\path\to\.
Hit upvote on this tutorial :) Have fun.
To setup a Windows machine to run cron.php at a specific time follow the specific instructions below. This can be useful if you are not familiar with Linux/Unix, or if your web host does not offer the ability to run cron jobs; you can run them remotely from your own computer.
Note: These instructions were written for Windows XP but should be similar in other versions of Windows.
https://www.drupal.org/node/31506
If you use answer from Trix and get same problem as Metafaniel:
I have a problem, with this procedure, the task is beinge executed,
however my php script it's not running, I got the "Open with"
dialogue. If I see the properties of my task, it states: ActionName
C:\Windows\system32\OpenWith.exe even if I set it to the vbs file as
you suggested. What am I doing wrong?
you should use this:
Instead "enter path to shellscript.vbs like C:\path\to\shellscript.vbs." use "C:\Windows\System32\wscript.exe"
Instead "leave Add argumentts (optional) section empty" use (with quotes):
"C:\path\to\shellscript.vbs"
After many failed attempts at using Windows Task Scheduler this is the method that worked for me on my Windows 10 Professional machine. I just skipped adding a .vbs and .bat file and posted the PATH to the script file directly.
Under "Actions > Settings" Where it gives the option to browse for a "Program/Script" that's where I put the full PATH AND Filename to the script I wanted to run WITHOUT quotes.
C:\Server\Scripts\script.py
Then in the same area under "Actions > Settings" right below the text box where you just entered your "Program/Script", it has the area that says "Start in (optional)". That's where I put the PATH to the program that runs the script (ie: python.exe in this instance). But I did not specify "python.exe" directly I used the PATH ONLY without quotes like shown below.
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64
If you installed Python directly without Visual Studio it will be under a different path probably something like "C:\Program Files\Python\Python37" or something similar if I remember correctly...
Little bit late but just for someone who don't want it to be lengthy and don't want to waste time in testing (as the aim is to host it to server), can do one thing by adding http content refresh in the html meta tag of that php file and add the required timer to it and run it in the browser.
NOTE: You have to make the system remain open untill you don't get the required output.
Again I am saying this is not the perfect and proper way but ya can save someone's configuration setting time.

Use php to set cron jobs in Windows

I am looking for a way to set cron job using PHP. All I would like to do is run a PHP script at a specific time. The user first inputs a time in a script, according to the time specified the server will run the script. I am using windows 7 and xampp.
What I have found is:
Create a php file that calls the cron.php file: Using notepad (or whatever), paste the following into a new file:
$data = file(“http://pearl.supplychain.com/cron.php”);
you’ll need to put it inside the regular php tags, with the “less than sign” ? php at the front, and the ? “greater than sign” at the end. (I can’t seem to just type that because it is “suspicious content” and drupal doesn’t allow it)
Save it as executecron.php, into the same directory as cron.php (htdocs).
Set up a scheduled task that calls this regularly:
Open Start–All Programs–Accessories–System tools–Scheduled tasks.
Double-click on scheduled tasks.
Set up a Daily task that starts at 12:00 am and runs every half hour (or whatever) until 11:59 pm. Tell the task to “run” the following:
C:\cms\xampp\php\php.exe c:\cms\xampp\htdocs\executecron.php
(On this system, php.exe is installed in C:\cms\xampp\php, but you’ll probably have to change the path).
As you can see, to do this, one must Open Start–All Programs–Accessories–System tools–Scheduled tasks.
Can it specific by php code or using another way to do this? Because i want all the work done on php / server instead of need my user config the cron job themselves. Which means i want my php code can set the cron in server and server will look at the cron?
To stefgosselin:
To create the batch file
Open Notepad.
Paste the line "C:\xampp\php\php.exe C:\wamp\www\index.php"
Click "File" -> "Save As"
Ensure "Save as type:" is set to "All Files"
Save the file as "cron.bat" to your C drive
To schedule the batch file to run
Open Command Prompt
Paste the following "schtasks /create /sc minute /mo 20 /tn "PHP Cron Job" /tr C:\cron.bat"
Press Enter
This will make the script run every 20 minutes, the first time 20 minutes from now.
I am able to create a bath file using php, however, are there any way to Paste the following "schtasks /create /sc minute /mo 20 /tn "PHP Cron Job" /tr C:\cron.bat
using php instead of using os? Thank you
Thank you
I found my answer to that question at waytocode.com
They provide 3 possible solutions to run cron jobs on Windows:
Solution-1 using Task scheduler
In your Windows 7/windows 2005/2008.
Go to Startmenu->All Programs->Accessories->System Tools->Task Scheduler->create Task
In the new window:
General (Give the Task name and for testing you can select “Run when User is logged in“)
Trigger (You can Select the running interval as “daily,weekly,monthly”. )
Action (This is most important part. Select a Mozilla firefox as the “program/script” and in the Argument provide the URL to fire with Mozilla firefox).
Solution-2 using Task scheduler and PHP from your XAMPP server
In Windows Xp,no need to copy or install anything(Already PHP is installed on the server like XAMPP)
Goto Task scheduler
Create a task give Running time, then in avanced setting option in the “RUN” command textbox type
C:\xampp\php\php.exe -f c:/xampp/htdocs/waytocode/mycron.php
In Windows 7/server 2005/2008
No need to copy or install anything(Already PHP is installed on the server)
Create a task give Running time in Trigger setting.Then in Action setting option in the “Program/Script” command textbox type
C:\xampp\php\php.exe
and in the “Add arguments (optional)” type
-f c:/xampp/htdocs/mycron.php
Solution–3 install a Windows exe file that will simulate the cron job from *nix system
I don't like to install any exe file to my servers or development machine,
but I'll provide the solution as they posted:
In Windows Xp,Copy all 2 DLL file with wget.exe to the C:\windows folder
Create a task give Running time then in avanced setting option in the “RUN” command textbox type
C:\Windows\wget.exe -q -O NUL http://localhost/mycron.php
In Windows 7/server 2005/2008 ,Copy all 2 DLL file with wget.exe to the C:\windows folder
Create a task give Running time then in avanced setting option in the “Program/Script” command textbox type
C:\Windows\wget.exe
and in the “Add arguments (optional)” type
-q -O NUL http://localhost/mycron.php
Solution-4 using a .bat file and the task scheduler
I found it here at Stackoverflow and it is similar to the first 2:
Create a cron.php file (the code you want to execute at a regular interval)
Create a CRON.BAT file, copy and past the below code in the file
D:\xampp\php\php.exe D:\xampp\htdocs\Application\cron.php
The path I have written is according to my xampp and cron.php file, update the path of files according to your system directory
To schedule a task
Click on start > All Programs > Accessories > System Tools > Scheduled Tasks
Or you can go directly
Control Panel > Scheduled Tasks
Right click in the folder
New > Schedule Task
Give appropriate name to the Task.
In the RUN text field… Type the complete path of the CRON.BAT file
in my case it is
D:\xampp\htdocs\Application\CRON.BAT
Set the schedule of the job, you can use advanced button if required.
Solution-5
I don't like it either because one script can't depend on someone else website but it is a solution anyway.
Use an external online cron job service.
https://www.google.ca/search?q=cron+job+online+service
Chose one solution that it is more appropriate for you. Hope this will help someone.
UPDATE
Solution-6 (Based on the answers below, and works with CodeIgniter too!)
Create the cron.bat file and write the following command and save it.
#ECHO OFF
c:
cd C:\Program Files\Internet Explorer
START iexplore.exe http://localhost/path/to/cron/job/1
Create a task give Running time in Trigger setting.Then in Action setting option in the “Program/Script” command textbox type
C:\xampp\path\htdocs\folder\includes\cron.bat
END UPDATE
Answering your question:
Can it specific by php code or using another way to do this? Because i
want all the work done on php / server instead of need my user config
the cron job themselves. Which means i want my php code can set the
cron in server and server will look at the cron?
There are other ways to do this:
Using cron manager from within PHP
Using cron manager from within PHP
Managing Cron Jobs with PHP
http://code.tutsplus.com/tutorials/managing-cron-jobs-with-php-2--net-19428
Unfortunately, all solutions with PHP needs a *nix server type and / or cPanel and are more or less complicated to implement.
Ok, if I understood correctly, you would like to have a cron job created on a system, without a user having to create the task.
Basically, this can easily be done in a .bat file, (that could even be called from php).
The schtasks app can easily automate the creation of a scheduled task. For example:
schtasks /create /tn UNO /tr YOURAPP.EXE /sc HOURLY /mo 2
See the official MS support page for more info on this nifty application.
Another option that can even be easier for the user is to use an installer. I have not created that many windows apps myself but did have the pleasure of playing with NSIS, and this app also has options to create scheduled tasks, among many many other features way too numerous to name here. Highly recommended if you need a user-installable package.
Hope that helps, happy coding friend.
Another great tool is available for free on Windows, nncron. It uses the exact same syntax than unix' cron:
http://www.nncron.ru/
I find it easier to manage that this horrible schtasks :)
The lite version is the one I would suggest to use. The full powered version supports scripting in the config files. It could be handy but somehow over killed for normal cron jobs.
Steps Create Cron in Windows :
Step 1> Create .bat file
1. open notepad.
2. click File -> Save As.
3.Give the file a name i.e. MyBatchFile.bat Underneath the filename box change the file type to All Files. The .bat on the end of the filename will instruct the PC to open it as a batch file.
Step 2> IN MyBatchFile.bat write following command
ECHO OFF
START File Full Path write here like http://www.google.com
Step 3>
Open Start–All Programs–Accessories–System tools–Scheduled tasks.
Click on create task from right side bar.
name:any one
trigger: set time from drop down and select per day
Action:select .bat file
Click on OK button
Steps Create Cron in Windows OS:
Step 1> Create .bat file
1. Open notepad.
2. Click File -> Save As.
3. Give the file a name i.e. MyBatchFile.bat Underneath the filename box change the file type to All Files. The .bat on the end of the filename will instruct the PC to open it as a batch file.
Step 2> IN MyBatchFile.bat write following command
ECHO OFF
START File Full Path write here like http://www.google.com
Step 3>
Open Start–All Programs–Accessories–System tools–Scheduled tasks.
Click on create task from right side bar.
name:any one
trigger: set time from drop down and select per day
Action:select .bat file
Click on OK button
Another quite simple approach is powershell, to fetch the content of your php-file.
Task-Scheduler, target: Powershell.exe, use arguments:
-Command "(New-Object Net.WebClient).DownloadString('http://myhost/cron/cron.php')"
(Ups, old post)
It is the complete solution I have tried for my own project for windows 10.
1) Go to "start" menu and search for "task scheduler" and open it.
If you are facing any issue of permissions, open it as "run as system administrator".This option is exactly below "open".
And still you are facing issues of permissions contact your system admin.
2) Click on "new task" in right sidebar and a window will be opened.
3) In this window "general" tab will be selected by default. Give a suitable name to task and if require provide short description.
4) Go to "triggers" set here frequency at you want to run it and start date-time, if require expires date-time.and set all require parameters.
5) Got to "Actions" tab. This is the most important part.
In "Program/script", write the absolute path of "php.exe".
In "Start in", write the absolute path of the folder where your intended php file for cron is.
In "Add arguments", write the name of the php file which you want to get run thought the task job.
In "Add arguments", you can also specify the arguments to the files space separated after php file name. and if it has text or special characters, enclose it with double quotes.
If you provide arguments to scripts, you will receive them in $argv[ 1 ], $argv[ 2 ] and so on respectively.
For more details visit: https://medium.com/#shraddha_kulkarni/run-php-cron-in-windows-513fb1aa53a5

Categories