3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Util;
30 * Read the pid from a given pid file
32 * @param string $file Filename of pid file
34 * @return boolean|string PID or "false" if not existent
36 static private function pidFromFile($file) {
37 if (!file_exists($file)) {
41 return trim(@file_get_contents($file));
45 * Is there a running process with the given pid file
47 * @param string $file Filename of pid file
49 * @return boolean Is it running?
51 static public function isRunningProcess($file) {
52 $pid = self::pidFromFile($file);
58 // Is the process running?
59 $running = posix_kill($pid, 0);
61 // If not, then we will kill the stale file
69 * Kills a process from a given pid file
71 * @param string $file Filename of pid file
73 * @return boolean Was it killed successfully?
75 static public function killProcess($file) {
76 $pid = self::pidFromFile($file);
78 // We don't have a process id? then we quit
83 // We now kill the process
84 $killed = posix_kill($pid, SIGTERM);
86 // If we killed the process successfully, we can remove the pidfile
96 * @param string $file Filename of pid file
98 * @return boolean|string PID or "false" if not created
100 static public function create(string $file)
102 $pid = self::pidFromFile($file);
104 // We have a process id? then we quit
110 file_put_contents($file, $pid);
112 // Now we check if everything is okay
113 return self::pidFromFile($file);
117 * Deletes a given pid file
119 * @param string $file Filename of pid file
121 * @return boolean Is it running?
123 static public function delete(string $file): bool
125 return @unlink($file);