2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-05 00:39:32 +08:00
Files
panel/app/services/cron.go
2023-08-14 22:26:42 +08:00

45 lines
1.1 KiB
Go

package services
import (
"strings"
"panel/app/models"
"panel/pkg/tools"
)
type Cron interface {
AddToSystem(cron models.Cron)
DeleteFromSystem(cron models.Cron)
}
type CronImpl struct {
}
func NewCronImpl() *CronImpl {
return &CronImpl{}
}
// AddToSystem 添加到系统
func (r *CronImpl) AddToSystem(cron models.Cron) {
if tools.IsRHEL() {
tools.Exec("echo \"" + cron.Time + " " + cron.Shell + " >> " + cron.Log + " 2>&1\" >> /var/spool/cron/root")
tools.Exec("systemctl restart crond")
} else {
tools.Exec("echo \"" + cron.Time + " " + cron.Shell + " >> " + cron.Log + " 2>&1\" >> /var/spool/cron/crontabs/root")
tools.Exec("systemctl restart cron")
}
}
// DeleteFromSystem 从系统中删除
func (r *CronImpl) DeleteFromSystem(cron models.Cron) {
// 需要转义Shell路径的/为\/
cron.Shell = strings.ReplaceAll(cron.Shell, "/", "\\/")
if tools.IsRHEL() {
tools.Exec("sed -i '/" + cron.Shell + "/d' /var/spool/cron/root")
tools.Exec("systemctl restart crond")
} else {
tools.Exec("sed -i '/" + cron.Shell + "/d' /var/spool/cron/crontabs/root")
tools.Exec("systemctl restart cron")
}
}