Ensure a single instance of an application in Linux

The Right Thing is advisory locking using flock(LOCK_EX); in Python, this is found in the fcntl module. Unlike pidfiles, these locks are always automatically released when your process dies for any reason, have no race conditions exist relating to file deletion (as the file doesn’t need to be deleted to release the lock), and there’s … Read more

Let gVim always run a single instance

If you are using the bash shell (on Linux/OS X/using Cygwin) is to add you ~/.bashrc file: gvim () { command gvim –remote-silent “$@” || command gvim “$@”; } On Windows I think you could have a gvim.bat batch-script to achieve the same.. gvim.exe -p –remote-tab-silent %1 %* If gvim.exe isn’t in your path Run … Read more

Is using a Mutex to prevent multiple instances of the same program from running safe?

It is more usual and convenient to use Windows events for this purpose. E.g. static EventWaitHandle s_event ; bool created ; s_event = new EventWaitHandle (false, EventResetMode.ManualReset, “my program#startup”, out created) ; if (created) Launch () ; else Exit () ; When your process exits or terminates, Windows will close the event for you, and … Read more

How to create a single instance application in C or C++

A good way is: #include <sys/file.h> #include <errno.h> int pid_file = open(“/var/run/whatever.pid”, O_CREAT | O_RDWR, 0666); int rc = flock(pid_file, LOCK_EX | LOCK_NB); if(rc) { if(EWOULDBLOCK == errno) ; // another instance is running } else { // this is the first instance } Note that locking allows you to ignore stale pid files (i.e. … Read more

How to implement a single instance Java application?

I use the following method in the main method. This is the simplest, most robust, and least intrusive method I have seen so I thought that I’d share it. private static boolean lockInstance(final String lockFile) { try { final File file = new File(lockFile); final RandomAccessFile randomAccessFile = new RandomAccessFile(file, “rw”); final FileLock fileLock = … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)