- add pidfile handling (required for automatic startup/shutdown)
authorAndreas Scherbaum <andreas@scherbaum.biz>
Sun, 18 Nov 2012 10:39:16 +0000 (11:39 +0100)
committerAndreas Scherbaum <andreas@scherbaum.biz>
Sun, 18 Nov 2012 10:39:16 +0000 (11:39 +0100)
docbot.conf
docbot.pl

index 0a4386be007e4d4b05040b36571cd233d20a4b87..28f3ad2baa845824525418d7b2e8cf3515bf487e 100644 (file)
@@ -59,3 +59,5 @@ translations:
   file: 'docbot.translations'
 monitoring:
   file: 'docbot.monitoring'
+system:
+  pidfile: 'docbot.pid'
index 23d2acbb92c77a9c88c0a48b3abc94e8b123a266..459206d02f6eca4e17ea4e72ce0a34c31bd543e7 100755 (executable)
--- a/docbot.pl
+++ b/docbot.pl
@@ -51,6 +51,7 @@ use POSIX ":sys_wait_h";
 use Scalar::Util 'refaddr';
 use YAML::XS qw (/./);
 use URI::Escape;
+use File::Pid;
 use feature qw/switch/;
 import docbot::config;
 import docbot::db;
@@ -158,6 +159,28 @@ if (length($main::config_file) > 0) {
 init_translations();
 read_translations($main::translations_file);
 
+
+# create pid file
+my $pidfile_name = config_get_key2('system', 'pidfile');
+my ($pidfile);
+if (defined($pidfile_name)) {
+    my $pidfile = File::Pid->new({ file => $pidfile_name, pid => $$ });
+    my $pid_running = $pidfile->running;
+    if (defined($pid_running)) {
+        print_msg("Another docbot instance is running!", ERROR);
+        print_msg("PID of the other instance: $pid_running", INFO);
+        exit(1);
+    }
+    if (!defined($pidfile->write)) {
+        my $error_msg = $!;
+        print_msg("Not able to write PID file!", ERROR);
+        print_msg("Error: $error_msg", INFO);
+        exit(1);
+    }
+    print_msg("Wrote PID " . $pidfile->pid . " to file " . $pidfile_name . "", DEBUG);
+}
+
+
 init_database();
 init_sessions();