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.
Related
I schedule a new job on cronjob. But it's not working. Is there any way to run cronjob in cpanel using commands.
There is a bunch of way to running script with CRON. I do use this way on my web application most of the time when I need to use CRON.
The first step is to choose a setting. The setting means when you want to run this script.
and after then simply just use CURL with Cron to get this work properly like this: curl http://example.com/script.php on command box and click on the add button and whola! You have successfully added your first CRON Task.
You may have a look at this picture below to get it properly
Go to CRON JOB option on your cpanle menus
Set url like this and set time according to your functionality
php /home/username/public_html/filename.php
Run it in putty
/usr/bin/php /var/www/html/project_name/artisan schedule:run 1>> /dev/null 2>&1 this is for laravel project
you can simply use like this - /usr/local/bin/ea-php74 /home/username/domainname.com/script_path/yourfile.php
replace username to your cpanel username, domain name to your domain name, script path where you want to run it, then replace the main file for corn job in yourfile.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.
I am trying to set up a cron job on my localhost to call a php script every half an hour 24/7. Following are my steps:
1) I have created a cron.php file to call myscript.php:
<?php
echo exec('*/30 * * * * C:\BitNami\wampstack-5.4.21-0\php -f C:\BitNami\wampstack-5.4.21-0\apache2\htdocs\myscript.php');
?>
2) Created a cron.bat file to run the cron.php on browser:
"C:\Program Files\Mozilla Firefox\firefox.exe" "localhost\cron.php"
3) Then I have set up a windows 7 task scheduler to call cron.bat every day.
I welcome any idea on how to set up a cron job as my approach doesn't do anything.
Thanks
This is not an answer, but only pointers you need to check before you proceed further.
Crons are available only on unix systems, not windows.
While debugging cron or anything similar, schedule them for every minute or (any such acceptable smaller frequency), and once they work every minute, only then schedule them for the original frequencies. That will save you some time during debugging.
Its an incorrect call to exec. You don't have to pass all those * in it
Since this is windows, You want to do php.exe and not C:\BitNami\wampstack-5.4.21-0\php -f which would have been done in linux (with proper path)
Your .bat file is currently scheduled daily, you need to schedule it every half hour.
you don't need to creat .bat file to run php script
follow the step to setup cron job for php interpreter..
step 1- Then I have set up a windows 7 task scheduler to call php script by giving path of php interpreter like C:\xampp\php\php.exe filename.php by setting desired interval
dude, you cannot define cron jobs in wamp,
the cron is a linux feature and you are running the wamp on windows.
you can run the script from the command line (of from cygwin, much better),
something like "/php.exe "
but you cannot define crons on wamp
but you can setup the scheduler to run a .bat file that contains the next code
cd/
cd "Program Files (x86)"
cd GnuWin32
cd bin
wget.exe http:// localhost/... (your file)
echo DONE
For my reminder system app I need to send emails, one day before the event . The database stores the reminders with date,time,user_id. Is there any way I can schedule an email that automatically gets sent a day before the event occurs ?
1) Create a batch file that will run the script
Open up notepad, add this command, with your own attributes:
"PATH_TO_PHP.EXE" -f "SCRIPT_TO_RUN.PHP"
So it may look like this:
"C:\Program Files (x86)\PHP\v5.3\php.exe" -f "C:\tasks\task.php")
Save this file as a .bat file (Batch file).
(OK so creating a batch file isn't completely necessary but what if you wanted to expand on this task? Right?)
2) Have task scheduler run the task
Open up Task Scheduler (You'll find it in Accessories > System Tools, or just search for 'taskschd.msc')
Create a task.
Give it a name, Create a trigger for the specified day.
Now go to the Actions tab, hit New and it should say 'Start a program' by default.
Browse to the batch file that you just made, click OK and you're done.
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