You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:实现脚本24小时运行间隔锁定功能的技术方案

可行解决方案

一、利用系统定时任务(推荐)

这是最可靠的实现方式,完全由系统管控运行周期,彻底规避手动篡改和时间精度问题:

  • Linux/macOS:使用crontab配置固定时间运行。比如要每天晚8点执行脚本,执行crontab -e编辑定时任务,添加:
    0 20 * * * /usr/bin/python3 /path/to/your/script.py
    
    同时在脚本开头加校验逻辑:仅允许系统定时任务触发的运行(比如检查当前时间是否在20:00前后5分钟窗口内),手动启动直接退出。
  • Windows:打开「任务计划程序」创建基本任务,触发条件设为“每天”、时间选20:00,操作选择启动你的脚本。同样在脚本内添加校验,拒绝非任务计划的手动启动。

二、加密存储运行时间(防篡改)

如果必须保留手动启动脚本的逻辑,可将上次运行时间加密后存储,用户篡改文件会导致解密失败,脚本直接拒绝非法运行:
以Python为例,使用cryptography库实现:

  1. 安装依赖:pip install cryptography
  2. 脚本核心校验代码:
    from cryptography.fernet import Fernet
    import time
    import os
    
    # 密钥可通过Fernet.generate_key()生成,硬编码时注意不要泄露
    KEY = b"your-generated-encryption-key"
    cipher = Fernet(KEY)
    # Linux用系统临时路径,Windows用%APPDATA%下的隐藏文件
    RECORD_FILE = "/var/run/script_last_run.dat"
    
    def can_run():
        if not os.path.exists(RECORD_FILE):
            return True
        try:
            with open(RECORD_FILE, "rb") as f:
                encrypted_data = f.read()
            last_run = int(cipher.decrypt(encrypted_data).decode())
            return time.time() - last_run >= 86400  # 24小时=86400秒
        except:
            # 解密失败(文件被篡改),允许运行并重新记录时间
            return True
    
    def update_last_run():
        with open(RECORD_FILE, "wb") as f:
            encrypted_data = cipher.encrypt(str(int(time.time())).encode())
            f.write(encrypted_data)
    
    if not can_run():
        print(f"脚本需间隔24小时才能再次运行,下次可运行时间:{time.ctime(last_run + 86400)}")
        exit(1)
    
    # 此处编写你的脚本业务逻辑
    print("脚本开始执行...")
    
    # 运行完成后更新记录
    update_last_run()
    

三、隐藏文件+哈希校验(轻量防篡改)

如果不想引入加密依赖,可通过哈希校验实现简单防篡改:

  1. 将上次运行时间写入隐藏文件,同时生成该文件的SHA256哈希值存入另一个文件
  2. 每次启动脚本先校验哈希,不一致则判定文件被篡改,重新记录时间
    示例(Python):
import time
import os
import hashlib

TIME_FILE = ".script_last_run"  # 隐藏文件
HASH_FILE = ".script_last_run.hash"

def get_file_hash(file_path):
    with open(file_path, "rb") as f:
        return hashlib.sha256(f.read()).hexdigest()

def can_run():
    if not os.path.exists(TIME_FILE) or not os.path.exists(HASH_FILE):
        return True
    try:
        with open(HASH_FILE, "r") as f:
            stored_hash = f.read().strip()
        if get_file_hash(TIME_FILE) != stored_hash:
            return True
        with open(TIME_FILE, "r") as f:
            last_run = int(f.read().strip())
        return time.time() - last_run >= 86400
    except:
        return True

def update_records():
    current_time = str(int(time.time()))
    with open(TIME_FILE, "w") as f:
        f.write(current_time)
    file_hash = get_file_hash(TIME_FILE)
    with open(HASH_FILE, "w") as f:
        f.write(file_hash)
    # 设置文件为隐藏:Linux/macOS用chflags,Windows用attrib +h
    os.system(f"chflags hidden {TIME_FILE} {HASH_FILE}")

if not can_run():
    print("脚本需间隔24小时才能再次运行")
    exit(1)

# 脚本业务逻辑...
print("脚本运行中...")
update_records()

内容的提问来源于stack exchange,提问作者Montana black

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 00:15:45