From 2bd2068c999cda5bda8c0787ed0dcaac6cb7afdb Mon Sep 17 00:00:00 2001 From: zhangzengfei <zhangzengfei@iotlink.com> Date: 星期四, 31 十月 2019 13:27:46 +0800 Subject: [PATCH] feat: add system reboot api --- extend/sys/system.go | 55 +++++++++++++++++++++++++++++++++++++++++++------------ 1 files changed, 43 insertions(+), 12 deletions(-) diff --git a/extend/sys/system.go b/extend/sys/system.go index cccba7e..b1bb46d 100644 --- a/extend/sys/system.go +++ b/extend/sys/system.go @@ -119,8 +119,7 @@ func NTPConfig() (bool, string, string) { status, server, interval := false, "", "" - cmd := exec.Command("/bin/sh", "-c", "crontab -l | grep ntpdate | tr -d '\n'") - cron, _ := cmd.Output() + cron, _ := execRootCommand("crontab -l | grep ntpdate | sed -z -r 's/([^0-9* ]+)(.+)//g'") if task := string(cron); task != "" { status = true slice := strings.Split(task, " ") @@ -160,8 +159,6 @@ return false } - // args := []string{"-s", newTime} - // exec.Command("date", args...).Run() dateCMD := fmt.Sprintf("date -s \"%s\"", newTime) execRootCommand(dateCMD) stopNTPCron() @@ -169,7 +166,7 @@ return true } -const NTPCRONTABFILE = "~/.webServer.crontab" +const NTPCRONTABFILE = "/tmp/.webServer.crontab" func EnableNTPCron(server string, interval int) bool { stopNTPCron() @@ -178,24 +175,32 @@ return false } - addTask := fmt.Sprintf("echo \"*/%d * * * * /usr/sbin/ntpdate %s\" >> %s; crontab %s", interval, server, NTPCRONTABFILE, NTPCRONTABFILE) - exec.Command("/bin/sh", "-c", addTask).Run() + update := fmt.Sprintf("echo \"*/%d * * * * /usr/sbin/ntpdate %s\" >> %s", interval, server, NTPCRONTABFILE) + execRootCommand(update) + + addNTPTask := fmt.Sprintf("crontab %s", NTPCRONTABFILE) + execRootCommand(addNTPTask) return true } func stopNTPCron() { - cleanTask := fmt.Sprintf("crontab -l | grep -v /usr/sbin/ntpdate > %s; crontab %s", NTPCRONTABFILE, NTPCRONTABFILE) - exec.Command("/bin/sh", "-c", cleanTask).Run() + update := fmt.Sprintf("crontab -l | grep -v /usr/sbin/ntpdate > %s", NTPCRONTABFILE) + execRootCommand(update) + + cleanNTPTask := fmt.Sprintf("crontab %s", NTPCRONTABFILE) + execRootCommand(cleanNTPTask) } -func RunNTPDate(server string) (bool, error) { +func RunNTPDate(server string) bool { if ip := net.ParseIP(server); ip == nil { - return false, errors.New("鍙傛暟閿欒") + return false } ntpdate := fmt.Sprintf("/usr/sbin/ntpdate %s", server) - return true, exec.Command("/bin/sh", "-c", ntpdate).Run() + _, err := execRootCommand(ntpdate) + + return err == nil } func Reboot() (bool, string) { @@ -203,3 +208,29 @@ return err == nil, string(stdout) } + +// * * * * * /bin/echo "$(date) Perform basic-reboot-task" >> /tmp/webserver.crontab.log;/sbin/reboot & >> /tmp/webserver.crontab.log +func ReadRebootTaskInCrontab() (bool, string) { + stdout, err := execRootCommand("crontab -l | grep basic-reboot-task | sed -z -r 's/([^0-9* ]+)(.+)//g'") + return err == nil, string(stdout) +} + +func CleanRebootTask() { + update := fmt.Sprintf("crontab -l | grep -v basic-reboot-task > %s", NTPCRONTABFILE) + execRootCommand(update) + + crontab := fmt.Sprintf("crontab %s", NTPCRONTABFILE) + execRootCommand(crontab) +} + +func UpdateRebootTask(task string) bool { + CleanRebootTask() + + tasks := fmt.Sprintf("%s /bin/echo \"$(date) Perform basic-reboot-task\" >> /tmp/webserver.crontab.log;/sbin/reboot & >> /tmp/webserver.crontab.log", task) + update := fmt.Sprintf("echo '%s' >> %s", tasks, NTPCRONTABFILE) + execRootCommand(update) + addNTPTask := fmt.Sprintf("crontab %s", NTPCRONTABFILE) + _, err := execRootCommand(addNTPTask) + + return err == nil +} -- Gitblit v1.8.0