Stopping and Starting Apache Using PHP (!) - php

I have two problems which are related.
1) I have a batch file that contains this:
net stop wampapache
net start wampapache
Which tries to stop and start my wamp server. When I double click the stop.bat file with the above it works successfully. When I try to run that from my PHP script, it stops the server but doesn't start it fully which I am guessing is because Apache is waiting for that PHP process to exit?
function php_kill(){
exec('stop.bat', $output = array(), $return);
return $return;
}
2) Is there a way to restart my webserver (apache) whilst keeping session variables that PHP needs available?
Thanks all

The PHP process is killing a process that, in turn, kills the PHP process. It's like going back in time and murdering your parents before they gave birth to you. I don't see how it can work.
One has to ask why this functionality is necessary. If you must do it this way, you should look into scheduling a service restart from the script. I don't know if this is possible via PHP and Windows.

The problem is related to the fact that exec waits for the process to end, but the process actually kills PHP so the whole thing gets stuck.
Running stop.bat as a background process should fix it. (here how to run a background process on Windows)

Related

How to stop PHP process in windows

I am running PHP using XAMPP on Windows I have this PHP page called interface.phpwhich calls another PHP script proxy.php using ajax.
It is something like $.get("proxy.php");
Now proxy.php just does exec(php process.php);
Everything works fine here. I press a button in interface.php, the ajax call is made to proxy.php, which starts executing process.php and returns a message to interface that the process has begun.
My question is, sometimes I need to kill the process.php. I can do it manually, no need for automation. I need to kill it because while testing, I see something wrong so I fix it and want to restart. Now when I check task manager I see nothing related to process.php. I end the httpd.exe and xampp-control.exe from task manager but the process still runs till it ends.
How can I kill this php script when I need to?
Just look for php.exe in the task manager. exec('php') is equivalent to manually typing php in CLI: a "standalone" (independent of the web server) php.exe process is started with CLI SAPI.
You can't but you can modify you php.ini with set_time_limit to have it done automatically if it takes too long.
http://php.net/manual/en/function.set-time-limit.php

keep a php script running all the time

I need to keep a php script running and alive on my server, the script runs and checks a DB for record, processes if needed, sleeps for 3 and then loops to the top of the script in an infinite loop. The issue is launching it, if I launch it via terminal (its running on an ubuntu system) using php script.php then if the terminal session is ended the script stops running.
So how can I go about launching the script so that it will remain running in the background.
Furthermore if I set up a cron job that runs once an hour and fires off a different script that check the primary one is still running and if not restarts it, how would I get the this checker script to check that the initial script is still running (even if its in sleep).
Any assistance will be greatly appreciated
If starting the script from the web is an option, then set time limit to "unlimited" (in the script itself):
set_time_limit(0);
and set ignore_user_abort to "true":
ignore_user_abort(true);
Then you can run the script as usual from the web, and it will run forever (until the process is killed or script exits in the usual way).
Of course, you can (and MUST) protect such a starter-script by password, e.g. by using HTTP authentication via .htaccess, so that somebody cannot start many forever-running scripts, which would lay down your server.
On checking whether another process is running, see question1, question2, or try searching "[php] check if process is running" here on StackOverflow. See also http://php.net/manual/en/refs.fileprocess.process.php.
If you want to run it from the terminal and keep it running permanently, look into GNU screen. It's a virtual terminal that keeps running in the background even when you close the terminal.
$ sudo apt-get install screen
With this, you can simply do:
$ screen php myscript.php
The script will load in a screen session which you can disconnect from, and even when you close the terminal it will keep running. To check up on it, simply call:
$ screen -x
Best part is screen is free software and is in the repo of all good distros (and other *NIX's if Linux doesn't float your boat).
Cron Job would be one solution:
More details about Cron job.
Another way to do it is to use Gearman or some other taks managers like in this post

php stop script process that is already running

I have a php program that does extensive curl requests to scrape web pages. It could be up to a million requests. I need to completely stop the script from running. Even though I stopped it in my browser, it is still processing requests. How can I stop it permanently?
You are just killing the request, you will need to stop apache to stop it for now. In the future redesign it so that the process looks for a kill switch (like the presence of a file) and stops processing if it finds it. Sounds like you are jamming a long running process into a php script, why not run it as a normal system process directly?
Assuming you are running the typical lamp stack, SSH into your machine, if necessary, and restart Apache.
If you are really going to perform long running tasks with PHP, I must suggest you consider using cron to run them or implement a task queue of some sort. It's generally a really bad idea to have these sort of things fired from a browser request.
Restart Apache. If you're using XAMP, stop and start it from the control panel.
If not, on Windows, go to task manager and end the apache.exe process. Then start it again.
Why the hell is everyone assuming you're running Apache? Restart your web server and it should be dandy. In the future, you could have a kill switch like (example):
while(!file_exists('stop.txt'))
Then just make that file when you're ready to stop ^.^ Or have a finite number of iterations before cutting off.

Modifying a running script and having it reload without killing it (php)

I have a gameserver running on Debian where players can edit an (already-running) php script via web to modify the game. However, once changes are made to the script and saved, the affects of the changes only happen once the script is killed and rebooted (I have to do this manually in terminal). Without giving shell access to users, how can the script know to reload a new version of itself once changes have been made? The script is running in a GNU Screen.
Although my overall knowledge on GNU screen, php, and linux commands are limited, I think there has to be a way for this to be done.
What would the easiest way be?
EDIT
To clarify, the script that people modify is a basic script that usually reads a server output log. So when the script sees "PLAYER_DIED" it writes to a file, which in turn is read by the server and does some stuff, like spawn a zone.
People edit this script right now with a basic web-based text editor linked to the php source code
There are numerous ways to achieve this, but it's hard to tell which method is the best since you don't share any of your source code.
Why not restart it within the same script that you use to let the players modify the script?
Another solution is to have a small cron-script that runs every minute to check if the file was changed. If so it will then restart the instance. In a worst case scenario, the players have to wait a minute until the changes are seen.
Also I'm wondering if you are using some kind of deamon that is running the actual script that is edited by the players or if you are running that script directly.
Shooting in the dark here.. but it seems like you will need to use PHP's process control functions to terminate the script and run it again once you know the script has changed. I have not tested this (at all), so take it with a grain of salt:
// signal handler function
function sig_handler($signo)
{
switch ($signo) {
case SIGHUP:
// Asked to restart. I guess you will need to call `exec` to start a new instance before terminating
break;
default:
// handle all other signals
}
}
// setup signal handler
pcntl_signal(SIGHUP, "sig_handler");
// Send restart signal to self (after you detect the script was modified):
posix_kill(posix_getpid(), SIGHUP);
There are limitations of using pcntl_* functions : PHP needs to be run as a CGI and you need to compile php with --enable-pcntl. Since you said you own the hardware, I guess this shouldn't be an issue.

running console app from php script

i start a linux console app from my php5 script, it starts ok but then termintates. I've tried using system(), shell_exec and tried starting as background process but to no avail it starts and then quits.
What i am trying to achieve is from a remote browser start a console app using a php5 script and then it should remain running (just as it would if i started it from a bash shell) , i then want to send commands (from a bash shell it would be keyboard strokes) to the console app from another set of php5 scripts. Hope its clear what i am trying to do.
If anyone could give some info on the best way about doing this, as i think i may have something fundamentally wrong.
I have a Debian Lenny box running apache.The console app is just a simple program that prints to stdout and reads from stdin.
How do you expect to send input to this app? Where is it listening for input?
It simply may only support interactive use, and exit as a result of that. Or, even simpler, it may terminate because it sees that is has no input (nothing piped in or nothing from some file) and since it's not connected to an interactive shell, it has nothing to do. There's no point in waiting for input from a user that doesn't have a way to interact w/ the application.
On every request, PHP starts up, compiles your script and executes it. After execution, the script exists. When the script exits, all of the resources it was using, including file handles, database handles, and pipes to other programs are terminated.
You're going to need to find another way to keep your program open and have PHP communicate with it. Otherwise, every request to your script is going to open a new copy of the program, and then both will exit when the PHP script is complete.
Unfortunately without knowing what the program is, it will be hard to offer suggestions on how to go about doing this.

Categories