Determine if only one instance is running: Difference between revisions

From Rosetta Code
Content added Content deleted
(Check SocketOSException type)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(18 intermediate revisions by 9 users not shown)
Line 4: Line 4:


=={{header|Ada}}==
=={{header|Ada}}==

The following solution tries to open a file for reading. If the file does ''not'' exist, a 'Name_Error' is raised. The exception handler creates that file, allows the program to perform its task, and, eventually, makes sure the file is deleted. If no exception is raised, the file exists, so another instance is running, and the program stops. It also stops if the wrong exception is raised, i.e., any exception other than 'Name_Error'.
The following solution tries to open a file for reading. If the file does ''not'' exist, a 'Name_Error' is raised. The exception handler creates that file, allows the program to perform its task, and, eventually, makes sure the file is deleted. If no exception is raised, the file exists, so another instance is running, and the program stops. It also stops if the wrong exception is raised, i.e., any exception other than 'Name_Error'.


<lang Ada>with Ada.Text_IO;
<syntaxhighlight lang="ada">with Ada.Text_IO;


procedure Single_Instance is
procedure Single_Instance is
Line 34: Line 33:
exception
exception
when others => IO.Delete(Lock_File);
when others => IO.Delete(Lock_File);
end Single_Instance;</lang>
end Single_Instance;</syntaxhighlight>


Note that there is a race condition: If another instance tries to open the file for reading, before the first one has created it, then more than one instance will actually run.
Note that there is a race condition: If another instance tries to open the file for reading, before the first one has created it, then more than one instance will actually run.
Line 40: Line 39:
=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
AutoHotkey has a #SingleInstance command. If you run two scripts that don't have it at the same time, it alerts the user. #SingleInstance FORCE closes the older instance when a newer one is run, and #SingleInstance IGNORE does nothing when you try to open a new instance of an already-running script.
AutoHotkey has a #SingleInstance command. If you run two scripts that don't have it at the same time, it alerts the user. #SingleInstance FORCE closes the older instance when a newer one is run, and #SingleInstance IGNORE does nothing when you try to open a new instance of an already-running script.

=={{header|BASIC}}==
==={{header|BaCon}}===
Using advisory locks from libc.
<syntaxhighlight lang="bacon">PRAGMA INCLUDE <sys/file.h>
OPTION DEVICE O_NONBLOCK

OPEN ME$ FOR DEVICE AS me

IF flock(me, LOCK_EX | LOCK_NB) <> 0 THEN
PRINT "I am already running, exiting..."
END
ENDIF

PRINT "Running this program, doing things..."
SLEEP 5000

CLOSE DEVICE me</syntaxhighlight>

=={{header|Bash Shell}}==

Using flock, exits 0 if you got the lock, otherwise exits 1, below is a simplified example:

<syntaxhighlight lang="bbcbasic">
local fd=${2:-200}

# create lock file
eval "exec $fd>/tmp/my_lock.lock"

# acquire the lock, or fail
flock -nx $fd \
&& # do something if you got the lock \
|| # do something if you did not get the lock
</syntaxhighlight>

There's a nice program called singleton that wraps this in an easy to use package : https://github.com/krezreb/singleton


=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
{{works with|BBC BASIC for Windows}}
Change 'UniqueLockName' to something more likely to be unique, such as a GUID.
Change 'UniqueLockName' to something more likely to be unique, such as a GUID.
<lang bbcbasic> SYS "CreateMutex", 0, 1, "UniqueLockName" TO Mutex%
<syntaxhighlight lang="bbcbasic"> SYS "CreateMutex", 0, 1, "UniqueLockName" TO Mutex%
SYS "GetLastError" TO lerr%
SYS "GetLastError" TO lerr%
IF lerr% = 183 THEN
IF lerr% = 183 THEN
Line 54: Line 89:
SYS "ReleaseMutex", Mutex%
SYS "ReleaseMutex", Mutex%
SYS "CloseHandle", Mutex%
SYS "CloseHandle", Mutex%
END</lang>
END</syntaxhighlight>


=={{header|C}}==
=={{header|C}}==
Line 65: Line 100:


{{libheader|POSIX}}
{{libheader|POSIX}}
<lang c>#include <fcntl.h> /* fcntl, open */
<syntaxhighlight lang="c">#include <fcntl.h> /* fcntl, open */
#include <stdlib.h> /* atexit, getenv, malloc */
#include <stdlib.h> /* atexit, getenv, malloc */
#include <stdio.h> /* fputs, printf, puts, snprintf */
#include <stdio.h> /* fputs, printf, puts, snprintf */
Line 158: Line 193:
puts("Fin!");
puts("Fin!");
return 0;
return 0;
}</lang>
}</syntaxhighlight>


=== POSIX with file creation ===
=== POSIX with file creation ===
Line 168: Line 203:


{{libheader|POSIX}}
{{libheader|POSIX}}
<lang c>#include <fcntl.h>
<syntaxhighlight lang="c">#include <fcntl.h>
#include <signal.h>
#include <signal.h>
#include <stdio.h>
#include <stdio.h>
Line 203: Line 238:
unlink("/tmp/MyUniqueName"); close(myfd);
unlink("/tmp/MyUniqueName"); close(myfd);
return 0;
return 0;
}</lang>
}</syntaxhighlight>

=={{header|C++}}==
===Microsoft Windows===
{{works with|Windows|2000 or later}}
This line needs to be near the top of the file (or in stdafx.h, if you use one.)
<lang cpp>#include <afx.h></lang>

You need a variable of type HANDLE with the same lifetime as your program. Perhaps as a member of your CWinApp object.
<lang cpp>HANDLE mutex;</lang>

At the earliest possible point in your program, you need to initialize it and perform your check. "MyApp" should be a string unique to your application. See [http://msdn2.microsoft.com/en-us/library/ms682411.aspx here] for full details.

<lang cpp>mutex = CreateMutex( NULL, TRUE, "MyApp" );
if ( GetLastError() == ERROR_ALREADY_EXISTS )
{
// There's another instance running. What do you do?
}</lang>

Finally, near the end of your program, you need to close the mutex.
<lang cpp>CloseHandle( mutex );</lang>


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
===Using a TCP Port===
===Using a TCP Port===


<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Net;
using System.Net;
using System.Net.Sockets;
using System.Net.Sockets;
Line 245: Line 260:
}
}
}
}
}</lang>
}</syntaxhighlight>


===Using a mutex===
===Using a mutex===
<lang csharp>
<syntaxhighlight lang="csharp">
// Use this class in your process to guard against multiple instances
// Use this class in your process to guard against multiple instances
//
//
Line 326: Line 341:
}
}
}
}
}</lang>
}</syntaxhighlight>

=={{header|C++}}==
===Microsoft Windows===
{{works with|Windows|2000 or later}}
This line needs to be near the top of the file (or in stdafx.h, if you use one.)
<syntaxhighlight lang="cpp">#include <afx.h></syntaxhighlight>

You need a variable of type HANDLE with the same lifetime as your program. Perhaps as a member of your CWinApp object.
<syntaxhighlight lang="cpp">HANDLE mutex;</syntaxhighlight>

At the earliest possible point in your program, you need to initialize it and perform your check. "MyApp" should be a string unique to your application. See [http://msdn2.microsoft.com/en-us/library/ms682411.aspx here] for full details.

<syntaxhighlight lang="cpp">mutex = CreateMutex( NULL, TRUE, "MyApp" );
if ( GetLastError() == ERROR_ALREADY_EXISTS )
{
// There's another instance running. What do you do?
}</syntaxhighlight>

Finally, near the end of your program, you need to close the mutex.
<syntaxhighlight lang="cpp">CloseHandle( mutex );</syntaxhighlight>


=={{header|Clojure}}==
=={{header|Clojure}}==
{{trans|Java}}
{{trans|Java}}
<lang clojure>(import (java.net ServerSocket InetAddress))
<syntaxhighlight lang="clojure">(import (java.net ServerSocket InetAddress))


(def *port* 12345) ; random large port number
(def *port* 12345) ; random large port number
(try (new ServerSocket *port* 10 (. InetAddress getLocalHost))
(try (new ServerSocket *port* 10 (. InetAddress getLocalHost))
(catch IOException e (System/exit 0))) ; port taken, so app is already running </lang>
(catch IOException e (System/exit 0))) ; port taken, so app is already running </syntaxhighlight>

=={{header|D}}==
===Unix Domain Socket===
Unix domain sockets support another addressing mode, via the so-called abstract namespace. This allows us to bind sockets to names rather than to files. What we get are the following benefits (see page 1175 in The Linux Programming Interface):
* There is no need to worry about possible collisions with existing files in the filesystem.
* There is no socket file to be removed upon program termination.
* We do not need to create a file for the socket at all. This obviates target directory existence, permissions checks, and reduces filesystem clutter. Also, it works in chrooted environments.

All we need is to generate a unique name for our program and pass it as the address when calling bind(). The trick is that instead of specifying a file path as the address, we pass a null byte followed by the name of our choosing (e.g. "\0my-unique-name"). The initial null byte is what distinguishes abstract socket names from conventional Unix domain socket path names, which consist of a string of one or more non-null bytes terminated by a null byte.
Read more here: [https://blog.petrzemek.net/2017/07/24/ensuring-that-a-linux-program-is-running-at-most-once-by-using-abstract-sockets/]
<syntaxhighlight lang="d">
bool is_unique_instance()
{
import std.socket;
auto socket = new Socket(AddressFamily.UNIX, SocketType.STREAM);
auto addr = new UnixAddress("\0/tmp/myapp.uniqueness.sock");
try
{
socket.bind(addr);
return true;
}
catch (SocketOSException e)
{
import core.stdc.errno : EADDRINUSE;

if (e.errorCode == EADDRINUSE)
return false;
else
throw e;
}
}
</syntaxhighlight>

=={{header|Delphi}}==
=={{header|Delphi}}==
<lang Delphi>program OneInstance;
<syntaxhighlight lang="delphi">program OneInstance;


{$APPTYPE CONSOLE}
{$APPTYPE CONSOLE}
Line 362: Line 430:
end;
end;
end;
end;
end.</lang>
end.</syntaxhighlight>
=={{header|D}}==
===Unix Domain Socket===
Unix domain sockets support another addressing mode, via the so-called abstract namespace. This allows us to bind sockets to names rather than to files. What we get are the following benefits (see page 1175 in The Linux Programming Interface):
* There is no need to worry about possible collisions with existing files in the filesystem.
* There is no socket file to be removed upon program termination.
* We do not need to create a file for the socket at all. This obviates target directory existence, permissions checks, and reduces filesystem clutter. Also, it works in chrooted environments.

All we need is to generate a unique name for our program and pass it as the address when calling bind(). The trick is that instead of specifying a file path as the address, we pass a null byte followed by the name of our choosing (e.g. "\0my-unique-name"). The initial null byte is what distinguishes abstract socket names from conventional Unix domain socket path names, which consist of a string of one or more non-null bytes terminated by a null byte.
Read more here: [https://blog.petrzemek.net/2017/07/24/ensuring-that-a-linux-program-is-running-at-most-once-by-using-abstract-sockets/]
<lang d>import std.socket;

bool is_unique_instance() {
auto socket = new Socket(AddressFamily.UNIX, SocketType.STREAM);
auto addr = new UnixAddress("\0/tmp/myapp.uniqueness.sock");
try {
socket.bind(addr);
return true;
} catch (SocketOSException e) {
import core.stdc.errno : EADDRINUSE;

if (e.errorCode == EADDRINUSE) {
log("Duplicate instance detected.");
return false;
}
else {
throw e;
}
}
}
</lang>


=={{header|Erlang}}==
=={{header|Erlang}}==
Line 405: Line 443:
called as register(aname,<0.42.0>)
called as register(aname,<0.42.0>)
</pre>
</pre>


=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">Shell("tasklist > temp.txt")

Dim linea As String
Open "temp.txt" For Input As #1
Do While Not Eof(1)
Line Input #1, linea
If Instr(linea, "fbc.exe") = 0 Then Print "Task is Running" : Exit Do
Loop
Close #1
Shell("del temp.txt")
Sleep</syntaxhighlight>



=={{header|Go}}==
=={{header|Go}}==
Line 410: Line 463:
Recommended over file based solutions. It has the advantage that the port is always released
Recommended over file based solutions. It has the advantage that the port is always released
when the process ends.
when the process ends.
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 428: Line 481:
fmt.Println("single instance started")
fmt.Println("single instance started")
time.Sleep(10 * time.Second)
time.Sleep(10 * time.Second)
}</lang>
}</syntaxhighlight>
===File===
===File===
Solution using O_CREATE|O_EXCL. This solution has the problem that if anything terminates the
Solution using O_CREATE|O_EXCL. This solution has the problem that if anything terminates the
program early, the lock file remains.
program early, the lock file remains.
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 455: Line 508:
time.Sleep(10 * time.Second)
time.Sleep(10 * time.Second)
os.Remove(lfn)
os.Remove(lfn)
}</lang>
}</syntaxhighlight>
Here's a fluffier version that stores the PID in the lock file to provide better messages.
Here's a fluffier version that stores the PID in the lock file to provide better messages.
It has the same problem of the lock file remaining if anything terminates the program early.
It has the same problem of the lock file remaining if anything terminates the program early.
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 506: Line 559:
fmt.Println(os.Getpid(), "running...")
fmt.Println(os.Getpid(), "running...")
time.Sleep(1e10)
time.Sleep(1e10)
}</lang>
}</syntaxhighlight>


=={{header|Haskell}}==
=={{header|Haskell}}==
Simple implementation using a lock file. Two threads are launched, but the second cannot start because the first has created a lock file which is deleted when it has finished.
Simple implementation using a lock file. Two threads are launched, but the second cannot start because the first has created a lock file which is deleted when it has finished.
<lang Haskell>import Control.Concurrent
<syntaxhighlight lang="haskell">import Control.Concurrent
import System.Directory (doesFileExist, getAppUserDataDirectory,
import System.Directory (doesFileExist, getAppUserDataDirectory,
removeFile)
removeFile)
Line 551: Line 604:
-- thus will exit immediately
-- thus will exit immediately
forkIO oneInstance
forkIO oneInstance
return ()</lang>
return ()</syntaxhighlight>


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==
Line 557: Line 610:
The following only works in Unicon. The program uses a socket as a flag.
The following only works in Unicon. The program uses a socket as a flag.


<lang unicon>procedure main(A)
<syntaxhighlight lang="unicon">procedure main(A)
if not open(":"||54321,"na") then stop("Already running")
if not open(":"||54321,"na") then stop("Already running")
repeat {} # busy loop
repeat {} # busy loop
end</lang>
end</syntaxhighlight>


Sample run:
Sample run:
Line 573: Line 626:


=={{header|Java}}==
=={{header|Java}}==
<lang java>import java.io.IOException;
<syntaxhighlight lang="java">import java.io.IOException;
import java.net.InetAddress;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.ServerSocket;
Line 607: Line 660:
}
}
}
}
}</lang>
}</syntaxhighlight>


=={{header|Jsish}}==
=={{header|Jsish}}==
Using a socket on a fixed port number.
Using a socket on a fixed port number.
<lang javascript>/* Determine if only one instance, in Jsish */
<syntaxhighlight lang="javascript">/* Determine if only one instance, in Jsish */
var sock;
var sock;


Line 622: Line 675:
puts('Applicaion already running');
puts('Applicaion already running');
exit(1);
exit(1);
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 640: Line 693:


=={{header|Julia}}==
=={{header|Julia}}==
{{trans|Java}}<lang julia>
{{trans|Java}}<syntaxhighlight lang="julia">
using Sockets
using Sockets


Line 658: Line 711:


canopen()
canopen()
</syntaxhighlight>
</lang>



=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang scala>// version 1.0.6
<syntaxhighlight lang="scala">// version 1.0.6


import java.io.IOException
import java.io.IOException
Line 697: Line 749:
SingleInstance.close()
SingleInstance.close()
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 712: Line 764:


=={{header|Lasso}}==
=={{header|Lasso}}==
<lang Lasso>#!/usr/bin/lasso9
<syntaxhighlight lang="lasso">#!/usr/bin/lasso9


local(lockfile = file('/tmp/myprocess.lockfile'))
local(lockfile = file('/tmp/myprocess.lockfile'))
Line 733: Line 785:
sleep(10000)
sleep(10000)


stdoutnl('Execution done')</lang>
stdoutnl('Execution done')</syntaxhighlight>


Output Window 1:
Output Window 1:
Line 747: Line 799:


=={{header|Liberty BASIC}}==
=={{header|Liberty BASIC}}==
<lang lb>'Create a Mutex to prevent more than one instance from being open at a single time.
<syntaxhighlight lang="lb">'Create a Mutex to prevent more than one instance from being open at a single time.
CallDLL #kernel32, "CreateMutexA", 0 as Long, 1 as Long, "Global\My Program" as ptr, mutex as ulong
CallDLL #kernel32, "CreateMutexA", 0 as Long, 1 as Long, "Global\My Program" as ptr, mutex as ulong
CallDLL #kernel32, "GetLastError", LastError as Long
CallDLL #kernel32, "GetLastError", LastError as Long
Line 762: Line 814:
calldll #kernel32, "ReleaseMutex", mutex as ulong, ret as ulong
calldll #kernel32, "ReleaseMutex", mutex as ulong, ret as ulong
calldll #kernel32, "CloseHandle", mutex as ulong, ret as ulong
calldll #kernel32, "CloseHandle", mutex as ulong, ret as ulong
end</lang>
end</syntaxhighlight>


=={{header|M2000 Interpreter}}==
=={{header|M2000 Interpreter}}==
We can lock a file in user folder. Only one instance can lock a file.
We can lock a file in user folder. Only one instance can lock a file.


<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Checkit {
Module Checkit {
Try {
Try {
Line 776: Line 828:
}
}
}
}
</syntaxhighlight>
</lang>



=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
$Epilog is the action performed upon session exit.
$Epilog is the action performed upon session exit.
Running the following code before any other code will prevent 2 instances from concurrent execution.
Running the following code before any other code will prevent 2 instances from concurrent execution.
<lang Mathematica>$Epilog := Print["Another instance is running "];
<syntaxhighlight lang="mathematica">$Epilog := Print["Another instance is running "];
If[Attributes[Global`Mutex] == {Protected},
If[Attributes[Global`Mutex] == {Protected},
Exit[],
Exit[],
Global`Mutex[x_] := Locked; Protect[Global`Mutex];
Global`Mutex[x_] := Locked; Protect[Global`Mutex];
]</lang>
]</syntaxhighlight>


=={{header|Nim}}==
=={{header|Nim}}==
===fcntl based===
<lang nim>import os, posix

<syntaxhighlight lang="nim">import os, posix


let fn = getHomeDir() & "rosetta-code-lock"
let fn = getHomeDir() & "rosetta-code-lock"
Line 798: Line 851:
var fd = getFileHandle fn.open fmReadWrite
var fd = getFileHandle fn.open fmReadWrite
if fcntl(fd, F_SETLK, addr fl) < 0:
if fcntl(fd, F_SETLK, addr fl) < 0:
stderr.writeln "Another instance of this program is running"
stderr.writeLine "Another instance of this program is running"
quit 1
quit 1
addQuitProc ooiUnlink
addQuitProc ooiUnlink
Line 807: Line 860:
echo i
echo i
sleep 1000
sleep 1000
echo "Fin!"</lang>
echo "Fin!"</syntaxhighlight>

===Unix Domain Socket based===

<syntaxhighlight lang="nim">import options, os
from net import newSocket, bindUnix
from nativesockets import AF_UNIX, SOCK_DGRAM, IPPROTO_IP
from posix import EADDRINUSE

const sockAddr = "\0com.myapp.sock" # Linux will delete this when the application ends
# notice the prefixed null byte, it's the Linux abstract namespace

proc server()=
echo "Unique instance detected"

proc client()=
echo "Duplicate instance detected"

when isMainModule:
var
sock = newSocket(AF_UNIX, SOCK_DGRAM, IPPROTO_IP)
isUnique: Option[bool]

try:
sock.bindUnix(sock_addr)
is_unique = some true
except OSError:
if cint(osLastError()) == EADDRINUSE:
isUnique = some false
else:
raise getCurrentException()
if unlikely is_unique.isNone:
echo "Error detecting uniqueness" # unreachable
else:
if isUnique.unsafeGet():
server()
else:
client()</syntaxhighlight>


=={{header|OCaml}}==
=={{header|OCaml}}==
Replicates the '''C''' example, with the library [http://ocaml-sem.sourceforge.net/ ocaml-sem].
Replicates the '''C''' example, with the library [http://ocaml-sem.sourceforge.net/ ocaml-sem].
<lang ocaml>open Sem
<syntaxhighlight lang="ocaml">open Sem


let () =
let () =
Line 821: Line 912:
(* end of the app *)
(* end of the app *)
sem_unlink "MyUniqueName";
sem_unlink "MyUniqueName";
sem_close sem</lang>
sem_close sem</syntaxhighlight>


The standard library of OCaml also provides a [http://caml.inria.fr/pub/docs/manual-ocaml/libref/Mutex.html Mutex] module.
The standard library of OCaml also provides a [http://caml.inria.fr/pub/docs/manual-ocaml/libref/Mutex.html Mutex] module.


=={{header|Oz}}==
=={{header|Oz}}==
<lang oz>functor
<syntaxhighlight lang="oz">functor
import Application Open System
import Application Open System
define
define
Line 847: Line 938:
{{New Open.file init(name:stdin)} read(list:_ size:1)}
{{New Open.file init(name:stdin)} read(list:_ size:1)}
{Application.exit 0}
{Application.exit 0}
end</lang>
end</syntaxhighlight>


=={{header|Perl}}==
=={{header|Perl}}==
Line 853: Line 944:


Then it tries to get a lock to its own file, from where the script was called.
Then it tries to get a lock to its own file, from where the script was called.
<lang perl>use Fcntl ':flock';
<syntaxhighlight lang="perl">use Fcntl ':flock';


INIT
INIT
Line 861: Line 952:
}
}


sleep 60; # then your code goes here</lang>
sleep 60; # then your code goes here</syntaxhighlight>
=={{header|Perl 6}}==
{{works with|rakudo|2018.03}}
An old-school Unix solution, none the worse for the wear:
<lang perl6>my $name = $*PROGRAM-NAME;
my $pid = $*PID;

my $lockdir = "/tmp";
my $lockfile = "$lockdir/$name.pid";
my $lockpid = "$lockfile$pid";
my $havelock = False;

END {
unlink $lockfile if $havelock;
try unlink $lockpid;
}

my $pidfile = open "$lockpid", :w orelse .die;
$pidfile.say($pid);
$pidfile.close;

if try link($lockpid, $lockfile) {
$havelock = True;
}
else {
shell "kill -CONT `cat $lockfile` || rm $lockfile";
if try link($lockfile, $lockpid) {
$havelock = True;
}
else {
die "You can't run right now!";
}
}
note "Got lock!";
unlink $lockpid;</lang>


=={{header|Phix}}==
=={{header|Phix}}==
{{libheader|pGUI}}
{{libheader|Phix/pGUI}}
<!--<syntaxhighlight lang="phix">(notonline)-->
<lang Phix>--
-- demo\rosetta\Single_instance.exw
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Single_instance.exw</span>
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span>
--
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
include pGUI.e

<span style="color: #008080;">function</span> <span style="color: #000000;">copydata_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">pCommandLine</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">size</span><span style="color: #0000FF;">)</span>
function copydata_cb(Ihandle /*ih*/, atom pCommandLine, integer size)
-- (the first instance is sent a copy of the second one's command line)
<span style="color: #000080;font-style:italic;">-- (the first instance is sent a copy of the second one's command line)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"COPYDATA(%s, %d)\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">peek_string</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pCommandLine</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">size</span><span style="color: #0000FF;">});</span>
printf(1,"COPYDATA(%s, %d)\n",{peek_string(pCommandLine), size});
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span><span style="color: #0000FF;">;</span>
return IUP_DEFAULT;
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
end function

<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
function esc_close(Ihandle /*ih*/, atom c)
<span style="color: #7060A8;">IupSetGlobal</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"SINGLEINSTANCE"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Single"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (must [partially] match the main window title)</span>
return iff(c=K_ESC?IUP_CLOSE:IUP_CONTINUE)
<span style="color: #008080;">if</span> <span style="color: #7060A8;">IupGetGlobal</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"SINGLEINSTANCE"</span><span style="color: #0000FF;">)!=</span><span style="color: #008000;">""</span> <span style="color: #008080;">then</span>
end function
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">IupVbox</span><span style="color: #0000FF;">({</span><span style="color: #7060A8;">IupLabel</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"hello"</span><span style="color: #0000FF;">)},</span><span style="color: #008000;">"MARGIN=200x200"</span><span style="color: #0000FF;">))</span>

<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"TITLE"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Single Instance"</span><span style="color: #0000FF;">)</span>
IupOpen()
<span style="color: #7060A8;">IupSetCallback</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"COPYDATA_CB"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"copydata_cb"</span><span style="color: #0000FF;">));</span>
IupSetGlobal("SINGLEINSTANCE", "Single") -- (must [partially] match the main window title)
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
if IupGetGlobal("SINGLEINSTANCE")!="" then
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
Ihandle dlg = IupDialog(IupVbox({IupLabel("hello")},"MARGIN=200x200"))
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
IupSetAttribute(dlg,"TITLE","Single Instance")
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
IupSetCallback(dlg, "K_ANY", Icallback("esc_close"))
<!--</syntaxhighlight>-->
IupSetCallback(dlg, "COPYDATA_CB", Icallback("copydata_cb"));
IupShow(dlg)
IupMainLoop()
end if
IupClose()</lang>


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
Line 939: Line 992:
<pre>$ ./myScript & # Start in the background
<pre>$ ./myScript & # Start in the background
[1] 26438</pre>
[1] 26438</pre>
<lang PicoLisp>$ pil +
<syntaxhighlight lang="picolisp">$ pil +
: (call "killall" "-0" "-q" "myScript")
: (call "killall" "-0" "-q" "myScript")
-> T</lang>
-> T</syntaxhighlight>


===Using a mutex===
===Using a mutex===
Another possibility is to 'acquire' a mutex on program start, and never release
Another possibility is to 'acquire' a mutex on program start, and never release
it.
it.
<lang PicoLisp>: (acquire "running1")
<syntaxhighlight lang="picolisp">: (acquire "running1")
-> 30817 # A successful call returns the PID</lang>
-> 30817 # A successful call returns the PID</syntaxhighlight>
A second application trying to acquire the same mutex would receive 'NIL'
A second application trying to acquire the same mutex would receive 'NIL'


=={{header|PowerShell}}==
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
if (Get-Process -Name "notepad" -ErrorAction SilentlyContinue)
if (Get-Process -Name "notepad" -ErrorAction SilentlyContinue)
{
{
Line 960: Line 1,013:
Start-Process -FilePath C:\Windows\notepad.exe
Start-Process -FilePath C:\Windows\notepad.exe
}
}
</syntaxhighlight>
</lang>
No output because notepad.exe was not running, so it was started.
No output because notepad.exe was not running, so it was started.
{{Out}}
{{Out}}
Line 966: Line 1,019:
</pre>
</pre>
Run it again.
Run it again.
<syntaxhighlight lang="powershell">
<lang PowerShell>
if (Get-Process -Name "notepad" -ErrorAction SilentlyContinue)
if (Get-Process -Name "notepad" -ErrorAction SilentlyContinue)
{
{
Line 975: Line 1,028:
Start-Process -FilePath C:\Windows\notepad.exe
Start-Process -FilePath C:\Windows\notepad.exe
}
}
</syntaxhighlight>
</lang>
Since it is running a warning message is output.
Since it is running a warning message is output.
{{Out}}
{{Out}}
Line 983: Line 1,036:


=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang PureBasic>#MyApp="MyLittleApp"
<syntaxhighlight lang="purebasic">#MyApp="MyLittleApp"
Mutex=CreateMutex_(0,1,#MyApp)
Mutex=CreateMutex_(0,1,#MyApp)
If GetLastError_()=#ERROR_ALREADY_EXISTS
If GetLastError_()=#ERROR_ALREADY_EXISTS
Line 993: Line 1,046:


ReleaseMutex_(Mutex)
ReleaseMutex_(Mutex)
End</lang>
End</syntaxhighlight>


=={{header|Python}}==
=={{header|Python}}==
Line 1,001: Line 1,054:
Must be run from an application, not the interpreter.
Must be run from an application, not the interpreter.


<lang python>import __main__, os
<syntaxhighlight lang="python">import __main__, os


def isOnlyInstance():
def isOnlyInstance():
Line 1,008: Line 1,061:
return os.system("(( $(ps -ef | grep python | grep '[" +
return os.system("(( $(ps -ef | grep python | grep '[" +
__main__.__file__[0] + "]" + __main__.__file__[1:] +
__main__.__file__[0] + "]" + __main__.__file__[1:] +
"' | wc -l) > 1 ))") != 0</lang>
"' | wc -l) > 1 ))") != 0</syntaxhighlight>


This is not a solution - one can run the same app by copying the code to another location. A solution may be a lock file or lock directory created by the first instance and hold while the first instance is running.
This is not a solution - one can run the same app by copying the code to another location. A solution may be a lock file or lock directory created by the first instance and hold while the first instance is running.
Line 1,014: Line 1,067:
=={{header|Racket}}==
=={{header|Racket}}==
{{trans|Java}}
{{trans|Java}}
<lang racket>
<syntaxhighlight lang="racket">
#lang racket
#lang racket
(define *port* 12345) ; random large port number
(define *port* 12345) ; random large port number
Line 1,022: Line 1,075:
(printf "Working...\n")
(printf "Working...\n")
(sleep 10)
(sleep 10)
</syntaxhighlight>
</lang>

=={{header|Raku}}==
(formerly Perl 6)
{{works with|rakudo|2018.03}}
An old-school Unix solution, none the worse for the wear:
<syntaxhighlight lang="raku" line>my $name = $*PROGRAM-NAME;
my $pid = $*PID;

my $lockdir = "/tmp";
my $lockfile = "$lockdir/$name.pid";
my $lockpid = "$lockfile$pid";
my $havelock = False;

END {
unlink $lockfile if $havelock;
try unlink $lockpid;
}

my $pidfile = open "$lockpid", :w orelse .die;
$pidfile.say($pid);
$pidfile.close;

if try link($lockpid, $lockfile) {
$havelock = True;
}
else {
shell "kill -CONT `cat $lockfile` || rm $lockfile";
if try link($lockfile, $lockpid) {
$havelock = True;
}
else {
die "You can't run right now!";
}
}
note "Got lock!";
unlink $lockpid;</syntaxhighlight>


=={{header|REXX}}==
=={{header|REXX}}==
{{works with|ARexx}}
{{works with|ARexx}}
Solutions using a temporary file as a semaphore aren't very clean; if the program ends abruptly, the file isn't cleaned up. In this solution, we will instead open an ARexx port of our own. Ports are automatically closed by the interpreter if the program is ABENDed.
Solutions using a temporary file as a semaphore aren't very clean; if the program ends abruptly, the file isn't cleaned up. In this solution, we will instead open an ARexx port of our own. Ports are automatically closed by the interpreter if the program is ABENDed.
<lang rexx>/* Simple ARexx program to open a port after checking if it's already open */
<syntaxhighlight lang="rexx">/* Simple ARexx program to open a port after checking if it's already open */
IF Show('PORTS','ROSETTA') THEN DO /* Port is already open; exit */
IF Show('PORTS','ROSETTA') THEN DO /* Port is already open; exit */
SAY 'This program may only be run in a single instance at a time.'
SAY 'This program may only be run in a single instance at a time.'
Line 1,044: Line 1,133:
END
END


EXIT 0</lang>
EXIT 0</syntaxhighlight>


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
# Project : Determine if only one instance is running
# Project : Determine if only one instance is running


Line 1,070: Line 1,159:
end
end
return sum
return sum
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 1,078: Line 1,167:
=={{header|Ruby}}==
=={{header|Ruby}}==
Uses file locking on the program file
Uses file locking on the program file
<lang ruby>def main
<syntaxhighlight lang="ruby">def main
puts "first instance"
puts "first instance"
sleep 20
sleep 20
Line 1,092: Line 1,181:
end
end


__END__</lang>
__END__</syntaxhighlight>


=={{header|Run BASIC}}==
=={{header|Run BASIC}}==
<lang runbasic>if instr(shell$("tasklist"),"rbp.exe") <> 0 then print "Task is Running"</lang>
<syntaxhighlight lang="runbasic">if instr(shell$("tasklist"),"rbp.exe") <> 0 then print "Task is Running"</syntaxhighlight>


=={{header|Rust}}==
=={{header|Rust}}==
Using TCP socket
Using TCP socket
<lang rust>use std::net::TcpListener;
<syntaxhighlight lang="rust">use std::net::TcpListener;


fn create_app_lock(port: u16) -> TcpListener {
fn create_app_lock(port: u16) -> TcpListener {
Line 1,122: Line 1,211:
// ...
// ...
remove_app_lock(lock_socket);
remove_app_lock(lock_socket);
}</lang>
}</syntaxhighlight>


=={{header|Scala}}==
=={{header|Scala}}==
===Java Interoperability===
===Java Interoperability===
{{Out}}Best seen running in your browser [https://scastie.scala-lang.org/ja0sMzt5SGKHSu3w8qnlQQ Scastie (remote JVM)].
{{Out}}Best seen running in your browser [https://scastie.scala-lang.org/ja0sMzt5SGKHSu3w8qnlQQ Scastie (remote JVM)].
<lang Scala>import java.io.IOException
<syntaxhighlight lang="scala">import java.io.IOException
import java.net.{InetAddress, ServerSocket}
import java.net.{InetAddress, ServerSocket}


Line 1,149: Line 1,238:
sys.exit(0)
sys.exit(0)


}</lang>
}</syntaxhighlight>

=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby># For this to work, you need to explicitly
<syntaxhighlight lang="ruby"># For this to work, you need to explicitly
# store the returned fh inside a variable.
# store the returned fh inside a variable.
var fh = File(__FILE__).open_r
var fh = File(__FILE__).open_r
Line 1,162: Line 1,252:
say "Running..."
say "Running..."
Sys.sleep(20)
Sys.sleep(20)
say 'Done!'</lang>
say 'Done!'</syntaxhighlight>


=={{header|Swift}}==
=={{header|Swift}}==
Uses NSDistributedNotificationCenter. Works with Swift 1.2.
Uses NSDistributedNotificationCenter. Works with Swift 1.2.
<lang Swift>import Foundation
<syntaxhighlight lang="swift">import Foundation


let globalCenter = NSDistributedNotificationCenter.defaultCenter()
let globalCenter = NSDistributedNotificationCenter.defaultCenter()
Line 1,191: Line 1,281:


send()
send()
CFRunLoopRun()</lang>
CFRunLoopRun()</syntaxhighlight>


=={{header|Tcl}}==
=={{header|Tcl}}==
{{trans|Java}}
{{trans|Java}}
{{works with|Tcl|8.6}}
{{works with|Tcl|8.6}}
<lang Tcl>package require Tcl 8.6
<syntaxhighlight lang="tcl">package require Tcl 8.6
try {
try {
# Pick a port number based on the name of the main script executing
# Pick a port number based on the name of the main script executing
Line 1,205: Line 1,295:
puts stderr "Application $::argv0 already running?"
puts stderr "Application $::argv0 already running?"
exit 1
exit 1
}</lang>
}</syntaxhighlight>


=={{header|TXR}}==
=={{header|TXR}}==
Line 1,211: Line 1,301:
==== Microsoft Windows ====
==== Microsoft Windows ====


<lang txrlisp>;;; Define some typedefs for clear correspondence with Win32
<syntaxhighlight lang="txrlisp">;;; Define some typedefs for clear correspondence with Win32
(typedef HANDLE cptr)
(typedef HANDLE cptr)
(typedef LPSECURITY_ATTRIBUTES cptr)
(typedef LPSECURITY_ATTRIBUTES cptr)
Line 1,235: Line 1,325:
)
)
(CloseHandle m)</lang>
(CloseHandle m)</syntaxhighlight>


=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==
Line 1,241: Line 1,331:
{{works with|Bourne Shell}}
{{works with|Bourne Shell}}
{{works with|Bourne Again SHell}}
{{works with|Bourne Again SHell}}
<syntaxhighlight lang="sh">
<lang sh>
# (c) Copyright 2005 Mark Hobley
# (c) Copyright 2005 Mark Hobley
#
#
Line 1,278: Line 1,368:
fi
fi
}
}
</syntaxhighlight>
</lang>


=={{header|Visual Basic}}==
=={{header|Visual Basic}}==
Line 1,284: Line 1,374:
{{works with|Visual Basic|5}}
{{works with|Visual Basic|5}}
{{works with|Visual Basic|6}}
{{works with|Visual Basic|6}}
<lang vb>Dim onlyInstance as Boolean
<syntaxhighlight lang="vb">Dim onlyInstance as Boolean
onlyInstance = not App.PrevInstance</lang>
onlyInstance = not App.PrevInstance</syntaxhighlight>

=={{header|Wren}}==
This works by trying to create a special file with exclusive access.

If this fails then the error is captured, a suitable message is printed and the script exits.

Otherwise, the script completes normally and the special file is deleted just before it exits.
<syntaxhighlight lang="wren">import "io" for File, FileFlags

var specialFile = "wren-exclusive._sp"
var checkOneInstanceRunning = Fn.new {
// attempt to create the special file with exclusive access
var ff = FileFlags.create | FileFlags.exclusive
File.openWithFlags(specialFile, ff) { |file| } // closes automatically if successful
}

// check if the current instance is the only one running
var fiber = Fiber.new {
checkOneInstanceRunning.call()
}
var error = fiber.try()
if (error) {
System.print("An instance is already running.")
return
}

// do something that takes a while for testing purposes
var sum = 0
for (i in 1...1e8) {
sum = sum + i
}
System.print(sum)

File.delete(specialFile) // clean up</syntaxhighlight>

{{out}}
<pre>
(in window 1)
$ wren one_instance_running.wren
4.99999995e+15
$

(in window2, started before the first instance completes)
$ wren one_instance_running.wren
An instance is already running.
$
</pre>


{{omit from|ACL2}}
{{omit from|ACL2}}

Latest revision as of 16:53, 24 November 2023

Task
Determine if only one instance is running
You are encouraged to solve this task according to the task description, using any language you may know.

This task is to determine if there is only one instance of an application running. If the program discovers that an instance of it is already running, then it should display a message indicating that it is already running and exit.

Ada

The following solution tries to open a file for reading. If the file does not exist, a 'Name_Error' is raised. The exception handler creates that file, allows the program to perform its task, and, eventually, makes sure the file is deleted. If no exception is raised, the file exists, so another instance is running, and the program stops. It also stops if the wrong exception is raised, i.e., any exception other than 'Name_Error'.

with Ada.Text_IO;

procedure Single_Instance is

   package IO renames Ada.Text_IO;
   Lock_File: IO.File_Type;
   Lock_File_Name: String := "single_instance.magic_lock";

begin
   begin
      IO.Open(File => Lock_File, Mode=> IO.In_File, Name => Lock_File_Name);
      IO.Close(Lock_File);
      IO.Put_Line("I can't -- another instance of me is running ...");
   exception
      when IO.Name_Error =>
         IO.Put_Line("I can run!");
         IO.Create(File => Lock_File, Name => Lock_File_Name);
         for I in 1 .. 10 loop
            IO.Put(Integer'Image(I));
            delay 1.0; -- wait one second
         end loop;
         IO.Delete(Lock_File);
         IO.New_Line;
         IO.Put_Line("I am done!");
   end;
exception
   when others => IO.Delete(Lock_File);
end Single_Instance;

Note that there is a race condition: If another instance tries to open the file for reading, before the first one has created it, then more than one instance will actually run.

AutoHotkey

AutoHotkey has a #SingleInstance command. If you run two scripts that don't have it at the same time, it alerts the user. #SingleInstance FORCE closes the older instance when a newer one is run, and #SingleInstance IGNORE does nothing when you try to open a new instance of an already-running script.

BASIC

BaCon

Using advisory locks from libc.

PRAGMA INCLUDE <sys/file.h>
OPTION DEVICE O_NONBLOCK

OPEN ME$ FOR DEVICE AS me

IF flock(me, LOCK_EX | LOCK_NB) <> 0 THEN
    PRINT "I am already running, exiting..."
    END
ENDIF

PRINT "Running this program, doing things..."
SLEEP 5000

CLOSE DEVICE me

Bash Shell

Using flock, exits 0 if you got the lock, otherwise exits 1, below is a simplified example:

local fd=${2:-200}

# create lock file
eval "exec $fd>/tmp/my_lock.lock"

# acquire the lock, or fail 
flock -nx $fd \
&& # do something if you got the lock \
|| # do something if you did not get the lock

There's a nice program called singleton that wraps this in an easy to use package : https://github.com/krezreb/singleton

BBC BASIC

Change 'UniqueLockName' to something more likely to be unique, such as a GUID.

      SYS "CreateMutex", 0, 1, "UniqueLockName" TO Mutex%
      SYS "GetLastError" TO lerr%
      IF lerr% = 183 THEN
        SYS "CloseHandle", Mutex%
        SYS "MessageBox", @hwnd%, "I am already running", 0, 0
        QUIT
      ENDIF
      
      SYS "ReleaseMutex", Mutex%
      SYS "CloseHandle", Mutex%
      END

C

POSIX with file lock

This solution opens ~/rosetta-code-lock and uses fcntl() to set a write lock on the file. Only one instance can set this lock. If fcntl() fails, this program assumes that another instance is running. This program always clears its lock when it terminates.

The user might use an interrupt or other signal to terminate the program. If so, the lock file survives, but the system still clears the lock. The user can run the program again.

Note that the underlying file system needs to support file locking; for example if ~/ directory is on a NFS mounted partition, success in a locking fcntl() call is not always meaningful.

Library: POSIX
#include <fcntl.h>	/* fcntl, open */
#include <stdlib.h>	/* atexit, getenv, malloc */
#include <stdio.h>	/* fputs, printf, puts, snprintf */
#include <string.h>	/* memcpy */
#include <unistd.h>	/* sleep, unlink */

/* Filename for only_one_instance() lock. */
#define INSTANCE_LOCK "rosetta-code-lock"

void
fail(const char *message)
{
	perror(message);
	exit(1);
}

/* Path to only_one_instance() lock. */
static char *ooi_path;

void
ooi_unlink(void)
{
	unlink(ooi_path);
}

/* Exit if another instance of this program is running. */
void
only_one_instance(void)
{
	struct flock fl;
	size_t dirlen;
	int fd;
	char *dir;

	/*
	 * Place the lock in the home directory of this user;
	 * therefore we only check for other instances by the same
	 * user (and the user can trick us by changing HOME).
	 */
	dir = getenv("HOME");
	if (dir == NULL || dir[0] != '/') {
		fputs("Bad home directory.\n", stderr);
		exit(1);
	}
	dirlen = strlen(dir);

	ooi_path = malloc(dirlen + sizeof("/" INSTANCE_LOCK));
	if (ooi_path == NULL)
		fail("malloc");
	memcpy(ooi_path, dir, dirlen);
	memcpy(ooi_path + dirlen, "/" INSTANCE_LOCK,
	    sizeof("/" INSTANCE_LOCK));  /* copies '\0' */

	fd = open(ooi_path, O_RDWR | O_CREAT, 0600);
	if (fd < 0)
		fail(ooi_path);

	fl.l_start = 0;
	fl.l_len = 0;
	fl.l_type = F_WRLCK;
	fl.l_whence = SEEK_SET;
	if (fcntl(fd, F_SETLK, &fl) < 0) {
		fputs("Another instance of this program is running.\n",
		    stderr);
		exit(1);
	}

	/*
	 * Run unlink(ooi_path) when the program exits. The program
	 * always releases locks when it exits.
	 */
	atexit(ooi_unlink);
}

/*
 * Demo for Rosetta Code.
 * http://rosettacode.org/wiki/Determine_if_only_one_instance_is_running
 */
int
main()
{
	int i;

	only_one_instance();

	/* Play for 10 seconds. */
	for(i = 10; i > 0; i--) {
		printf("%d...%s", i, i % 5 == 1 ? "\n" : " ");
		fflush(stdout);
		sleep(1);
	}
	puts("Fin!");
	return 0;
}

POSIX with file creation

This solution opens a file with O_CREAT|O_EXCL. If the file already exists, this program assumes that another instance is running. This solution is not as good as file locking, because the program might terminate without deleting the file.

The program, when terminating, must be sure to unlink() the file. This example has unlink() at two places: at the end of main(), and at a SIGINT handler. If you interrupt the program, it will probably delete /tmp/MyUniqueName, but not if SIGINT wins a race before the program installs its handler. If you terminate the program with a different signal, then you will get stuck, because /tmp/MyUniqueName will still exist, preventing another execution of the program. One might add code to catch some other signals, but there is no way to catch SIGKILL!

This program uses a regular file, with open() and unlink(). There is an older version that uses a semaphore, with sem_open() and sem_unlink(). The switch from a semaphore to a regular file was easy, because the program never used the semaphore as a semaphore; it only checked the existence of a semaphore. If you get stuck, rm /tmp/MyUniqueName might be easier than deleting a semaphore.

Library: POSIX
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/* unistd for sleep */

void sigint_handler(int sig)
{
   fprintf(stderr, "Caught signal %d.\n", sig);
   unlink("/tmp/MyUniqueName");
   /* exit() is not safe in a signal handler, use _exit() */
   _exit(1);
}

int main()
{
   struct sigaction act;
   int myfd;
   
   myfd = open("/tmp/MyUniqueName", O_CREAT|O_EXCL);
   if ( myfd < 0 )
   {
      fprintf(stderr, "I am already running!\n");
      exit(1);
   }
   act.sa_handler = sigint_handler;
   sigemptyset(&act.sa_mask);
   act.sa_flags = 0;
   sigaction(SIGINT, &act, NULL);
   /* here the real code of the app*/
   sleep(20);
   /* end of the app */
   unlink("/tmp/MyUniqueName"); close(myfd);
   return 0;
}

C#

Using a TCP Port

using System;
using System.Net;
using System.Net.Sockets;

class Program {        
    static void Main(string[] args) {        
        try {
            TcpListener server = new TcpListener(IPAddress.Any, 12345);
            server.Start();
        } 
       
        catch (SocketException e) {
            if (e.SocketErrorCode == SocketError.AddressAlreadyInUse) {
                Console.Error.WriteLine("Already running.");
            }
        }
    }
}

Using a mutex

// Use this class in your process to guard against multiple instances
//
// This is valid for C# running on Windows, but not for C# with Linux.
//
using System;
using System.Threading;

/// <summary>
/// RunOnce should be instantiated in the calling processes main clause
/// (preferably using a "using" clause) and then calling process
/// should then check AlreadyRunning and do whatever is appropriate
/// </summary>
public class RunOnce : IDisposable
{
	public RunOnce( string name )
	{
		m_name = name;
		AlreadyRunning = false;

		bool created_new = false;

		m_mutex = new Mutex( false, m_name, out created_new );

		AlreadyRunning = !created_new;
	}

	~RunOnce()
	{
		DisposeImpl( false );
	}

	public bool AlreadyRunning
	{
		get { return m_already_running; }
		private set { m_already_running = value; }
	}

	private void DisposeImpl( bool is_disposing )
	{
		GC.SuppressFinalize( this );

		if( is_disposing )
		{
			m_mutex.Close();
		}
	}

	#region IDisposable Members

	public void Dispose()
	{
		DisposeImpl( true );
	}

	#endregion

	private string m_name;
	private bool m_already_running;
	private Mutex m_mutex;
}

class Program
{
    // Example code to use this
    static void Main( string[] args )
    {
        using ( RunOnce ro = new RunOnce( "App Name" ) )
        {
            if ( ro.AlreadyRunning )
            {
                Console.WriteLine( "Already running" );
                return;
            }

            // Program logic
        }
    }
}

C++

Microsoft Windows

Works with: Windows version 2000 or later

This line needs to be near the top of the file (or in stdafx.h, if you use one.)

#include <afx.h>

You need a variable of type HANDLE with the same lifetime as your program. Perhaps as a member of your CWinApp object.

HANDLE mutex;

At the earliest possible point in your program, you need to initialize it and perform your check. "MyApp" should be a string unique to your application. See here for full details.

mutex = CreateMutex( NULL, TRUE, "MyApp" );
if ( GetLastError() == ERROR_ALREADY_EXISTS )
{
     // There's another instance running.  What do you do?
}

Finally, near the end of your program, you need to close the mutex.

CloseHandle( mutex );

Clojure

Translation of: Java
(import (java.net ServerSocket InetAddress))

(def *port* 12345) ; random large port number
(try (new ServerSocket *port* 10 (. InetAddress getLocalHost))
     (catch IOException e (System/exit 0))) ; port taken, so app is already running

D

Unix Domain Socket

Unix domain sockets support another addressing mode, via the so-called abstract namespace. This allows us to bind sockets to names rather than to files. What we get are the following benefits (see page 1175 in The Linux Programming Interface):

  • There is no need to worry about possible collisions with existing files in the filesystem.
  • There is no socket file to be removed upon program termination.
  • We do not need to create a file for the socket at all. This obviates target directory existence, permissions checks, and reduces filesystem clutter. Also, it works in chrooted environments.

All we need is to generate a unique name for our program and pass it as the address when calling bind(). The trick is that instead of specifying a file path as the address, we pass a null byte followed by the name of our choosing (e.g. "\0my-unique-name"). The initial null byte is what distinguishes abstract socket names from conventional Unix domain socket path names, which consist of a string of one or more non-null bytes terminated by a null byte. Read more here: [1]

bool is_unique_instance()
{
    import std.socket;
    auto socket = new Socket(AddressFamily.UNIX, SocketType.STREAM);
    auto addr = new UnixAddress("\0/tmp/myapp.uniqueness.sock");
    try
    {
        socket.bind(addr);
        return true;
    }
    catch (SocketOSException e)
    {
        import core.stdc.errno : EADDRINUSE;

        if (e.errorCode == EADDRINUSE)
            return false;
        else
            throw e;
    }
}

Delphi

program OneInstance;

{$APPTYPE CONSOLE}

uses SysUtils, Windows;

var
  FMutex: THandle;
begin
  FMutex := CreateMutex(nil, True, 'OneInstanceMutex');
  if FMutex = 0 then
    RaiseLastOSError
  else
  begin
    try
      if GetLastError = ERROR_ALREADY_EXISTS then
        Writeln('Program already running.  Closing...')
      else
      begin
        // do stuff ...
        Readln;
      end;
    finally
      CloseHandle(FMutex);
    end;
  end;
end.

Erlang

From the Erlang shell, or in a program, register the application process. If this works, the process is the only one.

Output:
7> erlang:register( aname, erlang:self() ).          
true
8> erlang:register( aname, erlang:self() ).
** exception error: bad argument
     in function  register/2
        called as register(aname,<0.42.0>)


FreeBASIC

Shell("tasklist > temp.txt")

Dim linea As String
Open "temp.txt" For Input As #1
Do While Not Eof(1)
    Line Input #1, linea
    If Instr(linea, "fbc.exe") = 0 Then Print "Task is Running" : Exit Do
Loop
Close #1
Shell("del temp.txt")
Sleep


Go

Port

Recommended over file based solutions. It has the advantage that the port is always released when the process ends.

package main

import (
    "fmt"
    "net"
    "time"
)

const lNet = "tcp"
const lAddr = ":12345"

func main() {
    if _, err := net.Listen(lNet, lAddr); err != nil {
        fmt.Println("an instance was already running")
        return
    }
    fmt.Println("single instance started")
    time.Sleep(10 * time.Second)
}

File

Solution using O_CREATE|O_EXCL. This solution has the problem that if anything terminates the program early, the lock file remains.

package main

import (
    "fmt"
    "os"
    "time"
)

// The path to the lock file should be an absolute path starting from the root.
// (If you wish to prevent the same program running in different directories,
// that is.)
const lfn = "/tmp/rclock"

func main() {
    lf, err := os.OpenFile(lfn, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
    if err != nil {
        fmt.Println("an instance is already running")
        return
    }
    lf.Close()
    fmt.Println("single instance started")
    time.Sleep(10 * time.Second)
    os.Remove(lfn)
}

Here's a fluffier version that stores the PID in the lock file to provide better messages. It has the same problem of the lock file remaining if anything terminates the program early.

package main

import (
    "fmt"
    "os"
    "strconv"
    "strings"
    "time"
)

// The path to the lock file should be an absolute path starting from the root.
// (If you wish to prevent the same program running in different directories, that is.)
const lfn = "/tmp/rclock"

func main() {
    lf, err := os.OpenFile(lfn, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
    if err == nil {
        // good
        // 10 digit pid seems to be a standard for lock files
        fmt.Fprintf(lf, "%10d", os.Getpid())
        lf.Close()
        defer os.Remove(lfn)
    } else {
        // problem
        fmt.Println(err)
        // dig deeper
        lf, err = os.Open(lfn)
        if err != nil {
            return
        }
        defer lf.Close()
        fmt.Println("inspecting lock file...")
        b10 := make([]byte, 10)
        _, err = lf.Read(b10)
        if err != nil {
            fmt.Println(err)
            return
        }
        pid, err := strconv.Atoi(strings.TrimSpace(string(b10)))
        if err != nil {
            fmt.Println(err)
            return
        }
        fmt.Println("lock file created by pid", pid)
        return
    }
    fmt.Println(os.Getpid(), "running...")
    time.Sleep(1e10)
}

Haskell

Simple implementation using a lock file. Two threads are launched, but the second cannot start because the first has created a lock file which is deleted when it has finished.

import Control.Concurrent
import System.Directory (doesFileExist, getAppUserDataDirectory,
    removeFile)
import System.IO (withFile, Handle, IOMode(WriteMode), hPutStr)

oneInstance :: IO ()
oneInstance = do
    -- check if file "$HOME/.myapp.lock" exists
    user <- getAppUserDataDirectory "myapp.lock"
    locked <- doesFileExist user
    if locked
    then print "There is already one instance of this program running."
    else do
        t <- myThreadId
        -- this is the entry point to the main program:
        -- withFile creates a file, then calls a function,
        -- then closes the file
        withFile user WriteMode (do_program t)
        -- remove the lock when we're done
        removeFile user

do_program :: ThreadId -> Handle -> IO ()
do_program t h = do
    let s = "Locked by thread: " ++ show t
    -- print what thread has acquired the lock
    putStrLn s
    -- write the same message to the file, to show that the
    -- thread "owns" the file
    hPutStr h s
    -- wait for one second
    threadDelay 1000000

main :: IO ()
main = do
    -- launch the first thread, which will create the lock file
    forkIO oneInstance
    -- wait for half a second
    threadDelay 500000
    -- launch the second thread, which will find the lock file and
    -- thus will exit immediately
    forkIO oneInstance
    return ()

Icon and Unicon

The following only works in Unicon. The program uses a socket as a flag.

procedure main(A)
   if not open(":"||54321,"na") then stop("Already running")
   repeat {}	# busy loop
end

Sample run:

->self &
[1] 15358
->self
Already running
->

Java

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.UnknownHostException;
 
public class SingletonApp
{
    private static final int PORT = 65000;  // random large port number
    private static ServerSocket s;

    // static initializer
    static {
        try {
            s = new ServerSocket(PORT, 10, InetAddress.getLocalHost());
        } catch (UnknownHostException e) {
            // shouldn't happen for localhost
        } catch (IOException e) {
            // port taken, so app is already running
            System.out.print("Application is already running,");
            System.out.println(" so terminating this instance.");
            System.exit(0);
        }
    }

    public static void main(String[] args) {
        System.out.print("OK, only this instance is running");
        System.out.println(" but will terminate in 10 seconds.");
        try {
            Thread.sleep(10000);
            if (s != null && !s.isClosed()) s.close();
        } catch (Exception e) {
            System.err.println(e);
        }
    }
}

Jsish

Using a socket on a fixed port number.

/* Determine if only one instance, in Jsish */
var sock;

try {
    sock = new Socket({client:false, port:54321});
    puts('\nApplication running for 30 seconds, from', strftime());
    update(30000);
    puts('\nApplication ended at', strftime());
} catch (err) {
    puts('Applicaion already running');
    exit(1);
}
Output:
prompt$ jsish oneInstance.jsi &
[1] 2003
prompt$
Application running for 30 seconds, from 2019-06-14 11:19:37

prompt$ jsish oneInstance.jsi
Applicaion already running
prompt$ jsish oneInstance.jsi
Applicaion already running
prompt$
Application ended at 2019-06-14 11:20:06

[1]+  Done                    jsish oneInstance.jsi

Julia

Translation of: Java
using Sockets

const portnum = 12345

function canopen()
    try
        server = listen(portnum)
        println("This is the only instance.")
        sleep(20)
    catch y
        if findfirst("EADDRINUSE", string(y)) != nothing
            println("There is already an instance running.")
        end
    end
end

canopen()

Kotlin

// version 1.0.6

import java.io.IOException
import java.net.*

object SingleInstance {
    private var ss: ServerSocket? = null  

    fun alreadyRunning(): Boolean {
        try {
            ss = ServerSocket(65000, 10, InetAddress.getLocalHost()) // using private port 65000        
        }
        catch (e: IOException) {
            // port already in use so an instance is already running
            return true   
        }
        return false
    }

    fun close() {
        if (ss == null || ss?.isClosed() == true) return
        ss?.close()
    }
}

fun main(args: Array<String>) {
    if (SingleInstance.alreadyRunning()) {
        println("Application is already running, so terminating this instance")
        System.exit(0)
    }
    else { 
        println("OK, only this instance is running but will terminate in 10 seconds")
        Thread.sleep(10000)
        SingleInstance.close()  
    }
}
Output:

First output window:

OK, only this instance is running but will terminate in 10 seconds
Output:

Second output window (second instance started within 10 seconds of first):

Application is already running, so terminating this instance

Lasso

#!/usr/bin/lasso9

local(lockfile = file('/tmp/myprocess.lockfile'))

if(#lockfile -> exists) => {
	stdoutnl('Error: App is running as of ' + #lockfile -> readstring)
	abort
}

handle => {
	#lockfile -> delete
}

stdoutnl('Starting execution')

#lockfile -> doWithClose => {
	#lockfile -> writebytes(bytes(date))
}

sleep(10000)

stdoutnl('Execution done')

Output Window 1:

$./rosetta 
Starting execution
Execution done

Output Window 2:

$./rosetta 
Error: App is running as of 2013-11-27 08:42:45

Liberty BASIC

'Create a Mutex to prevent more than one instance from being open at a single time.
CallDLL #kernel32, "CreateMutexA", 0 as Long, 1 as Long, "Global\My Program" as ptr, mutex as ulong
CallDLL #kernel32, "GetLastError", LastError as Long

if LastError = 183 then 'Error returned when a Mutex already exists
    'Close the handle if the mutex already exists
    calldll #kernel32, "CloseHandle", mutex as ulong, ret as ulong
    notice "An instance of My Program is currently running!"
    end
end if

'Release the Mutex/ Close the handle prior to ending the program
'Comment out these lines to allow the program to remain active to test for the mutex's presence
calldll #kernel32, "ReleaseMutex", mutex as ulong, ret as ulong
calldll #kernel32, "CloseHandle", mutex as ulong, ret as ulong
end

M2000 Interpreter

We can lock a file in user folder. Only one instance can lock a file.

Module Checkit {
      Try {
            Open "MYLOCK" For Output Exclusive As #F
            Print "DO SOMETHING"
            A$=Key$
            Close#f
      }
}

Mathematica / Wolfram Language

$Epilog is the action performed upon session exit. Running the following code before any other code will prevent 2 instances from concurrent execution.

$Epilog := Print["Another instance is running "]; 
If[Attributes[Global`Mutex] == {Protected},
 Exit[],
 Global`Mutex[x_] := Locked; Protect[Global`Mutex];
 ]

Nim

fcntl based

import os, posix

let fn = getHomeDir() & "rosetta-code-lock"
proc ooiUnlink {.noconv.} = discard unlink fn

proc onlyOneInstance =
  var fl = TFlock(lType: F_WRLCK.cshort, lWhence: SEEK_SET.cshort)
  var fd = getFileHandle fn.open fmReadWrite
  if fcntl(fd, F_SETLK, addr fl) < 0:
    stderr.writeLine "Another instance of this program is running"
    quit 1
  addQuitProc ooiUnlink

onlyOneInstance()

for i in countdown(10, 1):
  echo i
  sleep 1000
echo "Fin!"

Unix Domain Socket based

import options, os
from net import newSocket, bindUnix
from nativesockets import AF_UNIX, SOCK_DGRAM, IPPROTO_IP
from posix import EADDRINUSE

const sockAddr = "\0com.myapp.sock" # Linux will delete this when the application ends
# notice the prefixed null byte, it's the Linux abstract namespace

proc server()=
  echo "Unique instance detected"

proc client()=
  echo "Duplicate instance detected"

when isMainModule:
  var 
    sock = newSocket(AF_UNIX, SOCK_DGRAM, IPPROTO_IP)
    isUnique: Option[bool]

  try:
    sock.bindUnix(sock_addr)
    is_unique = some true
  except OSError:
      if cint(osLastError()) == EADDRINUSE:
        isUnique = some false
      else:
        raise getCurrentException()
  
  if unlikely is_unique.isNone:
    echo "Error detecting uniqueness" # unreachable 
  else:
    if isUnique.unsafeGet():
      server()
    else:
      client()

OCaml

Replicates the C example, with the library ocaml-sem.

open Sem

let () =
  let oflags = [Unix.O_CREAT;
                Unix.O_EXCL] in
  let sem = sem_open "MyUniqueName" ~oflags () in
  (* here the real code of the app *)
  Unix.sleep 20;
  (* end of the app *)
  sem_unlink "MyUniqueName";
  sem_close sem

The standard library of OCaml also provides a Mutex module.

Oz

functor
import Application Open System
define
   fun {IsAlreadyRunning}
      try
	 S = {New Open.socket init}
      in
	 {S bind(takePort:12345)}
	 false
      catch system(os(os "bind" ...) ...) then
	 true
      end
   end

   if {IsAlreadyRunning} then
      {System.showInfo "Exiting because already running."}
      {Application.exit 1}
   end
   {System.showInfo "Press enter to exit."}
   {{New Open.file init(name:stdin)} read(list:_ size:1)}
   {Application.exit 0}
end

Perl

The INIT block is runned just before the Perl runtime begins execution. See perlmod

Then it tries to get a lock to its own file, from where the script was called.

use Fcntl ':flock';

INIT
{
	die "Not able to open $0\n" unless (open ME, $0);
	die "I'm already running !!\n" unless(flock ME, LOCK_EX|LOCK_NB);
}

sleep 60; # then your code goes here

Phix

Library: Phix/pGUI
-- demo\rosetta\Single_instance.exw
without js
include pGUI.e

function copydata_cb(Ihandle /*ih*/, atom pCommandLine, integer size)
    -- (the first instance is sent a copy of the second one's command line)
    printf(1,"COPYDATA(%s, %d)\n",{peek_string(pCommandLine), size});
    return IUP_DEFAULT;
end function

IupOpen()
IupSetGlobal("SINGLEINSTANCE", "Single") -- (must [partially] match the main window title)
if IupGetGlobal("SINGLEINSTANCE")!="" then
    Ihandle dlg = IupDialog(IupVbox({IupLabel("hello")},"MARGIN=200x200"))
    IupSetAttribute(dlg,"TITLE","Single Instance")
    IupSetCallback(dlg, "COPYDATA_CB", Icallback("copydata_cb"));
    IupShow(dlg)
    IupMainLoop()
end if
IupClose()

PicoLisp

Calling 'killall'

One possibility is to send a zero-signal with 'killall', and check the return value. This is useful if each application is started by a hash-bang script (the first line is e.g. "#!/usr/bin/picolisp /usr/lib/picolisp/lib.l"). In that way, each application has its own name which can be passed to 'killall'.

$ cat myScript
#!/usr/bin/picolisp /usr/lib/picolisp/lib.l

(wait 120000)
(bye)
$ ./myScript &  # Start in the background
[1] 26438
$ pil +
: (call "killall" "-0" "-q" "myScript")
-> T

Using a mutex

Another possibility is to 'acquire' a mutex on program start, and never release it.

: (acquire "running1")
-> 30817  # A successful call returns the PID

A second application trying to acquire the same mutex would receive 'NIL'

PowerShell

if (Get-Process -Name "notepad" -ErrorAction SilentlyContinue)
{
    Write-Warning -Message "notepad is already running."
}
else
{
    Start-Process -FilePath C:\Windows\notepad.exe
}

No output because notepad.exe was not running, so it was started.

Output:

Run it again.

if (Get-Process -Name "notepad" -ErrorAction SilentlyContinue)
{
    Write-Warning -Message "notepad is already running."
}
else
{
    Start-Process -FilePath C:\Windows\notepad.exe
}

Since it is running a warning message is output.

Output:
WARNING: notepad is already running

PureBasic

#MyApp="MyLittleApp"
Mutex=CreateMutex_(0,1,#MyApp)
If GetLastError_()=#ERROR_ALREADY_EXISTS
  MessageRequester(#MyApp,"One instance is already started.")
  End
EndIf

; Main code executes here

ReleaseMutex_(Mutex)
End

Python

Linux (including cygwin) and Mac OSX Leopard

Works with: Python version 2.6

Must be run from an application, not the interpreter.

import __main__, os

def isOnlyInstance():
    # Determine if there are more than the current instance of the application
    # running at the current time.
    return os.system("(( $(ps -ef | grep python | grep '[" +
                     __main__.__file__[0] + "]" + __main__.__file__[1:] +
                     "' | wc -l) > 1 ))") != 0

This is not a solution - one can run the same app by copying the code to another location. A solution may be a lock file or lock directory created by the first instance and hold while the first instance is running.

Racket

Translation of: Java
#lang racket
(define *port* 12345) ; random large port number
(define listener-handler
  (with-handlers ([exn? (λ(e) (printf "Already running, bye.\n") (exit))])
    (tcp-listen *port*)))
(printf "Working...\n")
(sleep 10)

Raku

(formerly Perl 6)

Works with: rakudo version 2018.03

An old-school Unix solution, none the worse for the wear:

my $name = $*PROGRAM-NAME;
my $pid = $*PID;

my $lockdir = "/tmp";
my $lockfile = "$lockdir/$name.pid";
my $lockpid = "$lockfile$pid";
my $havelock = False;

END {
    unlink $lockfile if $havelock;
    try unlink $lockpid;
}

my $pidfile = open "$lockpid", :w orelse .die;
$pidfile.say($pid);
$pidfile.close;

if try link($lockpid, $lockfile) {
    $havelock = True;
}
else {
    shell "kill -CONT `cat $lockfile` || rm $lockfile";
    if try link($lockfile, $lockpid) {
        $havelock = True;
    }
    else {
        die "You can't run right now!";
    }
}
note "Got lock!";
unlink $lockpid;

REXX

Works with: ARexx

Solutions using a temporary file as a semaphore aren't very clean; if the program ends abruptly, the file isn't cleaned up. In this solution, we will instead open an ARexx port of our own. Ports are automatically closed by the interpreter if the program is ABENDed.

/* Simple ARexx program to open a port after checking if it's already open */
IF Show('PORTS','ROSETTA') THEN DO           /* Port is already open; exit */
   SAY 'This program may only be run in a single instance at a time.'
   EXIT 5                                    /* Exit with a mild warning   */
   END
                 /* Open rexxsupport.library so that ports can be opened   */
IF ~Show('LIBRARIES','rexxsupport.library') 
   THEN CALL AddLib('rexxsupport.library',0,-30,0) 

IF ~OpenPort('ROSETTA')    THEN EXIT 10       /* Open port, end if it fails */

SAY 'Program is now running.'

DO FOREVER                                    /* Busyloop                   */
   /* Program stuff here */
   END

EXIT 0

Ring

# Project : Determine if only one instance is running

task = "ringw.exe"
taskname = "tasklist.txt"
remove(taskname)
system("tasklist >> tasklist.txt")
fp = fopen(taskname,"r")
tasks = read("tasklist.txt")
counttask = count(tasks,task)
if counttask > 0
   see task + " running in " + counttask + " instances" + nl
else
   see task + " is not running" + nl
ok

func count(cString,dString)
     sum = 0
     while substr(cString,dString) > 0
           sum++
           cString = substr(cString,substr(cString,dString)+len(string(sum)))
     end
     return sum

Output:

ringw.exe running in 2 instances

Ruby

Uses file locking on the program file

def main
  puts "first instance"
  sleep 20
  puts :done
end

if $0 == __FILE__
  if File.new(__FILE__).flock(File::LOCK_EX | File::LOCK_NB)
    main
  else
    raise "another instance of this program is running"
  end
end

__END__

Run BASIC

if instr(shell$("tasklist"),"rbp.exe") <> 0 then print "Task is Running"

Rust

Using TCP socket

use std::net::TcpListener;

fn create_app_lock(port: u16) -> TcpListener {
    match TcpListener::bind(("0.0.0.0", port)) {
        Ok(socket) => {
            socket
        },
        Err(_) => {
            panic!("Couldn't lock port {}: another instance already running?", port);
        }
    }
}

fn remove_app_lock(socket: TcpListener) {
    drop(socket);
}

fn main() {
    let lock_socket = create_app_lock(12345);
    // ...
    // your code here
    // ...
    remove_app_lock(lock_socket);
}

Scala

Java Interoperability

Output:

Best seen running in your browser Scastie (remote JVM).

import java.io.IOException
import java.net.{InetAddress, ServerSocket}

object SingletonApp extends App {
  private val port = 65000

  try {
    val s = new ServerSocket(port, 10, InetAddress.getLocalHost)
  }
  catch {
    case _: IOException =>
      // port taken, so app is already running
      println("Application is already running, so terminating this instance.")
      sys.exit(-1)
  }

  println("OK, only this instance is running but will terminate in 10 seconds.")

  Thread.sleep(10000)

  sys.exit(0)

}

Sidef

# For this to work, you need to explicitly
# store the returned fh inside a variable.
var fh = File(__FILE__).open_r

# Now call the flock() method on it
fh.flock(File.LOCK_EX | File.LOCK_NB) ->
    || die "I'm already running!"

# Your code here...
say "Running..."
Sys.sleep(20)
say 'Done!'

Swift

Uses NSDistributedNotificationCenter. Works with Swift 1.2.

import Foundation

let globalCenter = NSDistributedNotificationCenter.defaultCenter()
let time = NSDate().timeIntervalSince1970

globalCenter.addObserverForName("OnlyOne", object: nil, queue: NSOperationQueue.mainQueue()) {not in
    if let senderTime = not.userInfo?["time"] as? NSTimeInterval where senderTime != time {
        println("More than one running")
        exit(0)
    } else {
        println("Only one")
    }
}

func send() {
    globalCenter.postNotificationName("OnlyOne", object: nil, userInfo: ["time": time])
    
    let waitTime = dispatch_time(DISPATCH_TIME_NOW, Int64(3 * NSEC_PER_SEC))
    
    dispatch_after(waitTime, dispatch_get_main_queue()) {
        send()
    }
}

send()
CFRunLoopRun()

Tcl

Translation of: Java
Works with: Tcl version 8.6
package require Tcl 8.6
try {
    # Pick a port number based on the name of the main script executing
    socket -server {apply {{chan args} {close $chan}}} -myaddr localhost \
            [expr {1024 + [zlib crc32 [file normalize $::argv0]] % 30000}]
} trap {POSIX EADDRINUSE} {} {
    # Generate a nice error message
    puts stderr "Application $::argv0 already running?"
    exit 1
}

TXR

Microsoft Windows

;;; Define some typedefs for clear correspondence with Win32
(typedef HANDLE cptr)
(typedef LPSECURITY_ATTRIBUTES cptr)
(typedef WINERR (enum WINERR ERROR_SUCCESS
                             (ERROR_ALREADY_EXISTS 183))) 
(typedef BOOL (enum BOOL FALSE TRUE))
(typedef LPCWSTR wstr)

;;; More familiar spelling for null pointer.
(defvarl NULL cptr-null)

;;; Define access to foreign functions.
(with-dyn-lib "kernel32.dll"
  (deffi CreateMutex "CreateMutexW" HANDLE (LPSECURITY_ATTRIBUTES BOOL LPCWSTR))
  (deffi CloseHandle "CloseHandle" BOOL (HANDLE))
  (deffi GetLastError "GetLastError" WINERR ()))

;;; Now, the single-instance program:
(defvar m (CreateMutex NULL 'TRUE "ApplicationName"))

(unless (eq (GetLastError) 'ERROR_ALREADY_EXISTS)
  ;; mutual exclusion here
  )
 
(CloseHandle m)

UNIX Shell

Works with: Bourne Shell
Works with: Bourne Again SHell
# (c) Copyright 2005 Mark Hobley
#
# This is free software. This file can be redistributed or modified
# under the terms of version 1.2 of the GNU Free Documentation Licence
# as published by the Free Software Foundation.
#

 singleinstance ()
 {
   if [ -d $SRUNDIR ] ; then
     if [ -w $SRUNDIR ] ; then
       if [ -d $SRUNDIR/$APPNAME ] ; then
         echo "Process Already Running" >& 2
         return 221
       else
         mkdir $SRUNDIR/$APPNAME
         if [ "$?" -ne 0 ] ; then
           if [ -d $SRUNDIR/$APPNAME ] ; then
             echo "Process Already Running" >& 2
             return 221
           else
             echo "Unexpected Error" >& 2
             return 239
           fi
         fi
         return 0 ; # This is a unique instance
       fi
     else
       echo "Permission Denied" >& 2
       return 210
     fi
   else
     echo "Missing Directory" >& 2
     return 199
   fi
 }

Visual Basic

Works with: Visual Basic version 4
Works with: Visual Basic version 5
Works with: Visual Basic version 6
Dim onlyInstance as Boolean
onlyInstance = not App.PrevInstance

Wren

This works by trying to create a special file with exclusive access.

If this fails then the error is captured, a suitable message is printed and the script exits.

Otherwise, the script completes normally and the special file is deleted just before it exits.

import "io" for File, FileFlags

var specialFile = "wren-exclusive._sp"
var checkOneInstanceRunning = Fn.new {
    // attempt to create the special file with exclusive access
    var ff = FileFlags.create | FileFlags.exclusive
    File.openWithFlags(specialFile, ff) { |file| }  // closes automatically if successful
}

// check if the current instance is the only one running
var fiber = Fiber.new {
    checkOneInstanceRunning.call()
}
var error = fiber.try()
if (error) {
    System.print("An instance is already running.")
    return
}

// do something that takes a while for testing purposes
var sum = 0
for (i in 1...1e8) {
   sum = sum + i
}
System.print(sum)

File.delete(specialFile) // clean up
Output:
(in window 1)
$ wren one_instance_running.wren
4.99999995e+15
$

(in window2, started before the first instance completes)
$ wren one_instance_running.wren
An instance is already running.
$