π§π· Read this documentation in Portuguese
SelfRestart is a Go library that allows applications to restart themselves automatically in a safe and elegant way. Unlike other solutions like go-selfupdate, this library focuses specifically on automatic process restart, offering fine-grained control over the application lifecycle.
- β Automatic Restart: Restarts the application preserving arguments and environment
- β Platform Detection: Support for Linux, macOS and Windows
- β Automatic Go Installation: Installs Go automatically if needed
- β Process Management: Complete control over PIDs and signals
- β Integrated Logging: Logging system with different levels
- β Modular: Clean and well-organized architecture
- β Thread-Safe: Safe for use in concurrent applications
go get github.com/rafa-mori/selfrestartpackage main
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/rafa-mori/selfrestart"
)
func main() {
// Create a SelfRestart instance
sr := selfrestart.New()
// Check if Go is installed
if !sr.IsGolangInstalled() {
fmt.Println("Go is not installed")
os.Exit(1)
}
// Setup signal capture
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGUSR1)
for {
select {
case <-sigChan:
fmt.Println("Restarting application...")
if err := sr.Restart(); err != nil {
fmt.Printf("Error restarting: %v\\n", err)
os.Exit(1)
}
os.Exit(0) // Terminate current process
}
}
}type SelfRestart struct {
// Internal fields (not exposed)
}Creates a new SelfRestart instance.
Checks if Go is installed on the system and offers automatic installation if needed.
Restarts the current process safely.
Returns the current process PID.
Terminates the current process gracefully.
Checks if a process with the specified PID is running.
Installs Go automatically on Unix systems.
Returns information about the current platform (OS/Architecture).
The project is organized in a modular way:
selfrestart/
βββ selfrestart.go # Main public API
βββ example/
β βββ main.go # Usage example
βββ internal/
β βββ install/ # Go installation management
β βββ platform/ # Platform detection
β βββ process/ # Process management
β βββ restart/ # Restart logic
βββ logger/ # Logging system
- install: Responsible for Go detection and automatic installation
- platform: Manages platform and architecture information
- process: Controls processes, PIDs and system signals
- restart: Implements restart logic using temporary scripts
- logger: Integrated logging system
See the example/main.go file for a complete example that demonstrates:
- System signal capture
- Automatic restart via SIGUSR1
- Continuous application monitoring
- Graceful shutdown management
To run the example:
cd example
go run main.goIn another terminal, test the restart:
# Get the application PID
ps aux | grep main
# Send restart signal
kill -USR1 <PID>PATH: Used to detect Go installation
--wait: Waits for the restart script to be executed completely
# Run all tests
go test ./...
# Run tests with coverage
go test -v -race -coverprofile=coverage.out ./...
# View coverage report
go tool cover -html=coverage.outERROR: Go is not installed or not found in PATH
Solution: The library will offer automatic Go installation.
ERROR: Error creating restart script: permission denied
Solution: Run with adequate permissions or check if /tmp is writable.
ERROR: Process did not terminate after interrupt signal
Solution: Check if there are no blocks in the application that prevent graceful shutdown.
- Fork the project
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
- Windows Service support
- systemd integration (Linux)
- Automatic backup before restart
- Webhooks for notifications
- Web interface for monitoring
- Metrics and monitoring
- Docker container support
This project is licensed under the MIT License - see the LICENSE file for details.
- Inspired by the go-selfupdate project
- Built with Go community best practices
- Special thanks to contributors
If you have any questions, suggestions or issues, feel free to reach out:
- π Documentation: GitHub Wiki
- π Issues: GitHub Issues
- π¬ Discussions: GitHub Discussions
- π€ LinkedIn: Rafael Mori
- π¦ Twitter: @faelOmori
- π§ Email: faelmori
- π Website: rafamori.pro
β If this project was useful to you, consider giving it a star on GitHub!
