]> git.mxchange.org Git - friendica.git/blob - src/Util/Pidfile.php
Move Photo module, update Photo model
[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         /**
13          * @brief Read the pid from a given pid file
14          *
15          * @param string $file Filename of pid file
16          *
17          * @return boolean|string PID or "false" if not existent
18          */
19         static private function pidFromFile($file) {
20                 if (!file_exists($file)) {
21                         return false;
22                 }
23
24                 return trim(@file_get_contents($file));
25         }
26
27         /**
28          * @brief Is there a running process with the given pid file
29          *
30          * @param string $file Filename of pid file
31          *
32          * @return boolean Is it running?
33          */
34         static public function isRunningProcess($file) {
35                 $pid = self::pidFromFile($file);
36
37                 if (!$pid) {
38                         return false;
39                 }
40
41                 // Is the process running?
42                 $running = posix_kill($pid, 0);
43
44                 // If not, then we will kill the stale file
45                 if (!$running) {
46                         self::delete($file);
47                 }
48                 return $running;
49         }
50
51         /**
52          * @brief Kills a process from a given pid file
53          *
54          * @param string $file Filename of pid file
55          *
56          * @return boolean Was it killed successfully?
57          */
58         static public function killProcess($file) {
59                 $pid = self::pidFromFile($file);
60
61                 // We don't have a process id? then we quit
62                 if (!$pid) {
63                         return false;
64                 }
65
66                 // We now kill the process
67                 $killed = posix_kill($pid, SIGTERM);
68
69                 // If we killed the process successfully, we can remove the pidfile
70                 if ($killed) {
71                         self::delete($file);
72                 }
73                 return $killed;
74         }
75
76         /**
77          * @brief Creates a pid file
78          *
79          * @param string $file Filename of pid file
80          *
81          * @return boolean|string PID or "false" if not created
82          */
83         static public function create($file) {
84                 $pid = self::pidFromFile($file);
85
86                 // We have a process id? then we quit
87                 if ($pid) {
88                         return false;
89                 }
90
91                 $pid = getmypid();
92                 file_put_contents($file, $pid);
93
94                 // Now we check if everything is okay
95                 return self::pidFromFile($file);
96         }
97
98         /**
99          * @brief Deletes a given pid file
100          *
101          * @param string $file Filename of pid file
102          *
103          * @return boolean Is it running?
104          */
105         static public function delete($file) {
106                 return @unlink($file);
107         }
108 }