Skip to content

Commit

Permalink
Merge pull request #3 from jehiah/cmd_svstat_3
Browse files Browse the repository at this point in the history
Add cmd/svstat
  • Loading branch information
jehiah authored Jan 12, 2022
2 parents cbef49c + 160924c commit 3a7b5ea
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

Go library for managing daemontools (svc, svstat)

`go install github.com/jehiah/go-daemontools/cmd/svstat@latest`

<https://cr.yp.to/daemontools.html>
1 change: 1 addition & 0 deletions cmd/svstat/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
svstat
22 changes: 22 additions & 0 deletions cmd/svstat/cmd_svstat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"fmt"
"os"

"github.com/jehiah/go-daemontools"
)

func main() {
exitCode := 0
for _, arg := range os.Args[1:] {
stat, err := daemontools.Svstat(arg)
if err != nil {
exitCode += 1
fmt.Println(fmt.Sprintf("%s:", arg), err)
} else {
fmt.Println(stat)
}
}
os.Exit(exitCode)
}
16 changes: 15 additions & 1 deletion svstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package daemontools
import (
"encoding/binary"
"fmt"
"io/fs"
"os"
"path"
"syscall"
"time"
)

Expand Down Expand Up @@ -46,10 +48,22 @@ func (s *Status) String() (o string) {
return
}

type Error struct {
Msg string
Err error
}

func (e Error) Error() string {
return e.Msg
}

func Svstat(service string) (s *Status, err error) {
var f *os.File
f, err = os.OpenFile(path.Join(service, "supervise", "ok"), os.O_WRONLY, 0)
f, err = os.OpenFile(path.Join(service, "supervise", "ok"), os.O_WRONLY|syscall.O_NONBLOCK, 0)
if err != nil {
if err, ok := err.(*fs.PathError); ok && err.Err.Error() == "no such device or address" {
return nil, Error{Msg: "supervise not running", Err: err}
}
return nil, err
}
f.Close()
Expand Down

0 comments on commit 3a7b5ea

Please sign in to comment.