1. Go to this page and download the library: Download rogerthomas84/daemoniser library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rogerthomas84 / daemoniser example snippets
chdir(dirname(__FILE__));
error_reporting(E_ALL | E_STRICT);
aemoniser\DaemonConfig;
use Daemoniser\DaemonException;
/**
* Class ExampleOneDaemon
*/
class ExampleOneDaemon extends DaemonAbstract
{
public function run()
{
$this->logInfo((new DateTime())->format('Y-m-d H:i:s'));
$this->logError('This logs an error This logs an error This logs an error This logs an error This logs an error This logs an error ');
}
/**
* @return string
*/
public function randomAnimal()
{
$animals = ['Cat', 'Dog', 'Giraffe'];
$this->echoLine($animals[array_rand($animals)]);
}
/**
* @return DaemonCommand[]
* @throws DaemonException
*/
protected function getAdditionalCommands()
{
return [
DaemonCommand::build('animal', 'Get a random animal.', 'randomAnimal')
];
}
/**
* @return bool
*/
protected function canImmediatelyStop()
{
return true; // Can we just kill the process?
}
}
try {
$daemon = new ExampleOneDaemon();
$config = new DaemonConfig(
$daemon,
'/path/to/error.log',
'/path/to/info.log',
'/path/to/mypid.pid',
'/path/to/stops/filename.stop',
10,
DaemonConfig::ONE_HUNDRED_MB,
[
'memory_limit' => '512MB',
'display_startup_errors' => 1,
'display_errors' => 1,
],
[
'www-data'
]
);
$config->setMaxLogFileSize(1000);
/**
* Alternatively you can elect to set them via set methods. If you chose this route, you only need to explicitely
* pass an instance of the daemon.
*/
// $config = new DaemonConfig($daemon);
// $config->setErrorLogFilePath('/path/to/error.log');
// $config->setInfoLogFilePath('/path/to/info.log');
// $config->setPidFilePath('/path/to/pid/filename.pid');
// $config->setSoftStopFilePath('/path/to/stops/filename.stop');
// $config->setSleepBetweenRuns(10); // Sleep for 10 seconds between calls to `run()`
// $config->setMaxLogFileSize(100000000); // Set the max log file size to 2,000,000 bytes before being rotated
// $config->setIniSettings(
// [
// 'memory_limit' => '512MB',
// 'display_startup_errors' => 1,
// 'display_errors' => 1,
// ]
// );
// $config->setWhitelistedUsers(
// [
// 'www-data'
// ]
// );
$daemon->execute(
$config,
$argv
);
} catch (DaemonException $e) {
echo ' Exception executing command:' . PHP_EOL;
echo sprintf(' Message: %s', $e->getMessage()) . PHP_EOL;
echo sprintf(' File: %s', $e->getFile()) . PHP_EOL;
echo sprintf(' Line: %s', $e->getLine()) . PHP_EOL;
exit(1);
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.