I have a piece of PHP code running on Windows that I have scheduled to run after every hour using Windows Task Scheduler. The problem is that the command line opens up everytime the code is run, whereas I was the execution to happen invisibly. Is there a way to do that?
http://docs.php.net/install.windows.manual
The PHP package structure and content
[...]
|-php-win.exe -- executes scripts without an opened command prompt
So, instead of php.exe use php-win.exe
Related
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.
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'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 have 55 php files that I would like to run simultaneously from the command line. Right now, I am running them in multiple CLI windows using the code:
php Script1.php
I would like to be able to call one single php file that would execute all 55 php files simultaneously. I have been reading about how to make the command line not wait for the output, but I can't seem to make it work.
This thread:
How to run multiple PHP scripts from CLI
suggests putting an & at the end of the command to run the command in the background, but using the xampp CLI this doesn't seem to do anything.
Any ideas greatly appreciated.
Brian
By mentioning XAMPP, I assume you are on windows. I think what you need is the start command. You probably need start php Script1.php. For more info, do a
start /?|more
Linux
Apart from adding a &, you also need to redirect output to somewhere - otherwise your php process waits until the other process finished, because there could be more output:
exec('/path/to/program & > /dev/null 2>&1')
You could use the php forking mechanism. Read about it here: http://php.net/manual/en/function.pcntl-fork.php
I am using windows OS with wamp system installed.
My question is :
How to stop or terminate the running command of PHP exec() launched? In my situation,I will launch several command at the same time, and is it has something like IDs which corresponding,so I can stop the running command I want?
Thank you very much!!
I'm not sure if the same logic applies when using Windows OS, but you will want to do it in two steps.
1) When you launch the process from php - you'll need to fork, so that your actual PHP doesn't hang. I'm used to doing it in perl, i'm sure php does it too.
2) Whatever program you launch, if you can get its PID then store it in a database. If it's a unique executable name then just run taskkill /IM uniquename.exe or taskkill /PID #### if you have the PID