mdserver-web/web/admin/setup/init_cron.py

141 lines
3.9 KiB
Python
Raw Permalink Normal View History

2025-03-19 23:48:12 -04:00
# coding:utf-8
# ---------------------------------------------------------------------------------
# MW-Linux面板
# ---------------------------------------------------------------------------------
# copyright (c) 2018-∞(https://github.com/midoks/mdserver-web) All rights reserved.
# ---------------------------------------------------------------------------------
# Author: midoks <midoks@163.com>
# ---------------------------------------------------------------------------------
2025-03-23 05:14:49 -04:00
import os
2025-03-23 04:50:30 -04:00
import core.mw as mw
2025-03-23 05:14:49 -04:00
from utils.crontab import crontab
from croniter import croniter
from datetime import datetime
2025-03-23 12:06:42 -04:00
import thisdb
def cron_todb(data):
# print("------")
rdata = {}
if data[3] == "*" and data[2] != "*" :
rdata['type'] = 'month'
elif data[3] == "*" and data[4] != "*" :
rdata['type'] = 'week'
elif data[3] == "*" and data[4] == "*" and data[2] == "*" :
rdata['type'] = 'day'
elif data[1].find("/") > -1 :
rdata['type'] = 'hour-n'
elif data[0].find("/") > -1 :
rdata['type'] = 'minute-n'
# print(rdata)
# print(data)
return rdata
# print("------")
2025-03-19 23:48:12 -04:00
2025-06-25 10:54:45 -04:00
def init_acme_cron():
name = "[勿删]ACME定时强制更新"
res = mw.M("crontab").field("id, name").where("name=?", (name,)).find()
if res:
return True
2025-11-12 13:26:40 -05:00
cmd = "/root/.acme.sh/acme.sh --cron --force --standalone"
2025-06-25 10:54:45 -04:00
params = {
'name': name,
2025-06-25 11:06:09 -04:00
'type': 'day-n',
2025-06-25 10:54:45 -04:00
'week': "",
2026-02-24 23:35:44 -05:00
'where1': "15",
2025-06-25 10:55:40 -04:00
'hour': 4,
'minute': 15,
2025-06-25 10:54:45 -04:00
'save': "",
'backup_to': "",
'stype': "toShell",
'sname': '',
'sbody': cmd,
'url_address': '',
2025-07-13 09:19:30 -04:00
'attr':'',
2025-06-25 10:54:45 -04:00
}
2025-06-25 10:59:42 -04:00
crontab.instance().add(params)
2025-06-25 10:54:45 -04:00
return True
2025-10-14 06:32:17 -04:00
def init_auto_update():
name = "[可删]面板自动更新"
res = mw.M("crontab").field("id, name").where("name=?", (name,)).find()
if res:
return True
2025-10-14 06:33:59 -04:00
cmd = "mw update"
2025-10-14 06:32:17 -04:00
params = {
'name': name,
2025-10-14 06:35:36 -04:00
'type': 'month',
2025-10-14 06:32:17 -04:00
'week': "",
'where1': "1",
'hour': 4,
'minute': 15,
'save': "",
'backup_to': "",
'stype': "toShell",
'sname': '',
'sbody': cmd,
'url_address': '',
'attr':'',
}
crontab.instance().add(params)
return True
2025-03-19 23:48:12 -04:00
# 识别linux计划任务
def init_cron():
2025-03-23 05:14:49 -04:00
file = ''
cron_file = [
'/var/spool/cron/crontabs/root',
'/var/spool/cron/root',
]
for i in cron_file:
if os.path.exists(i):
file = i
2025-03-23 04:50:30 -04:00
2025-03-23 05:14:49 -04:00
if file == "":
return True
2025-03-23 12:06:42 -04:00
2025-03-23 05:14:49 -04:00
with open(file) as f:
for line in f.readlines():
cron_line = line.strip()
if cron_line.startswith("#"):
continue
2025-03-23 05:27:43 -04:00
2025-03-23 12:06:42 -04:00
cron_expression = cron_line.split(maxsplit=5) # 提取前 5 个字段(* * * * *
2025-03-23 05:27:43 -04:00
command = cron_line.split(maxsplit=5)[5] # 提取命令部分
# 面板计划任务过滤
if command.startswith("/www/server/cron"):
continue
2025-03-23 12:06:42 -04:00
if command.startswith("\"/root/.acme.sh\""):
# print(cron_expression, command)
info = cron_todb(cron_expression)
# print(info)
# add_dbdata = {}
# add_dbdata['name'] = "ACME"
# add_dbdata['type'] = data['type']
# add_dbdata['where1'] = data['where1']
# add_dbdata['where_hour'] = data['hour']
# add_dbdata['where_minute'] = data['minute']
# add_dbdata['save'] = ""
# add_dbdata['backup_to'] = ""
# add_dbdata['sname'] = ""
# add_dbdata['sbody'] = data['sbody']
# add_dbdata['stype'] = "toShell"
# add_dbdata['echo'] = command
# add_dbdata['url_address'] = ''
# thisdb.addCrontab(add_dbdata)
2025-03-23 05:27:43 -04:00
2025-03-23 12:06:42 -04:00
# print(command)
2025-03-23 05:14:49 -04:00
# cron_list = content.split("\n")
# print(cron_list)