I'm trying to open a Windows program from PHP using exec() on a local machine. Is it possible to start a system program (on Windows 10 if it's relevant) that runs in the foreground using PHPs exec function?
This line:
exec("C:/Windows/notepad.exe 2>&1");
Causes Microsoft's Notepad to open in the background (verified it is actually running using task manager) but I have no access to it, i.e., it doesn't open a window. How do I get it to run in the foreground so I can actually see it and interact with it?
So this seems like an utter ball ache to achieve using exec() for your average coder. There's another way to achieve this result: Have PHP generate .bat files using file_put_contents() with instructions to open a given file path and then auto-delete itself, like so:
#echo off
Start "" "C:\Path\To\File\SomeFile.txt"
del %0
This method requires some kind of task scheduler to monitor a given folder and execute the batch files as they come in. I believe PowerShell can do this, and possibly the Windows Task Scheduler. I think Linux has Cron.
Related
I have a PHP script inside
c:users\xampp\htdocs\myapp\myphpscript.php
What I want is to make a windows service that runs this PHP script in the background.
So I searched for some questions here but they were not effective.
Is there anyone who can help me to make a windows service using nssm that executes for my script.
Create a batch file with following content and put it in windows startup ;)
start /b path-to-php-installation\php c:\users\xampp\htdocs\myapp\myphpscript.php
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.
I am wondering if a PHP script can be executed from a shell command line.
Does a script executed from shell have the exact functionality if executed from the browser? or is there a difference in coding.
Is it better to run a script from shell for performance and also is it better to run it from windows or unix/linux
I am asking all these questions because, I am suppose to develop a PHP script that can fetch some data from http headers of some urls listed in a MySQL db and then store the data in the database.
Can you guys point me to the right direction please, Do I need ubuntu, or is there a shell that can run php from windows? all I have at the moment installed is WAMP, which has mysql, php and apache server.
I am sorry for being a novice.
thanks for your kind help
I am wondering if a PHP script can be executed from a shell command line.
Yes
Does a script executed from shell have the exact functionality if executed from the browser? or is there a difference in coding.
It won't have $_REQUEST and friends populated, and $_SERVER won't have server information in it.
Is it better to run a script from shell for performance
Maybe. It avoids the overhead of runnning a webserver. It stops you having cached versions in memory for faster re-execution.
and also is it better to run it from windows or unix/linux
Benchmark it.
I am asking all these questions because, I am suppose to develop a PHP script that can fetch some data from http headers of some urls listed in a MySQL db and then store the data in the database.
There doesn't seem to be any need to involve a web server for that.
Can you guys point me to the right direction please, Do I need ubuntu, or is there a shell that can run php from windows?
The standard Windows shell can.
all I have at the moment installed is WAMP, which has mysql, php and apache server.
You'll need the command line version of PHP. I've no idea if WAMP includes it or not.
I am wondering if a PHP script can be executed from a shell command line.
It's possible either by executing:
$ php -f your_script.php
Or by inserting #/usr/bin/env php into the first line of the script and by making it executable.
$ head -n 1 your_sript.php
#/usr/bin/env php
$ chmod +x your_script.php
$ ./your_script.php
Note: this example only works on UNIX systems.
Does a script executed from shell have the exact functionality if executed from the browser? or is there a difference in coding.
You can use the same Syntax/Functions etc. The only difference is that there are command line arguments in $argv and some other values in the $_SERVER variable.
Is it better to run a script from shell for performance and also is it better to run it from windows or unix/linux.
That doesn't really matter. For your usecase you don't really need a webserver, and a full featured GUI. The advantage of having a command line tool is, you can combine your program with other program available like grep etc.
Can you guys point me to the right direction please, Do I need ubuntu, or is there a shell that can run php from windows?
You don't need ubuntu, you can also execute a shell script from windows. The PHP executable is located in the %PATH%. This question will help you in order to do this: https://superuser.com/questions/284342/what-are-path-and-other-environment-variables-and-how-can-i-set-or-use-them
Then simply open cmd.exe and execute a script using php -f your_script.php
Yes, PHP can be run from command line.
No, there aren't any differences in coding.
The only difference is that it's not Apache running the script, but the user you are currently logged in as. That could mean different privileges on certain maps and folders.
Yes you can execute PHP from the command line using:
/path/to/php.exe /path/to/script.php
The main difference is that it doesn't run through Apache, so you won't have things that rely on it (like some $_SERVER data).
Also it won't be subject to timeouts on the command line, unless you have a PHP limit set.
Take a look at http://php.net/manual/en/features.commandline.php for more info.
I got the 140dev Twitter framework (which uses the Twitter phirehose) manually
running (via the webbrowser on my local wamp server), but I can't
figure out how to run both get_tweets.php and parse_tweets.php as a
background process like with SSH commands:
nohup php script.php > /dev/null &
Some of you started using (the Windows equivalent of) cronjobs, but
this isn't the right way to go. I think this is because of creating
multiple connection (or re-connections) to the Twitter streaming phirehose isn't allowed?
How can I run both PHP scripts (get_tweets.php and parse_tweets.php)
as a background process on my local WAMP server (and later on a VPS)?
Just to clearify:
I am using a WAMP server (first to test a little bit and later to
run it on a VPS)
Using LAMP or any *nix server/system isn't an option (due to time,
experience and lack of skills)
I have searched for solutions (on google and stackoverflow), but they are either not working or not clear enough for me (I am new to this)
Thank you in advance.
Find the php/bin folder where the php.exe is located. Copy the folder path and add it to your PATH environment variable (Follow this for instance to edit your PATH variable.
Once this is done, you'll be able to execute php in the command line from anywhere. Just start php script.php with a command line in the right folder and it should work. There might be some configuration to make so that the php in command line uses WAMP's php.ini.
Say I want to execute an ant command on a linux server using php on a website...
Can I do say, /home/user/apache-ant-1.8.2/bin/ant compile - where compile is the command?
Basically I need to start up a few things with ANT in a few different directories. I would like to do this from a php webpage, can shell_exec perform this?
Yes, shell_exec can run any command (with appropriate permissions).