Commit 9a78857b authored by Matthew Holt's avatar Matthew Holt

Startup/shutdown commands run in background with &

parent 1e730a74
...@@ -3,6 +3,7 @@ package setup ...@@ -3,6 +3,7 @@ package setup
import ( import (
"os" "os"
"os/exec" "os/exec"
"strings"
"github.com/mholt/caddy/middleware" "github.com/mholt/caddy/middleware"
) )
...@@ -20,11 +21,19 @@ func Shutdown(c *Controller) (middleware.Middleware, error) { ...@@ -20,11 +21,19 @@ func Shutdown(c *Controller) (middleware.Middleware, error) {
// to the list of callback functions passed in by reference. // to the list of callback functions passed in by reference.
func registerCallback(c *Controller, list *[]func() error) error { func registerCallback(c *Controller, list *[]func() error) error {
for c.Next() { for c.Next() {
if !c.NextArg() { args := c.RemainingArgs()
if len(args) == 0 {
return c.ArgErr() return c.ArgErr()
} }
command, args, err := middleware.SplitCommandAndArgs(c.Val()) nonblock := false
if len(args) > 1 && args[len(args)-1] == "&" {
// Run command in background; non-blocking
nonblock = true
args = args[:len(args)-1]
}
command, args, err := middleware.SplitCommandAndArgs(strings.Join(args, " "))
if err != nil { if err != nil {
return c.Err(err.Error()) return c.Err(err.Error())
} }
...@@ -33,7 +42,12 @@ func registerCallback(c *Controller, list *[]func() error) error { ...@@ -33,7 +42,12 @@ func registerCallback(c *Controller, list *[]func() error) error {
cmd := exec.Command(command, args...) cmd := exec.Command(command, args...)
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
return cmd.Run()
if nonblock {
return cmd.Start()
} else {
return cmd.Run()
}
} }
*list = append(*list, fn) *list = append(*list, fn)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment