]> git.mxchange.org Git - friendica.git/blob - include/pidfile.php
Merge remote-tracking branch 'upstream/develop' into 1604-cookie
[friendica.git] / include / pidfile.php
1 <?php
2 class pidfile {
3         private $_file;
4         private $_running;
5
6         public function __construct($dir, $name) {
7                 $this->_file = "$dir/$name.pid";
8
9                 if (file_exists($this->_file)) {
10                         $pid = trim(@file_get_contents($this->_file));
11                         if (($pid != "") AND posix_kill($pid, 0)) {
12                                 $this->_running = true;
13                         }
14                 }
15
16                 if (! $this->_running) {
17                         $pid = getmypid();
18                         file_put_contents($this->_file, $pid);
19                 }
20         }
21
22         public function __destruct() {
23                 if ((! $this->_running) && file_exists($this->_file)) {
24                         @unlink($this->_file);
25                 }
26         }
27
28         public function is_already_running() {
29                 return $this->_running;
30         }
31
32         public function running_time() {
33                 return(time() - @filectime($this->_file));
34         }
35
36         public function kill() {
37                 if (file_exists($this->_file))
38                         return(posix_kill(file_get_contents($this->_file), SIGTERM));
39         }
40 }
41 ?>