Conky 在卸载卷后抱怨 "No such file or directory"

Conky complains about "No such file or directory" after unmounting a volume

我有一个 bash 脚本来检查我在系统中的可移动设备。 此脚本由 Conky 调用以动态 show/hide 所述设备及其使用和免费 space。

脚本似乎运行良好:

但是,在移除设备后,conky 会抱怨以下消息,直到设备再次插回:

conky: statfs64 '/media/acs/TOSHIBA': No such file or directory

有没有办法让 conky 停止抱怨已移除的设备?


Conky 配置(仅相关部分):

conky.config = {
    alignment = 'top_right',
    background = false,
    border_width = 1,
    cpu_avg_samples = 2,
    default_color = 'white',
    default_outline_color = 'white',
    default_shade_color = 'white',
    double_buffer = true,
    draw_borders = false,
    draw_graph_borders = true,
    draw_outline = false,
    draw_shades = false,
    font = 'DejaVu Sans Mono:size=12',
    gap_x = 10,
    gap_y = 60,
    minimum_height = 5,
    minimum_width = 5,
    net_avg_samples = 2,
    no_buffers = true,
    out_to_console = false,
    out_to_stderr = false,
    extra_newline = false,
    own_window = true,
    own_window_class = 'Conky',
    own_window_transparent = false,
    own_window_type = 'desktop',
    own_window_argb_visual = true,
    own_window_argb_value = 120,
    own_window_hints = 'undecorated,below,sticky,skip_taskbar,skip_pager',
    stippled_borders = 0,
    update_interval = 1.5,
    uppercase = false,
    use_xft = true,
    use_spacer = 'none',
    show_graph_scale = false,
    show_graph_range = false,
    template0 = [[${if_match "${hddtemp }" <= "45.0"}${color green}${hddtemp /dev/sda}º${color}${else}${color red}${hddtemp /dev/sda}º${color}${endif}]],
    template1 = [[${if_match "${execi  sensors | grep 'Core ' | cut -c16-20}" <= "60.0"}${color green}${execi  sensors | grep 'Core ' | cut -c16-21}${color}${else}${color red}${execi  sensors | grep 'Core ' | cut -c15-21}${color}${endif}]],
    template2 = [[${font FontAwesome}DISK${font}${voffset -2}  ${alignr 30} ${fs_used }/${fs_size } (${fs_used_perc }%)]],
    template3 = [[${font FontAwesome}${color orange}MEDIA${color}${font}${voffset -2}  ${alignr 30} ${fs_used }/${fs_size } (${fs_used_perc }%)]]
}

conky.text = [[
    ${color grey}Uptime:${color} $uptime
    ${color grey}Avg. CPU Frequency (GHz):${color} $freq_g
    ${color grey}Avg. CPU Usage:${color} $cpu% ${cpubar 4}
    ${color grey}RAM Usage:${color} $mem/$memmax - $memperc% ${membar 4}
    ${color grey}Swap Usage:${color} $swap/$swapmax - $swapperc% ${swapbar 4}
    ${hr}
    ${font FontAwesome}DISK ${font} /dev/sda (${template0 /dev/sda})
    ${goto 20}${diskiograph_read /dev/sda 30,178 06E9F8 2104FA}${goto 202}${diskiograph_write /dev/sda 30,175 FFFF00 DD3A21}
    ${font FontAwesome}${goto 20}DISK${font} ${diskio_read /dev/sda}${font FontAwesome}${goto 202}DISK${font} ${diskio_write /dev/sda}
    ${hr}
    ${color grey}File systems:${color}
     ${template2 /}
     ${template2 /home}
     ${execpi 1.5 ./conkscript/find-removable-devices.sh}
    $hr
    ${color grey}CPUs:${color}
    ${goto 20}CPU1: ${cpu cpu1}% ${cpubar 7,50 cpu1} - ${freq_g 1} GHz - ${template1 60 0}
    ${goto 20}CPU2: ${cpu cpu2}% ${cpubar 7,50 cpu2} - ${freq_g 1} GHz - ${template1 60 1}
    $hr
    ${color grey}Processes:${color} $processes  ${color grey}Running:${color} $running_processes
    ${color grey}Name              PID   CPU%   MEM%
    ${color lightgrey} ${top name 1} ${top pid 1} ${top cpu 1} ${top mem 1}
    ${color lightgrey} ${top name 2} ${top pid 2} ${top cpu 2} ${top mem 2}
    ${color lightgrey} ${top name 3} ${top pid 3} ${top cpu 3} ${top mem 3}
    ${color lightgrey} ${top name 4} ${top pid 4} ${top cpu 4} ${top mem 4}

]]

脚本:

#!/bin/bash

exec 2>>/tmp/trace.log; PS4=':${BASH_SOURCE}:$LINENO+'; set -x

# Example output:
#  /media/me/whatever
#  /media/me/whatever2
#
lsblkOutput=$(lsblk -J -o MOUNTPOINT | \
              jq -r '.blockdevices[] | select(.mountpoint != null) | .mountpoint | select(startswith("/media"))');

nameToShow=""
deviceMountpoint=""

toConky=""

if [[ "$lsblkOutput" == "" ]]
then
  exit
fi

# To array
IFS=$'\n' devices=($lsblkOutput)
for device in "${devices[@]}"
do
    # Remove the path...
    nameToShow=${device##*/}
    # If the name has several words, only show the first one.
    nameToShow=${nameToShow%\ *}
    # In case the device has whitespaces in the name -> escape them
    device=$(printf "%q" "$device")

    toConky="$toConky${template3 $nameToShow $device}\n"
done

printf '%b' "$toConky"

插入外部驱动器时的脚本跟踪:

::./conkscript/find-removable-devices.sh:10+jq -r '.blockdevices[] | select(.mountpoint != null) | .mountpoint | select(startswith("/media"))'
::./conkscript/find-removable-devices.sh:10+lsblk -J -o MOUNTPOINT
:./conkscript/find-removable-devices.sh:10+lsblkOutput=/media/acs/TOSHIBA
:./conkscript/find-removable-devices.sh:12+nameToShow=
:./conkscript/find-removable-devices.sh:13+deviceMountpoint=
:./conkscript/find-removable-devices.sh:15+toConky=
:./conkscript/find-removable-devices.sh:17+[[ /media/acs/TOSHIBA == '' ]]
:./conkscript/find-removable-devices.sh:23+IFS='
'
:./conkscript/find-removable-devices.sh:23+devices=($lsblkOutput)
:./conkscript/find-removable-devices.sh:24+for device in "${devices[@]}"
:./conkscript/find-removable-devices.sh:27+nameToShow=TOSHIBA
:./conkscript/find-removable-devices.sh:29+nameToShow=TOSHIBA
::./conkscript/find-removable-devices.sh:31+printf %q /media/acs/TOSHIBA
:./conkscript/find-removable-devices.sh:31+device=/media/acs/TOSHIBA
:./conkscript/find-removable-devices.sh:33+toConky='${template3 TOSHIBA /media/acs/TOSHIBA}\n'
:./conkscript/find-removable-devices.sh:36+printf %b '${template3 TOSHIBA /media/acs/TOSHIBA}\n'

移除外部驱动器时的脚本跟踪:

::./conkscript/find-removable-devices.sh:10+jq -r '.blockdevices[] | select(.mountpoint != null) | .mountpoint | select(startswith("/media"))'
::./conkscript/find-removable-devices.sh:10+lsblk -J -o MOUNTPOINT
:./conkscript/find-removable-devices.sh:10+lsblkOutput=
:./conkscript/find-removable-devices.sh:12+nameToShow=
:./conkscript/find-removable-devices.sh:13+deviceMountpoint=
:./conkscript/find-removable-devices.sh:15+toConky=
:./conkscript/find-removable-devices.sh:17+[[ '' == '' ]]
:./conkscript/find-removable-devices.sh:19+exit

There is an open issue 与 Conky 讨论这个问题。

除了使用可用的 fs_* Conky 命令,我们还可以使用脚本来检查大小和使用百分比,并将信息注入 Conky:

for device in "${devices[@]}"
do
    nameToShow=${device##*/}
    nameToShow=${nameToShow%\ *}

    { read _header; read size used pcnt; } < <(df -h --output=size,used,pcent $device)

    toConky+="${template3 $nameToShow $used $size $pcent}\n"
done

printf '%b' "$toConky"

然后,我们只需要修改Conky配置文件中的template3

template3 = [[${font FontAwesome}${color orange}Disk:$color$font${voffset -2}  ${alignr 30} / ()]]

由于 Conky 本身从不查询设备而只是打印文本,因此在卸载卷时它不会抱怨。