a working godaemon1

This commit is contained in:
Aliberk Sandıkçı 2023-07-29 13:04:25 +03:00
parent 6d7466bd78
commit 4d2ffde181
Signed by: asandikci
GPG key ID: 25C67A03B5666BC1
6 changed files with 146 additions and 3 deletions

1
.gitignore vendored
View file

@ -78,3 +78,4 @@ dkms.conf
### USER MODIFIED ###
c-daemon/bin/
go-daemon/bin/

View file

@ -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
)

37
go-daemon/godaemon1.go Normal file
View file

@ -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)
}
}

View file

@ -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

63
go-daemon/info.go Normal file
View file

@ -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)
}
}
}

24
go-daemon/install.sh Normal file
View file

@ -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