]> git.mxchange.org Git - friendica.git/blob - src/Util/Pidfile.php
Merge remote-tracking branch 'upstream/develop' into develop
[friendica.git] / src / Util / Pidfile.php
1 <?php
2 /**
3  * @file src/Util/Pidfile.php
4  */
5 namespace Friendica\Util;
6
7 /**
8  * @brief Pidfile class
9  */
10 class Pidfile
11 {
12         private $file;
13         private $running;
14
15         /**
16          * @param string $dir  path
17          * @param string $name filename
18          * @return void
19          */
20         public function __construct($dir, $name)
21         {
22                 $this->_file = "$dir/$name.pid";
23
24                 if (file_exists($this->_file)) {
25                         $pid = trim(@file_get_contents($this->file));
26                         if (($pid != "") && posix_kill($pid, 0)) {
27                                 $this->running = true;
28                         }
29                 }
30
31                 if (! $this->running) {
32                         $pid = getmypid();
33                         file_put_contents($this->file, $pid);
34                 }
35         }
36
37         /**
38          * @return void
39          */
40         public function __destruct()
41         {
42                 if ((! $this->running) && file_exists($this->file)) {
43                         @unlink($this->file);
44                 }
45         }
46
47         /**
48          * @return boolean
49          */
50         public static function isRunning()
51         {
52                 return self::$running;
53         }
54
55         /**
56          * @return object
57          */
58         public static function runningTime()
59         {
60                 return time() - @filectime(self::$file);
61         }
62
63         /**
64          * @return boolean
65          */
66         public static function kill()
67         {
68                 if (file_exists(self::$file)) {
69                         return posix_kill(file_get_contents(self::$file), SIGTERM);
70                 }
71         }
72 }