2017-12-04 18:12:22 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @file src/Util/Pidfile.php
|
|
|
|
*/
|
|
|
|
namespace Friendica\Util;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Pidfile class
|
|
|
|
*/
|
|
|
|
class Pidfile
|
|
|
|
{
|
|
|
|
private $file;
|
|
|
|
private $running;
|
2017-12-09 09:46:21 +00:00
|
|
|
private $pid;
|
2017-12-04 18:12:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $dir path
|
|
|
|
* @param string $name filename
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct($dir, $name)
|
|
|
|
{
|
2017-12-09 09:46:21 +00:00
|
|
|
$this->file = "$dir/$name";
|
|
|
|
$this->running = false;
|
2017-12-04 18:12:22 +00:00
|
|
|
|
2017-12-09 09:46:21 +00:00
|
|
|
if (file_exists($this->file)) {
|
|
|
|
$this->pid = trim(@file_get_contents($this->file));
|
|
|
|
if (($this->pid != "") && posix_kill($this->pid, 0)) {
|
2017-12-04 18:12:22 +00:00
|
|
|
$this->running = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-09 09:46:21 +00:00
|
|
|
if (!$this->running) {
|
|
|
|
$this->pid = getmypid();
|
|
|
|
file_put_contents($this->file, $this->pid);
|
2017-12-04 18:12:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __destruct()
|
|
|
|
{
|
2017-12-09 09:46:21 +00:00
|
|
|
if (!$this->running && file_exists($this->file)) {
|
2017-12-04 18:12:22 +00:00
|
|
|
@unlink($this->file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-09 09:46:21 +00:00
|
|
|
* @brief Check if a process with this pid file is already running
|
|
|
|
* @return boolean Is it running?
|
|
|
|
*/
|
|
|
|
public function isRunning()
|
|
|
|
{
|
|
|
|
return $this->running;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Return the pid of the process
|
|
|
|
* @return boolean process id
|
2017-12-04 18:12:22 +00:00
|
|
|
*/
|
2017-12-09 09:46:21 +00:00
|
|
|
public function pid()
|
2017-12-04 18:12:22 +00:00
|
|
|
{
|
2017-12-09 09:46:21 +00:00
|
|
|
return $this->pid;
|
2017-12-04 18:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-09 09:46:21 +00:00
|
|
|
* @brief Returns the seconds that the old process was running
|
|
|
|
* @return integer run time of the old process
|
2017-12-04 18:12:22 +00:00
|
|
|
*/
|
2017-12-09 09:46:21 +00:00
|
|
|
public function runningTime()
|
2017-12-04 18:12:22 +00:00
|
|
|
{
|
2017-12-09 09:46:21 +00:00
|
|
|
return time() - @filectime($this->file);
|
2017-12-04 18:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-09 09:46:21 +00:00
|
|
|
* @brief Kills the old process
|
2017-12-04 18:12:22 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2017-12-09 09:46:21 +00:00
|
|
|
public function kill()
|
2017-12-04 18:12:22 +00:00
|
|
|
{
|
2017-12-09 09:46:21 +00:00
|
|
|
if (!empty($this->pid)) {
|
|
|
|
return posix_kill($this->pid, SIGTERM);
|
2017-12-04 18:12:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|