Determine if only one instance is running: Difference between revisions
Determine if only one instance is running (view source)
Revision as of 23:35, 26 August 2022
, 1 year agosyntax highlighting fixup automation
m (→{{header|Phix}}: added syntax colouring, marked p2js incompatible) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 7:
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'.
<
procedure Single_Instance is
Line 34:
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.
Line 43:
=={{header|BaCon}}==
Using advisory locks from libc.
<
OPTION DEVICE O_NONBLOCK
Line 56:
SLEEP 5000
CLOSE DEVICE me</
=={{header|Bash Shell}}==
Line 62:
Using flock, exits 0 if you got the lock, otherwise exits 1, below is a simplified example:
<
local fd=${2:-200}
Line 72:
&& # 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
Line 79:
{{works with|BBC BASIC for Windows}}
Change 'UniqueLockName' to something more likely to be unique, such as a GUID.
<
SYS "GetLastError" TO lerr%
IF lerr% = 183 THEN
Line 89:
SYS "ReleaseMutex", Mutex%
SYS "CloseHandle", Mutex%
END</
=={{header|C}}==
Line 100:
{{libheader|POSIX}}
<
#include <stdlib.h> /* atexit, getenv, malloc */
#include <stdio.h> /* fputs, printf, puts, snprintf */
Line 193:
puts("Fin!");
return 0;
}</
=== POSIX with file creation ===
Line 203:
{{libheader|POSIX}}
<
#include <signal.h>
#include <stdio.h>
Line 238:
unlink("/tmp/MyUniqueName"); close(myfd);
return 0;
}</
=={{header|C sharp|C#}}==
===Using a TCP Port===
<
using System.Net;
using System.Net.Sockets;
Line 260:
}
}
}</
===Using a mutex===
<
// Use this class in your process to guard against multiple instances
//
Line 341:
}
}
}</
=={{header|C++}}==
Line 347:
{{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
You need a variable of type HANDLE with the same lifetime as your program. Perhaps as a member of your CWinApp object.
<syntaxhighlight 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.
<
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.
<syntaxhighlight lang
=={{header|Clojure}}==
{{trans|Java}}
<
(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 </
=={{header|D}}==
Line 380:
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()
{
Line 401:
}
}
</syntaxhighlight>
=={{header|Delphi}}==
<
{$APPTYPE CONSOLE}
Line 430:
end;
end;
end.</
=={{header|Erlang}}==
Line 446:
=={{header|FreeBASIC}}==
<
Dim linea As String
Line 456:
Close #1
Shell("del temp.txt")
Sleep</
Line 463:
Recommended over file based solutions. It has the advantage that the port is always released
when the process ends.
<
import (
Line 481:
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.
<
import (
Line 508:
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.
<
import (
Line 559:
fmt.Println(os.Getpid(), "running...")
time.Sleep(1e10)
}</
=={{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.
<
import System.Directory (doesFileExist, getAppUserDataDirectory,
removeFile)
Line 604:
-- thus will exit immediately
forkIO oneInstance
return ()</
=={{header|Icon}} and {{header|Unicon}}==
Line 610:
The following only works in Unicon. The program uses a socket as a flag.
<
if not open(":"||54321,"na") then stop("Already running")
repeat {} # busy loop
end</
Sample run:
Line 626:
=={{header|Java}}==
<
import java.net.InetAddress;
import java.net.ServerSocket;
Line 660:
}
}
}</
=={{header|Jsish}}==
Using a socket on a fixed port number.
<
var sock;
Line 675:
puts('Applicaion already running');
exit(1);
}</
{{out}}
Line 693:
=={{header|Julia}}==
{{trans|Java}}<
using Sockets
Line 711:
canopen()
</syntaxhighlight>
=={{header|Kotlin}}==
<
import java.io.IOException
Line 749:
SingleInstance.close()
}
}</
{{out}}
Line 764:
=={{header|Lasso}}==
<
local(lockfile = file('/tmp/myprocess.lockfile'))
Line 785:
sleep(10000)
stdoutnl('Execution done')</
Output Window 1:
Line 799:
=={{header|Liberty BASIC}}==
<
CallDLL #kernel32, "CreateMutexA", 0 as Long, 1 as Long, "Global\My Program" as ptr, mutex as ulong
CallDLL #kernel32, "GetLastError", LastError as Long
Line 814:
calldll #kernel32, "ReleaseMutex", mutex as ulong, ret as ulong
calldll #kernel32, "CloseHandle", mutex as ulong, ret as ulong
end</
=={{header|M2000 Interpreter}}==
We can lock a file in user folder. Only one instance can lock a file.
<syntaxhighlight lang="m2000 interpreter">
Module Checkit {
Try {
Line 828:
}
}
</syntaxhighlight>
=={{header|Mathematica}} / {{header|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.
<
If[Attributes[Global`Mutex] == {Protected},
Exit[],
Global`Mutex[x_] := Locked; Protect[Global`Mutex];
]</
=={{header|Nim}}==
===fcntl based===
<
let fn = getHomeDir() & "rosetta-code-lock"
Line 860:
echo i
sleep 1000
echo "Fin!"</
===Unix Domain Socket based===
<
from net import newSocket, bindUnix
from nativesockets import AF_UNIX, SOCK_DGRAM, IPPROTO_IP
Line 898:
server()
else:
client()</
=={{header|OCaml}}==
Replicates the '''C''' example, with the library [http://ocaml-sem.sourceforge.net/ ocaml-sem].
<
let () =
Line 912:
(* end of the app *)
sem_unlink "MyUniqueName";
sem_close sem</
The standard library of OCaml also provides a [http://caml.inria.fr/pub/docs/manual-ocaml/libref/Mutex.html Mutex] module.
=={{header|Oz}}==
<
import Application Open System
define
Line 938:
{{New Open.file init(name:stdin)} read(list:_ size:1)}
{Application.exit 0}
end</
=={{header|Perl}}==
Line 944:
Then it tries to get a lock to its own file, from where the script was called.
<
INIT
Line 952:
}
sleep 60; # then your code goes here</
=={{header|Phix}}==
{{libheader|Phix/pGUI}}
<!--<
<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>
Line 977:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<!--</
=={{header|PicoLisp}}==
Line 992:
<pre>$ ./myScript & # Start in the background
[1] 26438</pre>
<
: (call "killall" "-0" "-q" "myScript")
-> T</
===Using a mutex===
Another possibility is to 'acquire' a mutex on program start, and never release
it.
<
-> 30817 # A successful call returns the PID</
A second application trying to acquire the same mutex would receive 'NIL'
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
if (Get-Process -Name "notepad" -ErrorAction SilentlyContinue)
{
Line 1,013:
Start-Process -FilePath C:\Windows\notepad.exe
}
</syntaxhighlight>
No output because notepad.exe was not running, so it was started.
{{Out}}
Line 1,019:
</pre>
Run it again.
<syntaxhighlight lang="powershell">
if (Get-Process -Name "notepad" -ErrorAction SilentlyContinue)
{
Line 1,028:
Start-Process -FilePath C:\Windows\notepad.exe
}
</syntaxhighlight>
Since it is running a warning message is output.
{{Out}}
Line 1,036:
=={{header|PureBasic}}==
<
Mutex=CreateMutex_(0,1,#MyApp)
If GetLastError_()=#ERROR_ALREADY_EXISTS
Line 1,046:
ReleaseMutex_(Mutex)
End</
=={{header|Python}}==
Line 1,054:
Must be run from an application, not the interpreter.
<
def isOnlyInstance():
Line 1,061:
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.
Line 1,067:
=={{header|Racket}}==
{{trans|Java}}
<
#lang racket
(define *port* 12345) ; random large port number
Line 1,075:
(printf "Working...\n")
(sleep 10)
</syntaxhighlight>
=={{header|Raku}}==
Line 1,081:
{{works with|rakudo|2018.03}}
An old-school Unix solution, none the worse for the wear:
<syntaxhighlight lang="raku"
my $pid = $*PID;
Line 1,111:
}
note "Got lock!";
unlink $lockpid;</
=={{header|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.
<
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.'
Line 1,133:
END
EXIT 0</
=={{header|Ring}}==
<
# Project : Determine if only one instance is running
Line 1,159:
end
return sum
</syntaxhighlight>
Output:
<pre>
Line 1,167:
=={{header|Ruby}}==
Uses file locking on the program file
<
puts "first instance"
sleep 20
Line 1,181:
end
__END__</
=={{header|Run BASIC}}==
<
=={{header|Rust}}==
Using TCP socket
<
fn create_app_lock(port: u16) -> TcpListener {
Line 1,211:
// ...
remove_app_lock(lock_socket);
}</
=={{header|Scala}}==
===Java Interoperability===
{{Out}}Best seen running in your browser [https://scastie.scala-lang.org/ja0sMzt5SGKHSu3w8qnlQQ Scastie (remote JVM)].
<
import java.net.{InetAddress, ServerSocket}
Line 1,238:
sys.exit(0)
}</
=={{header|Sidef}}==
<
# store the returned fh inside a variable.
var fh = File(__FILE__).open_r
Line 1,252:
say "Running..."
Sys.sleep(20)
say 'Done!'</
=={{header|Swift}}==
Uses NSDistributedNotificationCenter. Works with Swift 1.2.
<
let globalCenter = NSDistributedNotificationCenter.defaultCenter()
Line 1,281:
send()
CFRunLoopRun()</
=={{header|Tcl}}==
{{trans|Java}}
{{works with|Tcl|8.6}}
<
try {
# Pick a port number based on the name of the main script executing
Line 1,295:
puts stderr "Application $::argv0 already running?"
exit 1
}</
=={{header|TXR}}==
Line 1,301:
==== Microsoft Windows ====
<
(typedef HANDLE cptr)
(typedef LPSECURITY_ATTRIBUTES cptr)
Line 1,325:
)
(CloseHandle m)</
=={{header|UNIX Shell}}==
Line 1,331:
{{works with|Bourne Shell}}
{{works with|Bourne Again SHell}}
<syntaxhighlight lang="sh">
# (c) Copyright 2005 Mark Hobley
#
Line 1,368:
fi
}
</syntaxhighlight>
=={{header|Visual Basic}}==
Line 1,374:
{{works with|Visual Basic|5}}
{{works with|Visual Basic|6}}
<
onlyInstance = not App.PrevInstance</
=={{header|Wren}}==
Line 1,383:
Otherwise, the script completes normally and the special file is deleted just before it exits.
<
var specialFile = "wren-exclusive._sp"
Line 1,409:
System.print(sum)
File.delete(specialFile) // clean up</
{{out}}
|