Index: src/switch.c
===================================================================
--- src/switch.c	(revision 4341)
+++ src/switch.c	(working copy)
@@ -70,12 +70,36 @@
 	return;
 }
 
-//kill a freeswitch process running in background mode
-static int freeswitch_kill_background()
+// flock mutex to prevent multiple freeswitch processes
+#include <sys/file.h>
+static int mutex_fd;
+static void enter_startup_mutex(){
+	char path[256] = "";	//full path of the LOGFILE, which we are going to lock on.
+
+	//set the globals so we can use the global paths.
+	switch_core_set_globals();
+	snprintf(path, sizeof(path), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, lfile);
+
+    mutex_fd = open(path, O_CREAT | O_APPEND);
+    if(mutex_fd < 0)
+    {
+        perror("failed to open " LOGFILE);
+        exit(255);
+
+    };
+    
+    flock(mutex_fd, LOCK_EX);
+}
+
+static void exit_startup_mutex(){
+    flock(mutex_fd, LOCK_UN);
+    close(mutex_fd);
+}
+
+static void freeswitch_get_last_pid( pid_t *pid)
 {
 	FILE *f;				//FILE handle to open the pid file
 	char path[256] = "";	//full path of the PID file
-	pid_t pid = 0;			//pid from the pid file	
 
 	//set the globals so we can use the global paths.
 	switch_core_set_globals();
@@ -87,12 +111,34 @@
 	if ((f = fopen(path, "r")) == 0) {
 		//pid file does not exist
 		fprintf(stderr, "Cannot open pid file %s.\n", path);
-		return 255;
+		*pid = 0;
 	}
+    else
+    {
 
-	//pull the pid from the file
-	fscanf(f, "%d", &pid);
+	    //pull the pid from the file
+	    fscanf(f, "%d", pid);
+    };
 
+	//be nice and close the file handle to the pid file
+	fclose(f);
+
+}
+
+static int freeswitch_check_last_pid(pid_t *pid)
+{
+    freeswitch_get_last_pid(pid);
+    if(pid) return kill(*pid,0); // 0 means still alive
+    return -1;
+}
+
+//kill a freeswitch process running in background mode
+static int freeswitch_kill_background()
+{
+    pid_t pid;
+
+    freeswitch_get_last_pid(&pid);
+
 	//if we have a valid pid
 	if (pid > 0) {
 
@@ -121,9 +167,6 @@
 #endif
 	}
 
-	//be nice and close the file handle to the pid file
-	fclose(f);
-
 	return 0;
 }
 
@@ -353,6 +396,13 @@
 		return freeswitch_kill_background();
 	}
 
+    enter_startup_mutex();
+
+    if (!freeswitch_check_last_pid(&pid)){
+		fprintf(stderr, "freeswitch process %d is still running: try using -stop\n", (int)pid);
+		return 255;
+    }
+
 	if (alt_dirs && alt_dirs !=3) {
 		fprintf(stderr, "You must use -conf, -log, and -db together\n");
 		return 255;
@@ -386,6 +436,8 @@
 
 	fprintf(f, "%d", pid = getpid());
 	fclose(f);
+    
+    exit_startup_mutex();
 
 	switch_core_runtime_loop(nc);
 
