如何设置centos cleanup自动运行

在 CentOS 系统中,你可以使用 systemd 来创建一个定时任务,以便自动运行 centos-cleanup 工具。以下是具体步骤:

  1. 安装 centos-cleanup 工具(如果尚未安装):

    sudo yum install -y centos-release-scl
    sudo yum install -y cleanup-tools
    
  2. 创建一个 systemd 服务文件
    创建一个新的服务文件,例如 /etc/systemd/system/centos-cleanup.service,并添加以下内容:

    [Unit]
    Description=CentOS Cleanup Service
    After=network.target
    
    [Service]
    Type=oneshot
    ExecStart=/usr/bin/centos-cleanup --enablerepo=updates --nocleanall
    RemainAfterExit=yes
    
    [Install]
    WantedBy=multi-user.target
    

    这个服务文件定义了一个名为 centos-cleanup.service 的服务,它会在系统启动后运行 centos-cleanup 命令,并启用 updates 仓库,但不清理所有缓存。

  3. 创建一个 systemd 定时器文件
    创建一个新的定时器文件,例如 /etc/systemd/system/centos-cleanup.timer,并添加以下内容:

    [Unit]
    Description=Run CentOS Cleanup Service periodically
    
    [Timer]
    OnBootSec=5min
    OnUnitActiveSec=1week
    Persistent=true
    
    [Install]
    WantedBy=timers.target
    

    这个定时器文件定义了一个名为 centos-cleanup.timer 的定时器,它会在系统启动后 5 分钟首次运行,然后每周运行一次。

  4. 启用并启动定时器
    使用以下命令启用并启动定时器:

    sudo systemctl enable centos-cleanup.timer
    sudo systemctl start centos-cleanup.timer
    
  5. 检查定时器状态
    你可以使用以下命令检查定时器的状态:

    sudo systemctl list-timers --all
    

    或者查看特定定时器的状态:

    sudo systemctl status centos-cleanup.timer
    

通过以上步骤,你已经成功创建了一个自动运行的 centos-cleanup 定时任务。你可以根据需要调整 OnBootSecOnUnitActiveSec 的值来改变定时任务的触发频率。

Both comments and pings are currently closed.

Comments are closed.

Powered by KingAbc | 粤ICP备16106647号-2 | Loading Time‌ 0.284