From 4d2ffde1815360efaac7cb3cd08c108a8c339036 Mon Sep 17 00:00:00 2001 From: asandikci Date: Sat, 29 Jul 2023 13:04:25 +0300 Subject: [PATCH] a working godaemon1 --- .gitignore | 3 +- go-daemon/go.mod | 5 +-- go-daemon/godaemon1.go | 37 ++++++++++++++++++++++ go-daemon/godaemon1.service | 17 ++++++++++ go-daemon/info.go | 63 +++++++++++++++++++++++++++++++++++++ go-daemon/install.sh | 24 ++++++++++++++ 6 files changed, 146 insertions(+), 3 deletions(-) create mode 100644 go-daemon/godaemon1.go create mode 100644 go-daemon/godaemon1.service create mode 100644 go-daemon/info.go create mode 100644 go-daemon/install.sh diff --git a/.gitignore b/.gitignore index 46343b1..67a015d 100644 --- a/.gitignore +++ b/.gitignore @@ -77,4 +77,5 @@ Mkfile.old dkms.conf ### USER MODIFIED ### -c-daemon/bin/ \ No newline at end of file +c-daemon/bin/ +go-daemon/bin/ \ No newline at end of file diff --git a/go-daemon/go.mod b/go-daemon/go.mod index 11a638f..3530250 100644 --- a/go-daemon/go.mod +++ b/go-daemon/go.mod @@ -1,9 +1,10 @@ -module go-daemon +module godaemon1 go 1.20 +require github.com/sevlyar/go-daemon v0.1.6 + require ( github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 // indirect - github.com/sevlyar/go-daemon v0.1.6 // indirect golang.org/x/sys v0.10.0 // indirect ) diff --git a/go-daemon/godaemon1.go b/go-daemon/godaemon1.go new file mode 100644 index 0000000..9e8ca87 --- /dev/null +++ b/go-daemon/godaemon1.go @@ -0,0 +1,37 @@ +package main + +import ( + // "log" + "time" + // "github.com/sevlyar/go-daemon" +) + +func main() { + // cntxt := &daemon.Context{ + // PidFileName: "godaemon1.pid", + // PidFilePerm: 0644, + // LogFileName: "godaemon1.log", + // LogFilePerm: 0640, + // WorkDir: "./", + // Umask: 027, + // Args: []string{"[go-daemon godaemon1]"}, + // } + + // d, err := cntxt.Reborn() + // if err != nil { + // log.Fatal("Unable to run: ", err) + // } + // if d != nil { + // return + // } + // defer cntxt.Release() + // + // log.Print("- - - - - - - - - - - - - - -") + // log.Print("daemon started") + + for true { + Info() + time.Sleep(100 * time.Millisecond) + } + +} diff --git a/go-daemon/godaemon1.service b/go-daemon/godaemon1.service new file mode 100644 index 0000000..76cbce8 --- /dev/null +++ b/go-daemon/godaemon1.service @@ -0,0 +1,17 @@ +[Unit] +Description= Go Daemon-1 +After=multi-user.target + +[Service] +Type=simple +User=root +Group=root +ExecStart=/usr/bin/godaemon1 +PIDFile=/run/godaemon1.pid + +PrivateTmp=false +Restart=always + +[Install] +WantedBy=multi-user.target +Alias=godaemon1.service \ No newline at end of file diff --git a/go-daemon/info.go b/go-daemon/info.go new file mode 100644 index 0000000..21513f1 --- /dev/null +++ b/go-daemon/info.go @@ -0,0 +1,63 @@ +package main + +import ( + "log" + "os" + "runtime" + "time" +) + +const ProgramName = "go-daemon" + +func checkExists(path string) (bool, error) { + _, err := os.Stat(path) + if err == nil { + return true, nil + } + if os.IsNotExist(err) { + return false, nil + } + return false, err +} + +func Info() { + // A SAMPLE COMMAND FOR LOGGING OS INFORMATION TO A DEDICATED FILE + // userhome, err := os.UserHomeDir() // TODO $HOME is not defined while running as daemon !!! + // if err != nil { + // log.Fatal(err) + // } + + userhome := "/home/asandikci" // TEMPORARY + configDir := userhome + "/.config/" + ProgramName + "/" + // TODO Will be replaced with a Go Config Library + + if flag, err := checkExists(configDir); flag { + // log.Println("Config dir already exists") + if err != nil { + log.Fatal(err) + } + } else if err := os.Mkdir(configDir, os.ModePerm); err != nil { + log.Fatal(err) + } + + osInfo := []struct { + str1, str2 string + }{ + {"OS", runtime.GOOS}, + {"Time", time.Now().Format("2006.01.02 15:04:05")}, + } + + f, err := os.Create(configDir + "godaemon1.conf") + if err != nil { + log.Fatal(err) + } + + defer f.Close() + + for _, info := range osInfo { + _, err := f.WriteString(info.str1 + ": " + info.str2 + "\n") + if err != nil { + log.Fatal(err) + } + } +} diff --git a/go-daemon/install.sh b/go-daemon/install.sh new file mode 100644 index 0000000..58d9429 --- /dev/null +++ b/go-daemon/install.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +### godaemon1 +# Pre-Cleaning +sudo rm /usr/bin/godaemon1 +sudo rm -rf /usr/lib/godaemon1/ + +# Building +temp_dir="$(mktemp -d)" +cd $temp_dir +echo $temp_dir +git clone https://git.aliberksandikci.com.tr/liderahenk/go-daemon +cd go-daemon/go-daemon/ +go build -o ./bin/ + +# Installing +sudo mv ./bin/godaemon1 /usr/bin/godaemon1 +sudo mv ./godaemon1.service /etc/systemd/system/ +sudo systemctl daemon-reload +sudo systemctl enable godaemon1 +sudo systemctl start godaemon1 + +# Post-Cleaning +sudo rm -rf $temp_dir \ No newline at end of file