golang执行linux后台命令
示例:
package main
import (
'fmt'
'os/exec'
'strings'
)
func generateBgCmd(name string, arg ...string) string {
cmdString := fmt.Sprintf(
'nohup %s %s &',
name,
strings.Join(arg, ' '),
)
return cmdString
}
func main() {
cmd := exec.Command('/bin/bash', '-c', generateBgCmd('ping', 'baidu.com', '>', '/tmp/ping.log'))
err := cmd.Start()
fmt.Println('err:', err)
}
赞0
踩0