1

I need to only allow one instance of my Golang executable at a time. I'm not sure how to use a Global Mutex to make sure no other instances are running. Because the sw is very memory-consuming, I want to allow only one instance of the executable to run. The other instances should wait untill the working instance has finished and then start working. This would be running on a Linux Machine.

There is same question for windows existing: Restricting to Single Instance of Executable with Golang

enter image description here

kostix
  • 51,517
  • 14
  • 93
  • 176
  • 1
    A Unix-domain socket, probably an abstract one (Linux-specific) could help. But still: what about merely creating a (user-level) `systemd` unit which would ensure only a single instnce of your program is active at any given time? You could then run the program by something like `systemd --user start that_program.service`. – kostix Jan 27 '22 at 11:52
  • 1
    Also, actually quite many forms of (POSIX) IPC can be used for such coordination. For instance—blocking attempt to take a lock on a file with an agreed-upon name: once one instance of the program is about to finish its execution it releases the lock and one of the waiting instances retakes the lock. This is inherently unordererd, but if you need ordering, you can have a mediating service to which each running instance connects and hears back when it's time for its turn. – kostix Jan 27 '22 at 11:57
  • 1
    While we're at it, please note that your question must not be tagged `go` because it wasn't about _programming_ in Go as it did not present any language-level problem. – kostix Jan 27 '22 at 11:58
  • I solved it this way: At the start of the programm you have to get a lock, if you cant get it, wait. At the end the lock is released. https://stackoverflow.com/questions/52986413/how-to-get-an-exclusive-lock-on-a-file-in-go – TrailSurfer Jan 27 '22 at 14:36

0 Answers0