I'm using manjaro linux (arch based) with systemd. I want to make a daemon service that runs in the background and takes screenshots of my screen at random intervals, up to 10 minutes apart.
I'm using Go, and I have a program that works just fine if I build it and run it from the command line (see below). I've made a .service file (see below) for the service and I've started it with systemctl start screenshot
and confirmed it's running with systemctl is-active screenshot
. However, it doesn't work. No screenshots are being saved like if I run it from the command line.
Out of interest I tried running it with nohup ./screenshot &
and it suffers from the same problem as my daemon attempt.
Is there some sort of problem with using the date package in the background? Or could it perhaps be the screenshot library?
screenshot.go
package main
import (
"fmt"
"image/png"
"math/rand"
"os"
"time"
"github.com/coreos/go-systemd/daemon"
"github.com/vova616/screenshot"
)
func main() {
daemon.SdNotify(false, "READY=1")
i := 0
for {
r := rand.Intn(10)
time.Sleep(time.Duration(r) * time.Minute)
t := time.Now()
year, month, day := t.Date()
date := fmt.Sprintf("%d-%s-%d", year, month.String(), day)
img, err := screenshot.CaptureScreen()
if err != nil {
fmt.Println(err)
continue
}
hour, min, sec := t.Clock()
stamp := fmt.Sprintf("%d:%d:%d", hour, min, sec)
fmt.Printf("Taking screenshot %s
", stamp)
f, err := os.Create(fmt.Sprintf("/home/dave/screenshot/%s/%s.png", date, stamp))
if err != nil {
fmt.Println(err)
continue
}
err = png.Encode(f, img)
if err != nil {
fmt.Println(err)
continue
}
err = f.Close()
if err != nil {
fmt.Println(err)
continue
}
i++
daemon.SdNotify(false, "WATCHDOG=1")
}
}
and here is the service file
[Unit]
Description=Random Screenshot Service
[Service]
Type=notify
ExecStart=/home/dave/screenshot/screenshot
WatchdogSec=30s
Restart=on-failure
[Install]
WantedBy=multi-user.target