~anjan/mnc

add652178f2de0aacbbe4b017297127272078345 — Maarten van Gompel 4 months ago f75ac19 0.3
Do not invoke crontab, read input from stdin instead. Recoded the awk trick in Go.
2 files changed, 29 insertions(+), 16 deletions(-)

M README.md
M mnc.go
M README.md => README.md +16 -3
@@ 14,15 14,28 @@ go build mnc.go

## Usage


Returns the number of seconds until the next cronjob is due, use with ``crontab -l``:

```sh
crontab -l | mnc
```

Mnc reads from stdin and supports only crontab-style input

## Use case

A construction like this is used in Sxmo to wakeup the system when the next cronjob is due:

```sh
sxmoscreenlock.sh rtc "$(mnc)"
sxmoscreenlock.sh rtc "$(crontab -l | mnc)"
```

Make sure you run this command via st on the phone rather than over ssh.
In this use case, make sure you run this command via st on the phone rather than over ssh.

## Support and patches

Send support questions and patches to the mailing list: 
Send support questions and patches to the mailing list:
[public inbox](https://lists.sr.ht/~anjan/public-inbox).

Please use [git send-email](https://git-send-email.io/) and send a patch to my

M mnc.go => mnc.go +13 -13
@@ 6,7 6,7 @@ import (
	"fmt"
	"io/ioutil"
	"log"
	"os/exec"
	"os"
	"strings"
	"time"



@@ 25,25 25,28 @@ func init() {
}

func main() {
	// parser is too clever and can try to parse the command as timespec fields six/seven
	c := exec.Command("sh", "-c", "crontab -l | awk '{print $1\" \"$2\" \"$3\" \"$4\" \"$5}'")
	r, _ := c.StdoutPipe()

	// seed with a large number that rtcwake can still handle
	m := 268435455
	d := make(chan struct{})
	s := bufio.NewScanner(r)
	s := bufio.NewScanner(os.Stdin)

	go func() {
		n := time.Now()
		for s.Scan() {
			l := strings.TrimSpace(s.Text())
			//we need to only get the first 5 fields otherwise
			//the cron library will try to parse the rest too
			fields := strings.Split(strings.TrimSpace(s.Text()), " ")
			l := strings.Join(fields[:5], " ")
			p, e := cron.ParseUTC(l)
			if e == nil {
				t, e := p.Next(n)
				if e != nil { log.Fatal(e) }
				if e != nil {
					log.Fatal(e)
				}
				i := int(t.Sub(n).Seconds())
				if i < m { m = i }
				if i < m {
					m = i
				}
				log.Print(i)
			} else {
				log.Print(e)


@@ 52,11 55,8 @@ func main() {
		d <- struct{}{}
	}()

	e := c.Start()
	if e != nil { log.Fatal(e) }
	<-d
	e = c.Wait()
	if e != nil { log.Fatal(e) }

	fmt.Println(m)

}