I've written a daily batch file which uses FTP to login to my webserver and download a CSV full of new members using mget members.csv which is created on my site by PHP.
I've also have a php page on server which emails me with these new members. This php executes when I load the page in a browser but is it possible to execute it from the batch file?
I could also keep these members in a database if it's easier/securer but then ideally I wouldn't like to hold sensitive database login details in a batch file...
Many thanks
Using a scheduled batch file under Windows you could use the start command to execute a browser instance requesting the URL of your PHP script (which generates the email).
Put this in your scheduled batch file:
start www.stackoverflow.com
This one would use the systems default browser. To start a specific browser instead, you can use:
start /d "C:\Program Files\Mozilla Firefox" firefox.exe www.stackoverflow.com
start /d "C:\Program Files\Internet Explorer" iexplore.exe www.stackoverflow.com
:
You need to replace www.stackoverflow.com with the URL of your PHP script, of course^^
Why not just setup a cron job to check every x minutes/hours for populated csv file and send you the file if it is populated?
how to setup cron job
Related
So I am using PHP exec() to run a batch file on my server:
echo exec("printCountries.bat");
The batch file opens Microsoft Access 2007 and runs a macro to print a form and then close Access:
START /MIN /B msaccess.exe "C:\inetpub\wwwroot\system\reports.accdb" /X printCountries
I can run the batch file from the command line successfully and the form prints to our network printer (this is for a company intranet) and closes Access. But when I run the PHP script, the batch file only runs the Access Process but does not perform the command line switches or open the database file.
I have configured PHP to have the right privileges on IIS, I am just stumped as to why the command line switches don't work and the batch file won't open the database.
Any ideas?
here is the echo output:
C:\inetpub\wwwroot\scale>START /MIN /B msaccess.exe "C:\inetpub\wwwroot\system\reports.accdb" /X printCountries
It looks like you have some access rights problem.
Did you try to run in PHP this echo exec("whoami");
What username you get?
Something like "nt authority\iusr" ?
You have to give to the default IIS user (i.e. IIS_IUSRS) full access to the path that your scripts lives.
In addition you can change (only TEMPORARILY huh?) the default application pool user from ApplicationPoolIdentity to LocalService or LocalSystem to test the results...
If I were you, I would created a new Application in IIS then I would created a new Application Pool and I would assigned it to this app. Then I created a new user that will have access to all the appropriate directories (web dir and where the bat is located).
If nothing of the above worked I would test my php application from my command line (locally) using the php command in my local command prompt.
I am neither good with PHP, nor with Access. However, when you invoke the batch file from PHP, please create an instance of cmd.exe and pass "/c printCountries.bat" to cmd.exe. In other words, the command run by PHP exec should look something like this (assuming PATH is set correctly):
cmd.exe /c printCountries.bat
Also, inside printContries.bat, I think you will need to wait until the access process terminates. So it should look like
START /WAIT /MIN /B msaccess.exe "C:\inetpub\wwwroot\system\reports.accdb" /X printCountries
What is the best way to generate an excel file and inform the user after that?
I am using PHPExcel to generate an excel file from an MSSQL Server to a webserver and allow a user to download it using a link. The problem is that the each time we try to execute the PHP script it always throws a fast-cgi timeout error. The script needs to read up to 2000 - 5000 rows of data.
We tried to execute it via command prompt using exec() and Shell. It successfully generates the file in the server, but we don't have a way/method in informing the user after the script is completed.
exec() should return the result of running the external program - can't you use it? You can move generated file to a directory that is reachable for user and just give him the URL to the file.
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.
I have a rss feed (xml) that is updated frequently. I need to send notifications to APNS if new update is avaliable on rss feed (xml). So far I know I can parse xml with php and send the result to APNS with my local Apache Server on Mac (MAMP).
But I do that by simply entering php xxx.php command on terminal window.The File xxx.php first parses the xml file and then sends the results to the APNS server.
My question is
How can I periodically run this php file on a server?
Do I need a Virtual Private Server?
If so what is the code or function for running a php file continuously or every 10 mins ?
To run a file periodically you have to manage cron jobs, I wanted to describe what you have to do but I found a good profound article, It's worthy to have a look on it.
http://net.tutsplus.com/tutorials/php/managing-cron-jobs-with-php-2/
You can use cron on a linux/mac server to run periodically any command (for example: php xxx.php)
I) In a command prompt you can edit the crontab with: crontab -e
II). At the end you add a line for every script you want to run periodically. Each line must follow this pattern:
`minute` `hour` `day of the month` `month` `day of the week` `command`
For example if you want to run the script every hour you add this line:
* */1 * * * php xxx.php
III) Then exit with CTRL+X and save.
More examples on Wikipedia.
What you need is a cron job: See this Wikipedia entry.
Check to see if your host allows you to run cron jobs, and if not -- consider contacting them to see if they can set it up for you.
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