Make Sure Only a Single Instance of a Program Is Running

Make sure only a single instance of a program is running

The following code should do the job, it is cross-platform and runs on Python 2.4-3.2. I tested it on Windows, OS X and Linux.

from tendo import singleton
me = singleton.SingleInstance() # will sys.exit(-1) if other instance is running

The latest code version is available singleton.py. Please file bugs here.

You can install tend using one of the following methods:

  • easy_install tendo
  • pip install tendo
  • manually by getting it from http://pypi.python.org/pypi/tendo

How to make sure that only a single instance of a Java application is running?

What you are looking for can probably best be accomplished with a lock file. By lock file I simply mean a file that will have a predefined location and whose existence is your mutex.

Test if that file exists when your program starts, if it does, exit immediately. Create a file in a known location. If your program exits normally, delete the lock file.

Probably best is if you can also populate the file with a pid (process id) so that you can detect abnormal exits that didn't delete the file but this get OS specific.

How to make sure that there is one instance of the application is running

The most common pattern for doing this is to use the Singleton pattern. As you haven't indicated a language, I'm going to assume that you are referring to C# here - if not, the principles are still the same in most OO languages.

This article should give you some help.

How do I make sure only one instance of my program can be executed?

You could use sockets - a ServerSocket can only listen on a port that's not already in use. The first launch successfully creates a ServerSocket instance on the port - while that program is running, no other ServerSocket can successfully be created on that port.

import java.io.IOException;
import java.net.ServerSocket;

public class OneInstance {

private static ServerSocket SERVER_SOCKET;

public static void main(String[] args) {
try {
SERVER_SOCKET = new ServerSocket(1334);
System.out.println("OK to continue running.");
System.out.println("Press any key to exit.");
System.in.read();
} catch (IOException x) {
System.out.println("Another instance already running... exit.");
}
}
}

What's the best way to make sure only one instance of a Perl program is running?

There are many ways to do it. PID files are the traditional way to do it. You could also hold a lock on a file, for example the program itself. This small piece of code will do the trick:

use Fcntl ':flock';
open my $self, '<', $0 or die "Couldn't open self: $!";
flock $self, LOCK_EX | LOCK_NB or die "This script is already running";

One advantage over PID files is that files automatically get unlocked when the program exits. It's much easier to implement in a reliable way.

How to Run Only One Instance of Application

You may used named mutex.

Code sample from the article:

WINAPI WinMain(
HINSTANCE, HINSTANCE, LPSTR, int)
{
try {
// Try to open the mutex.
HANDLE hMutex = OpenMutex(
MUTEX_ALL_ACCESS, 0, "MyApp1.0");

if (!hMutex)
// Mutex doesn’t exist. This is
// the first instance so create
// the mutex.
hMutex =
CreateMutex(0, 0, "MyApp1.0");
else
// The mutex exists so this is the
// the second instance so return.
return 0;

Application->Initialize();
Application->CreateForm(
__classid(TForm1), &Form1);
Application->Run();

// The app is closing so release
// the mutex.
ReleaseMutex(hMutex);
}
catch (Exception &exception) {
Application->
ShowException(&exception);
}
return 0;
}


Related Topics



Leave a reply



Submit