Close PHP CLI window on script completion - php

I have a basic PHP script I am running through Windows Task Scheduler. The task runs well, but doesn't close the window once the script has completed. The script runs every minute, and we quickly get our server overran by the completed php.exe windows. Is there a way to close the CLI window if the script has completed?
I've searched around, but haven't found a clean answer. I'd prefer not to schedule another task to terminate php.exe if at all possible.
Thanks!

Related

How to Keep Windows Command Prompt Window Open After Running a PHP Script from Task Scheduler?

I run various PHP scripts from the Windows 10 Task Scheduler, and the command prompt window closes after running them. Is there a way to pass an argument from the task scheduler interface to cmd.exe so the Command Prompt window doesn't close after running a specific PHP script? If so ... How?
Thank you, Your comment led me to this useful bit of php code: exec(`cmd /K`);

Launch shell command from PHP

Okay, this is going to be a very weird request/question.
There is a very long running PHP script that needs to be launched by the user (admin) who is not very technically adept. When running the script through apache, it throws a timeout (502 or 504 Bad Gateway).
Let's just assume that apache can't be configured to fix the timeout issues.
I want to create a button in the admin panel that sends an AJAX call to a PHP script on the server, that PHP script will act as a proxy of sorts to launch a shell command. The shell command will then execute the long running PHP script with certain arguments... but I don't want it to wait for the long running script to finish. The proxy PHP script can exit and return true/false based on if the shell command actually started (this part is optional).
Essentially, have PHP launch a shell command which launches a PHP script.
How can I pull something like this off?
Have you tried shell_exec. It worked for me...
http://php.net/manual/en/function.shell-exec.php

Windows Server killing long running PHP process

I have a Windows 2012 Server that runs IIS and SQL Server 2012.
I am trying to run a PHP script from the command prompt. The script takes around 1 hour to run. If I run it straight from the command line like this - c:\PHP>php.exe "D:\Web\phpScript.php" it runs fine. Again it takes around 1 hour to run but it completes fine.
The thing is I need to run it from another PHP page. So this code - exec('start c:\php\php.exe "D:\Web\phpScript.php"'); in PHP runs the script. When I run it from PHP like that it runs good for around 30 minutes or so but for some reason Windows ends up killing the process after around 30 minutes.
I have been watching the task manager on Windows and cannot see any difference in the way the process runs compared to when I run it straight from the command prompt or when I use PHP to run the command. They both show up as a background process and look exactly the same in the task manager but for some reason Windows is killing the one that runs from PHP and not the one ran straight from the command prompt.
I have even tried running the PHP one in Realtime thinking maybe if it had higher priority it would not get killed but that did not help.
I am really stuck with this.
Any help would be great.
Thanks!
If it has to do with your PHP configuration, you can force allowing a certain execution time with this at the beginning of the script
set_time_limit(60*60*2); // allows 2 hours execution time
Then to execute the external file just use include('D:\Web\phpScript.php'); in the script.
http://php.net/manual/en/function.set-time-limit.php
Otherwise if its a server problem, beats me. You could, of course...run it in your web browser instead of in the command prompt, if PHP is installed on the machine.

Execute php script Sequentially and Continuously

I have developed a script which should execute continuously and sequentially. Now on creating a cron job for this script, it keeps executing asynchronously.
1) I kept a while loop in script and thought of executing this script once so i used #reboot to execute the script once but if apache craches then this script will not start executing by its own ??
2)Using * * * * * cron it executes this script but creates multi-thread and all cron overlaps on the server eventually.
I have ran out of ideas how to execute a server script continuously and sequentially even if apache server restarts.
You're asking for:
a script which should execute continuously and sequentially
That's the definition of a daemon. You can use upstart to easily create a daemon with your php code.
Here you can find a good article to explain how create a daemon with upstart and node.js (but is easy to adapt to a php script): http://kvz.io/blog/2009/12/15/run-nodejs-as-a-service-on-ubuntu-karmic/
cron is for repeating jobs on a timed basis (min interval: 1 minute). It's intended for scripts that eventually exit and need to be restarted from scratch. Sounds like your script is essentially trying to be a daemon - started once, then left running permanently.
Instead of cron to repeatedly start new copies of the script, simply start your script ONCE via an init script, e.g. /etc/rc.local
There are some ideas to deal with checking if a script is running in this question, and you could have a cron run every couple of minutes to ensure its running and starting it if not.

PHP process can't terminate after end of script

I run PHP script in command line from CRON job. Once it complete a process, sometime PHP process still run on task manager as well as command prompt is active in windows forever, unless i kill manaully process from task manager. I put log file, it shows a code is working properly and end of the script. However, it does not exit from process and consume memory of server. I wonder is this bug of php ?
I run application in windows server 2003 and php is in CLI.

Categories