note 73490 deleted from function.shell-exec by cmb
| From: | cmb@php.net | Date: | Sun, 14 Jul 2019 11:37:25 +0000 |
| Subject: | note 73490 deleted from function.shell-exec by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-211595@lists.php.net to get a copy of this message | ||
Note Submitter: RayJ
----
After many failed attempts to find a way to run a php script in the background (the script had to be
activated from a web browser). Since I create and test my scripts on a windows box, I need an
automatic way to detect if the script was running on windows or the Linux server. The activated
script will continue to run even if you close your browser.
<?php
$runCommand = 'php -q FULLPATH/FILE.php';
if(isset($_SERVER['PWD'])//*nix (aka NOT windows)
{
$nullResult =
$runCommand > /dev/null &;
}
else //windowz
{
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run($runCommand, 7, false);
}
?>
Also, to get the correct "basepath" from the script that runs in the background. try:
<?php
ignore_user_abort(true);
set_time_limit(0);
if(isset($_SERVER['PWD']))
{ $basepath = $_SERVER['PWD']; }
else
{ $basepath = ereg_replace('[^/]*$', '', $_SERVER['PHP_SELF']); }
$extendedpath = '/path1/path2/';
$basepath = preg_replace("|(?:{$extendedpath})$|i",'',$basepath);
echo 'Start In: '.$basepath;
?>
Enjoy!
Later, RayJ