3 * @file src/Util/Pidfile.php
5 namespace Friendica\Util;
13 * @brief Read the pid from a given pid file
15 * @param string $file Filename of pid file
17 * @return boolean|string PID or "false" if not existent
19 static private function pidFromFile($file) {
20 if (!file_exists($file)) {
24 return trim(@file_get_contents($file));
28 * @brief Is there a running process with the given pid file
30 * @param string $file Filename of pid file
32 * @return boolean Is it running?
34 static public function isRunningProcess($file) {
35 $pid = self::pidFromFile($file);
41 // Is the process running?
42 $running = posix_kill($pid, 0);
44 // If not, then we will kill the stale file
52 * @brief Kills a process from a given pid file
54 * @param string $file Filename of pid file
56 * @return boolean Was it killed successfully?
58 static public function killProcess($file) {
59 $pid = self::pidFromFile($file);
61 // We don't have a process id? then we quit
66 // We now kill the process
67 $killed = posix_kill($pid, SIGTERM);
69 // If we killed the process successfully, we can remove the pidfile
77 * @brief Creates a pid file
79 * @param string $file Filename of pid file
81 * @return boolean|string PID or "false" if not created
83 static public function create($file) {
84 $pid = self::pidFromFile($file);
86 // We have a process id? then we quit
92 file_put_contents($file, $pid);
94 // Now we check if everything is okay
95 return self::pidFromFile($file);
99 * @brief Deletes a given pid file
101 * @param string $file Filename of pid file
103 * @return boolean Is it running?
105 static public function delete($file) {
106 return @unlink($file);