2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 07:57:21 +08:00
Files
panel/internal/services/cron.go
2024-05-29 21:31:01 +08:00

60 lines
1.4 KiB
Go

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