3 * @copyright Copyright (C) 2010-2023, 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 nonexistent
36 private static function pidFromFile(string $file)
38 if (!file_exists($file)) {
42 return trim(@file_get_contents($file));
46 * Is there a running process with the given pid file
48 * @param string $file Filename of pid file
50 * @return boolean Is it running?
52 public static function isRunningProcess(string $file): bool
54 $pid = self::pidFromFile($file);
60 // Is the process running?
61 $running = posix_kill($pid, 0);
63 // If not, then we will kill the stale file
71 * Kills a process from a given pid file
73 * @param string $file Filename of pid file
75 * @return boolean Was it killed successfully?
77 public static function killProcess(string $file): bool
79 $pid = self::pidFromFile($file);
81 // We don't have a process id? then we quit
86 // We now kill the process
87 $killed = posix_kill($pid, SIGTERM);
89 // If we killed the process successfully, we can remove the pidfile
99 * @param string $file Filename of pid file
101 * @return boolean|string PID or "false" if not created
103 public static function create(string $file)
105 $pid = self::pidFromFile($file);
107 // We have a process id? then we quit
113 file_put_contents($file, $pid);
115 // Now we check if everything is okay
116 return self::pidFromFile($file);
120 * Deletes a given pid file
122 * @param string $file Filename of pid file
124 * @return boolean Is it running?
126 public static function delete(string $file): bool
128 return @unlink($file);