Discussion:
waiting on exec()
(too old to reply)
c***@gmail.com
2008-03-27 17:07:02 UTC
Permalink
Hi,

I have been trying to find a way to delete a file about 30 minutes
after it is created by a php script. I came up with the idea to use
exec() to give the following linux command "sleep 1200 && php
delete.php". Ofcourse you have to download and install the module php5-
cli for this to work. The problem I am getting in that php sits and
waits until the sleep command lets php run the delete.php script. Is
there a way that php can give a system command and not have to wait
for it finish?


Thanks,

Carl


http://www.gaihosa.com
Delphi6
2008-03-28 08:31:32 UTC
Permalink
Post by c***@gmail.com
Hi,
I have been trying to find a way to delete a file about 30 minutes
after it is created by a php script. I came up with the idea to use
exec() to give the following linux command "sleep 1200 && php
delete.php". Ofcourse you have to download and install the module php5-
cli for this to work. The problem I am getting in that php sits and
waits until the sleep command lets php run the delete.php script. Is
there a way that php can give a system command and not have to wait
for it finish?
Thanks,
Carl
http://www.gaihosa.com
There is not direct way to do so :) even if you will specify that you
don't need any result message by adding '>/dev/null 2>&1' code to you
command but there is trick ;) You can create temporary 'command.sh'
file with necessary command(s) and execute it in background:

<?php
if ($fp = fopen('command.sh', 'w')) {
fwrite($fp, 'sleep 30 && mkdir test'."\n");
fwrite($fp, 'rm command.sh');
fclose($fp);
}
exec('/bin/sh ./command.sh >/dev/null 2>&1 &');
?>

this works find for me, the script finishes work immediately. It
creates test directory after 30 seconds and removes temporary
'command.sh' file.

Loading...