パッケージの更新に合わせて再起動する

サーバーのパッケージを最新の状態にするため、パッケージ更新&再起動スクリプトを作成しました。

DNF パッケージが存在する場合に、自動的に再起動するスクリプト

「/usr/local/bin/dnfAutoReboot.py」

!/usr/bin/env python

import subprocess
import os
import sys

# DNF でチェックを行い更新があれば更新後に再起動する。
if __name__ == '__main__':
    # スーパーユーザーかどうか?
    if os.getuid() != 0:
        print('This program is only for super user.', file=sys.stderr)
        exit(1)

    # 更新チェック
    dnf_args = ['dnf', 'check-update']
    return_code = subprocess.run(dnf_args).returncode
    if return_code == 0:
        # 更新なし
        exit(0)
    elif return_code != 100:
        # エラー
        print('subprocess error : ' + str(dnf_args) + 'return code is ' + str(return_code), file=sys.stderr)
        exit(return_code)

    # 更新あり
    dnf_args = ['dnf', 'update', '-y', '--refresh', '--best', '--allowerasing']
    return_code = subprocess.run(dnf_args).returncode
    if return_code != 0:
        # エラー
        print('subprocess error : ' + str(dnf_args) + 'return code is ' + str(return_code), file=sys.stderr)
        exit(return_code)

    # システムの再起動
    reboot_args = ['systemctl', 'reboot']
    return_code = subprocess.run(reboot_args).returncode
    exit(return_code)

「/etc/crontab」毎日の深夜1時に実行

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
0  1  *  *  * root dnfAutoReboot.py

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です