]> granicus.if.org Git - pgbadger/commitdiff
Automatically remove obsolete pid file when there is no other pgbadger process runnin...
authorDarold Gilles <gilles@darold.net>
Mon, 18 Jan 2016 09:11:51 +0000 (10:11 +0100)
committerDarold Gilles <gilles@darold.net>
Mon, 18 Jan 2016 09:11:51 +0000 (10:11 +0100)
pgbadger

index 77863430b4f1c0c630b081fa46e5f02ea380fbca..201476346fdc245645e937d4f736ca832edc468f 100755 (executable)
--- a/pgbadger
+++ b/pgbadger
@@ -448,19 +448,27 @@ if ($enable_checksum) {
 }
 
 # Check if an other process is already running
-if (-f "$PID_DIR/pgbadger.pid") {
-       print "FATAL: an other process is already started or remove the file, see $PID_DIR/pgbadger.pid\n";
-       exit 1;
-} else {
-       # Create pid file
-       unless(open(OUT, ">$PID_DIR/pgbadger.pid")) {
-               print "FATAL: can't create pid file $PID_DIR/pgbadger.pid, $!\n";
+if (-e "$PID_DIR/pgbadger.pid") {
+       my $is_running = 2;
+       if ($^O !~ /MSWin32|dos/i) {
+               eval { $is_running = `ps auwx | grep pgbadger | grep -v grep | wc -l`; chomp($is_running); };
+       }
+       if (!$@ && ($is_running <= 1)) {
+               unlink("$PID_DIR/pgbadger.pid");
+       } else {
+               print "FATAL: an other process is already started or remove the file, see $PID_DIR/pgbadger.pid\n";
                exit 1;
        }
-       print OUT $$;
-       close(OUT);
 }
 
+# Create pid file
+unless(open(OUT, ">$PID_DIR/pgbadger.pid")) {
+       print "FATAL: can't create pid file $PID_DIR/pgbadger.pid, $!\n";
+       exit 1;
+}
+print OUT $$;
+close(OUT);
+
 # Rewrite some command line arguments as lists
 &compute_arg_list();