2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 06:47:20 +08:00

fix: 修复debian下cron不生效

This commit is contained in:
耗子
2024-07-28 15:53:57 +08:00
parent 0e715a7576
commit bbc6e3c7cc
3 changed files with 20 additions and 26 deletions

View File

@@ -168,7 +168,7 @@ ignoreregex =
var err error
switch jailName {
case "ssh":
if os.IsDebian() {
if os.IsDebian() || os.IsUbuntu() {
logPath = "/var/log/auth.log"
} else {
logPath = "/var/log/secure"

View File

@@ -2,9 +2,6 @@ package services
import (
"errors"
"fmt"
"strings"
"github.com/TheTNB/panel/v2/app/models"
"github.com/TheTNB/panel/v2/pkg/os"
"github.com/TheTNB/panel/v2/pkg/shell"
@@ -20,38 +17,29 @@ func NewCronImpl() *CronImpl {
// AddToSystem 添加到系统
func (r *CronImpl) AddToSystem(cron models.Cron) error {
if os.IsRHEL() {
if _, err := shell.Execf(fmt.Sprintf(`echo "%s %s >> %s 2>&1" >> /var/spool/cron/root`, cron.Time, cron.Shell, cron.Log)); err != nil {
return err
}
return systemctl.Restart("crond")
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
}
if os.IsDebian() {
if _, err := shell.Execf(fmt.Sprintf(`echo "%s %s >> %s 2>&1" >> /var/spool/cron/crontabs/root`, cron.Time, cron.Shell, cron.Log)); err != nil {
return err
}
return systemctl.Restart("cron")
}
return errors.New("不支持的系统")
return r.restartCron()
}
// DeleteFromSystem 从系统中删除
func (r *CronImpl) DeleteFromSystem(cron models.Cron) error {
// 需要转义 shell 路径的/为\/
cron.Shell = strings.ReplaceAll(cron.Shell, "/", "\\/")
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() {
if _, err := shell.Execf("sed -i '/" + cron.Shell + "/d' /var/spool/cron/root"); err != nil {
return err
}
return systemctl.Restart("crond")
}
if os.IsDebian() {
if _, err := shell.Execf("sed -i '/" + cron.Shell + "/d' /var/spool/cron/crontabs/root"); err != nil {
return err
}
if os.IsDebian() || os.IsUbuntu() {
return systemctl.Restart("cron")
}

View File

@@ -15,3 +15,9 @@ func IsRHEL() bool {
_, err := os.Stat("/etc/redhat-release")
return err == nil
}
// IsUbuntu 判断是否是 Ubuntu 系统
func IsUbuntu() bool {
_, err := os.Stat("/etc/lsb-release")
return err == nil
}