-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
cron.go
47 lines (37 loc) · 1.07 KB
/
cron.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package services
import (
"errors"
"github.com/TheTNB/panel/v2/app/models"
"github.com/TheTNB/panel/v2/pkg/os"
"github.com/TheTNB/panel/v2/pkg/shell"
"github.com/TheTNB/panel/v2/pkg/systemctl"
)
type CronImpl struct {
}
func NewCronImpl() *CronImpl {
return &CronImpl{}
}
// AddToSystem 添加到系统
func (r *CronImpl) AddToSystem(cron models.Cron) error {
if _, err := shell.Execf(`( crontab -l; echo "%s %s >> %s 2>&1" ) | sort - | uniq - | crontab -`, cron.Time, cron.Shell, cron.Log); err != nil {
return err
}
return r.restartCron()
}
// DeleteFromSystem 从系统中删除
func (r *CronImpl) DeleteFromSystem(cron models.Cron) error {
if _, err := shell.Execf(`( crontab -l | grep -v -F "%s %s >> %s 2>&1" ) | crontab -`, cron.Time, cron.Shell, cron.Log); err != nil {
return err
}
return r.restartCron()
}
// restartCron 重启 cron 服务
func (r *CronImpl) restartCron() error {
if os.IsRHEL() {
return systemctl.Restart("crond")
}
if os.IsDebian() || os.IsUbuntu() {
return systemctl.Restart("cron")
}
return errors.New("不支持的系统")
}