随机
Enter 搜索 ↑↓ 切换 Esc 清空

lx_timer_list

脚本

Linux Systemd 定时器 美化列表

lx_timer_list

Linux Systemd 定时器 美化列表

一键脚本

bash <(curl -sL gitee.com/meimolihan/cmdbox/raw/master/sh/lx_timer_list.sh)
bash <(curl -sL ../sh/lx_timer_list.sh)

效果预览

执行脚本效果预览

补充说明

该脚本用于美化显示 Linux systemd 定时器列表,基于 systemctl list-timers 命令实现,适合快速查看系统所有定时任务及其触发时间的场景。

功能特点

输出说明

脚本输出包含以下字段:

字段 说明
定时器 Systemd 定时器单元名称
下次触发 下一次计划执行时间
已过时间 距离上次触发的时间
所属服务 定时器触发的服务单元
状态 激活或失效状态

注意事项

脚本源码

#!/bin/bash
set -uo pipefail

list_color_init() {
    export gl_hui=$'\033[38;5;59m'
    export gl_hong=$'\033[38;5;9m'
    export gl_lv=$'\033[38;5;10m'
    export gl_huang=$'\033[38;5;11m'
    export gl_lan=$'\033[38;5;32m'
    export gl_bai=$'\033[38;5;15m'
    export gl_zi=$'\033[38;5;13m'
    export gl_bufan=$'\033[38;5;14m'
    export reset=$'\033[0m'
}
list_color_init

break_end() {
    echo -e "${gl_lv}操作完成${gl_bai}"
    echo -e "${gl_bai}按任意键继续 ${gl_hong}.${gl_huang}.${gl_lv}.${gl_bai}\c"
    read -r -n 1 -s -r -p ""
    echo ""
    clear
}

column_if_available() {
    if command -v column &> /dev/null; then
        column -t -s $'\t'
    else
        cat
    fi
}

list_beautify_linux_timer() {
    {
        printf "%s%s\t%s\t%s\t%s\t%s%s\n" "$gl_hui" "定时器" "下次触发" "已过时间" "所属服务" "状态" "$reset"
        printf "%s%s\t%s\t%s\t%s\t%s%s\n" "$gl_hui" "--------" "--------" "--------" "--------" "--------" "$reset"

        data=$(systemctl list-timers --all 2>/dev/null)
        if [ -z "$data" ]; then
            printf "%s%s\t%s\t%s\t%s\t%s%s\n" "$gl_huang" "(无数据)" "(无数据)" "(无数据)" "(无数据)" "(无数据)" "$reset"
        else
            echo "$data" | tail -n +2 | grep -v "^$" | \
            grep -v "^[0-9]\+ timers listed\|^[0-9]\+ unit\|^LIST\|^$" | \
            awk -v name_color="$gl_bufan" \
                -v next_color="$gl_lv" \
                -v left_color="$gl_huang" \
                -v service_color="$gl_lan" \
                -v status_color="$gl_hui" \
                -v reset="$reset" '
            BEGIN {
                FS="[[:space:]]][[:space:]]*";
                OFS="\t"
            }
            {
                if (NF >= 5) {
                    nxt = $1
                    left = $2
                    serv = $3
                    unit = $4
                    active = $5
                } else if (NF >= 4) {
                    nxt = $1
                    left = $2
                    serv = $3
                    unit = $4
                    active = ""
                } else if (NF >= 3) {
                    nxt = $1
                    left = $2
                    serv = $3
                    unit = ""
                    active = ""
                } else {
                    next
                }

                gsub(/^[[:space:]]+/, "", nxt)
                gsub(/^[[:space:]]+/, "", left)
                gsub(/^[[:space:]]+/, "", serv)
                gsub(/^[[:space:]]+/, "", unit)
                gsub(/^[[:space:]]+/, "", active)

                gsub(/[[:space:]]+$/, "", nxt)
                gsub(/[[:space:]]+$/, "", left)
                gsub(/[[:space:]]+$/, "", serv)
                gsub(/[[:space:]]+$/, "", unit)
                gsub(/[[:space:]]+$/, "", active)

                if (nxt == "" || nxt == "-")
                    nxt = "(已过期)"

                if (active == "active") active = "激活"

                print name_color unit reset,
                      next_color nxt reset,
                      left_color left reset,
                      service_color serv reset,
                      status_color active reset
            }'
        fi
    } | column_if_available
}

list_beautify_all() {
    clear
    echo -e "${gl_zi}>>> Linux Systemd定时器列表${gl_bai}"
    echo -e "${gl_bufan}————————————————————————————————————————————————${gl_bai}"
    list_beautify_linux_timer
    echo -e "${gl_bufan}————————————————————————————————————————————————${gl_bai}"
    break_end
}

list_beautify_all

创建本地脚本

new_script="new_test.sh"

cat > "$new_script" << 'EOF'
#!/bin/bash

# 粘贴脚本源码

EOF

# 保留本地脚本,去掉 rm -f "$new_script"
chmod +x "$new_script" && ./"$new_script" && rm -f "$new_script"

相关命令