https://github.com/makifdb/pidfile
A Go package for single-instance execution via PID file management
https://github.com/makifdb/pidfile
go golang pid pid-control
Last synced: 25 days ago
JSON representation
A Go package for single-instance execution via PID file management
- Host: GitHub
- URL: https://github.com/makifdb/pidfile
- Owner: makifdb
- License: mit
- Created: 2023-11-29T02:24:22.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-11-29T02:27:11.000Z (over 2 years ago)
- Last Synced: 2025-12-26T06:48:11.897Z (6 months ago)
- Topics: go, golang, pid, pid-control
- Language: Go
- Homepage:
- Size: 2.93 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PIDFile Package for Go
The `pidfile` package provides a simple way to ensure that only one instance of a Go application runs at any given time by using PID (Process ID) files.
## Features
- Easy creation and management of a PID file.
- Checks if an application is already running.
- Prevents multiple instances of the application from running concurrently.
## Installation
To install the `pidfile` package, use the following `go get` command:
```sh
go get github.com/makifdb/pidfile
```
## Example
Here is a full example of an application that uses the pidfile package to ensure single instance execution
```go
package main
import (
"fmt"
"log"
"os"
"github.com/makifdb/pidfile"
)
func main() {
// Define the PID file path
pidFilePath := filepath.Join(os.TempDir(), "example.pid")
// Create or update the PID file
err := pidfile.CreateOrUpdatePIDFile(pidFilePath)
if err != nil {
log.Fatalf("Unable to create or update PID file: %v", err)
}
// Defer the removal of the PID file on application exit
defer os.Remove(pidFilePath)
// Your application logic goes here
fmt.Println("Application started successfully.")
// Application logic...
}
```
## Notes
This package assumes that the PID file is stored in a location that is writable by the application.
Make sure to handle the PID file correctly to prevent orphaned PID files, which could prevent the application from starting.
## License
This pidfile package is open-source software licensed under the MIT license.