By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,854 Members | 1,871 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,854 IT Pros & Developers. It's quick & easy.

How to stop program?

P: n/a
Hi All,
I have a php program which calls a c++ program using exec. However when
user presses stop button in the browser or closes it this program keeps
on running on the server. How can I stop it according to user's
actions.
Thanks in advance.
Sal

Jul 17 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
shyren wrote:
Hi All,
I have a php program which calls a c++ program using exec. However when
user presses stop button in the browser or closes it this program keeps
on running on the server. How can I stop it according to user's
actions.
Thanks in advance.
Sal


How can you detect the user pressed the stop button or closed the browser?
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 17 '05 #2

P: n/a
I don't know how to detect that user pressed the stop button or closed
the browser. That is one part of the problem.
Sal
Jerry Stuckle a écrit :
shyren wrote:
Hi All,
I have a php program which calls a c++ program using exec. However when user presses stop button in the browser or closes it this program keeps on running on the server. How can I stop it according to user's
actions.
Thanks in advance.
Sal

How can you detect the user pressed the stop button or closed the

browser?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================


Jul 17 '05 #3

P: n/a
shyren wrote:
I don't know how to detect that user pressed the stop button or closed
the browser. That is one part of the problem.
Sal


That was more of a rhetorical question. The answer is: you can't.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 17 '05 #4

P: n/a
shyren (sa********@yahoo.com) wrote:
: Hi All,
: I have a php program which calls a c++ program using exec. However when
: user presses stop button in the browser or closes it this program keeps
: on running on the server. How can I stop it according to user's
: actions.
: Thanks in advance.
: Sal

If the c++ program is sending data to the file handle that is going back
to the browser (normally stdout), then the c++ program _might_ receive an
error when it prints to stdout if the browser is stopped, depending on how
the web server works.

If a web server detected that the file handle to the browser was closed
then I suppose some web servers might signal the cgi. The script and its
children could arrange to be interupted by that.

I would experiment with those two things on the server in question to see
if your program can detect either one.

Both cases depend on the browser closing the socket when you hit the stop
button, which is not something you control.

--

This space not for rent.
Jul 17 '05 #5

P: n/a
NC
shyren wrote:

I have a php program which calls a c++ program using exec.
However when user presses stop button in the browser or closes
it this program keeps on running on the server. How can I stop
it according to user's actions.


1. Make sure PHP is not ignoring user's abort commands:

ignore_user_abort(false);

2. Write a function that would use exec() to send a kill
signal to the C++ program. On Unix, you could do
something like this:

function kill_my_cpp_prog() {
// call 'ps -auxww' to get the list of running processes
// find the pid for your C++ program
// call 'kill [pid]' to stop your C++ program
}

Refer to your OS manual for details of ps and kill usage.

3. Register that function to be executed at shutdown:

register_shutdown_function('kill_my_cpp_prog');

So when a user aborts, it will trigger execution of
kill_my_cpp_prog()...

Cheers,
NC

Jul 17 '05 #6

P: n/a
On 20 May 2005 10:07:02 -0700, "NC" <nc@iname.com> wrote:
shyren wrote:

I have a php program which calls a c++ program using exec.
However when user presses stop button in the browser or closes
it this program keeps on running on the server. How can I stop
it according to user's actions.


1. Make sure PHP is not ignoring user's abort commands:

ignore_user_abort(false);

2. Write a function that would use exec() to send a kill
signal to the C++ program. On Unix, you could do
something like this:

function kill_my_cpp_prog() {
// call 'ps -auxww' to get the list of running processes
// find the pid for your C++ program
// call 'kill [pid]' to stop your C++ program
}

Refer to your OS manual for details of ps and kill usage.

3. Register that function to be executed at shutdown:

register_shutdown_function('kill_my_cpp_prog');

So when a user aborts, it will trigger execution of
kill_my_cpp_prog()...


Changing to use proc_open so you can then use proc_terminate might also be
worth thinking about.

There's also posix_kill() for sending signals (but you still have to get the
pid first).

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #7

P: n/a
NC
Andy Hassall wrote:

1. Make sure PHP is not ignoring user's abort commands:

2. Write a function that would use exec() to send a kill
signal to the C++ program.

3. Register that function to be executed at shutdown:
Changing to use proc_open so you can then use proc_terminate might
also be worth thinking about.


Definitely.
There's also posix_kill() for sending signals (but you still
have to get the pid first).


Also, the POSIX extension is often disabled for security purposes...

Cheers,
NC

Jul 17 '05 #8

P: n/a
According to given suggestions I wrote a program for testing. To see
whether I capture the user's abort I write to the file in tmp directory
with writing permissions set. But still I am not able to detect it. i
am new to php and more help would be highly appreciated. Thanks..
__________________________________

<?php
ignore_user_abort(false);
function kill_my_cpp_prog()
{
$file=fopen("tmp/num","w");
fputs($file,"number2");
fclose($file);
echo "in";
}
#for($i=0;$i<10000000;$i++)
# echo $i;
exec("sleep 5");
register_shutdown_function('kill_my_cpp_prog');
?>

Jul 17 '05 #9

P: n/a
shyren,

You should most likely move the register_shutdown_function to right
after the function kill_my_cpp_prog as so:

<?php
ignore_user_about(false);
function kill_my_cpp_prog() {
...
}
register_shutdown_function('kill_my_cpp_prog');
....

Because the way you have it now, it will not register the shutdown
function until the script completes.

Mike
Jul 17 '05 #10

P: n/a
Mike,
Thanks for your response...

I moved the register_shutdown_function just after the kill_***
function. Still it doesn't work. Another observation is that if user
doesn't press stop button than the script completes its execution and
it writes "in" on the browser while there is nothing written in file
(even the file is not created). There is no problem with permissions
however.
Shyren

Jul 17 '05 #11

P: n/a
NC
shyren wrote:

According to given suggestions I wrote a program for testing.
To see whether I capture the user's abort I write to the file
in tmp directory with writing permissions set. But still I am
not able to detect it.

$file=fopen("tmp/num","w");


Are you sure you didn't mean:

$file=fopen("/tmp/num","w");

"tmp/num" refers to file "num" located in directory "tmp", which
is a subdirectory of the current working directory. "/tmp/num"
refers to file "num" located in directory "/tmp", which is the
default temporary directory on a Unix system...

Cheers,
NC

Jul 17 '05 #12

P: n/a
Hi,
There is another observation if I write to /tmp I can write in the
function kill_*** but to the directory tmp (I created this directory
with write permissions) I can't. While in other part of the script I
can write to both /tmp and tmp directory.
I am planning to write to a unique file the information that user has
pressed stop. And read this information in c program to stop it.
Will write soon...
thanks a lot for help.
shyren

Jul 17 '05 #13

P: n/a
It seems that I am not able to capture the event when user presses the
stop button. Here is my php script which calls a program test (in c,
code is given below). test program checks the file $pass_id every
second and if "exit" is written in the file it exits.
Whether or not the stop button is pressed file $pass_id is created
which shows that there is no detection of "stop" button being pressed
by the user.

Shyren
__________________________________
<?php
ignore_user_abort(false);
$hostid = hexdec(shell_exec("hostid"));
$td = gettimeofday();
$pass_id=$td["sec"].$td["usec"]. getmypid(). $hostid;
$pass_id="/tmp/my".$pass_id;
function kill_my_cpp_prog()
{
global $pass_id;
$file=fopen($pass_id,"w");
$num="exit";
fwrite($file,$num);
fclose($file);
echo "in";
}
register_shutdown_function('kill_my_cpp_prog');
$com="./test ".$pass_id;
exec($com,$out,$in);

?>
___________________________________
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc,const char** argv)
{
cout<<argv[1];
fstream fl1("/tmp/nun",ios::out);
for(int i=0;i<20;i++)
{
fstream fl(argv[1],ios::in);
char tmp[5];
fl.getline(tmp,5);
cout<<" "<<tmp;
if(string(tmp)=="exit")
i=10;
fl.close();
fl1<<tmp<<"in"<<i<<" ";
sleep(1);
}
fl1.close();
return 0;

}

Jul 17 '05 #14

P: n/a
shyren <sa********@yahoo.com> wrote:
It seems that I am not able to capture the event when user presses the
stop button.


Regartless if you actually manage to catch this, you should always make
sure your system call has proper limits (eg max memory/cpu(time) usage set).

How to do this depends on your OS...

Jul 17 '05 #15

P: n/a
This works finally...
the ignore_user_abort() was set to false before. Manual is also
misguiding. Also ob_flush() is necessary sometimes after flush() as
said in manual too...
"back" is to run the program in backgroun. It is mikehup available at
http://www.mikekohn.net/mikehup.php.
Thanks
_Shyren_____________________________________
<?php
set_time_limit(0);
ignore_user_abort(TRUE);
#register_shutdown_function('kill_my_cpp_prog');
$hostid = hexdec(shell_exec("hostid"));
$td = gettimeofday();
$pass_id=$td["sec"].$td["usec"]. getmypid(). $hostid;
$pass_id="/tmp/my".$pass_id;
#$pass_id="/tmp/num";

function kill_my_cpp_prog()
{
global $pass_id;
$file=fopen($pass_id,"w");
$num="exit";
fwrite($file,$num);
fclose($file);
echo "in";
exit();
}

$com="./back ./test ".$pass_id." &";
#$com="./test ".$pass_id." &";
echo $com;
flush();
ob_flush();
exec($com,$out,$in);
echo $out[0];
sleep(1);
while(1)
{
echo ".";
flush();
ob_flush();
if(connection_status() && connection_aborted())
{
kill_my_cpp_prog();
}
if(file_exists("/tmp/nun"))
{$file=fopen("/tmp/nun","r");
$num=fgets($file,7);
echo $num;
fclose($file);
if($num=="finish")
exit();
}
sleep(1);

$i--;
}

?>
______________________________________

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc,const char** argv)
{
cout<<argv[1];
bool check=0;
ofstream fl1("/tmp/nun",ios::out);
for(int i=0;i<30;i++)
{
fstream fl(argv[1],ios::in);
char tmp[5];
fl.getline(tmp,5);
cout<<" "<<tmp;
if(string(tmp)=="exit")
{
i=30;
check=1;
}
fl.close();

sleep(1);
}
if(!check)
{
char *str="finish";
fl1.write(str,7);
}
fl1.close();
return 0;

}

Jul 17 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.