https://github.com/miguel-dorta/si
Single Instance: keep just one instance of your program
https://github.com/miguel-dorta/si
execution singleton unique
Last synced: about 1 month ago
JSON representation
Single Instance: keep just one instance of your program
- Host: GitHub
- URL: https://github.com/miguel-dorta/si
- Owner: Miguel-Dorta
- License: mit
- Created: 2020-02-06T18:10:11.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-04-11T23:56:51.000Z (almost 6 years ago)
- Last Synced: 2024-06-20T02:47:25.714Z (over 1 year ago)
- Topics: execution, singleton, unique
- Language: Go
- Size: 12.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# si
Single Instance: keep just one instance of your program.
[](https://dev.azure.com/Miguel-Dorta/si/_build/latest?definitionId=1&branchName=master)
[](https://pkg.go.dev/github.com/Miguel-Dorta/si)
## Description
si is a Go(Lang) package that helps you to find and communicate with other instances of your program.
## Example
```go
package main
import (
"fmt"
"github.com/Miguel-Dorta/si"
"os"
)
func main() {
// Example 1: Register your instance and exit if there's another one running
if err := si.Register("my awesome program"); err != nil {
if err == si.ErrOtherInstanceRunning {
fmt.Println("There's another instance running, bye!")
os.Exit(0)
}
fmt.Println("Error registering instance: " + err.Error())
os.Exit(1)
}
// Example 2: Find another instance and kill it
p, err := si.Find("my awesome program")
if err != nil {
fmt.Println("Error finding another instance: " + err.Error())
os.Exit(1)
}
if p == nil {
fmt.Println("No other instance was found, bye!")
os.Exit(0)
}
p.process.Kill()
}
```
### Important
If you want to communicate with your program using si.Process.StdXPipe(), this should have a pipe asigned to its stdX, else it will have /dev/null as default. This makes impossible the communication between processes. See [si/test/test.go/createInstance2()](https://github.com/Miguel-Dorta/si/blob/1689b65dcb56c7849fb21539e922639b6a76ded9/test/test.go#L61) to see an example.
#### Meme
