update
This commit is contained in:
@@ -10,8 +10,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
host = flag.String("host", "localhost", "Server host")
|
||||
port = flag.String("port", "26666", "Server port")
|
||||
host = flag.String("h", "localhost", "Server host")
|
||||
port = flag.String("p", "26666", "Server port")
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
39
server.go
39
server.go
@@ -7,17 +7,52 @@ import (
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
port = flag.String("port", "26666", "Port to listen on")
|
||||
logFile = flag.String("logfile", "server.log", "Log file path")
|
||||
port = flag.String("p", "26666", "Port to listen on")
|
||||
logFile = flag.String("log", "server.log", "Log file path")
|
||||
daemon = flag.Bool("d", false, "Run as daemon")
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
// If daemon flag is set, restart the program in background
|
||||
if *daemon {
|
||||
// Get the path of the current executable
|
||||
executable, err := os.Executable()
|
||||
if err != nil {
|
||||
log.Fatal("Failed to get executable path:", err)
|
||||
}
|
||||
|
||||
// Prepare command to run the program in background
|
||||
// We exclude the -d flag to prevent infinite loop
|
||||
args := []string{}
|
||||
if *port != "26666" {
|
||||
args = append(args, "-p", *port)
|
||||
}
|
||||
if *logFile != "server.log" {
|
||||
args = append(args, "-log", *logFile)
|
||||
}
|
||||
|
||||
cmd := exec.Command(executable, args...)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
// Start the command in background
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
log.Fatal("Failed to start daemon:", err)
|
||||
}
|
||||
|
||||
// Print the PID of the background process
|
||||
fmt.Printf("Server started as daemon with PID %d\n", cmd.Process.Pid)
|
||||
return
|
||||
}
|
||||
|
||||
// Open log file
|
||||
file, err := os.OpenFile(*logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user