Conky在卸载卷后抱怨“没有这样的文件或目录”

时间:2017-10-06 22:47:14

标签: bash conky

我有一个bash脚本来检查系统中的可移动设备。 Conky调用此脚本以动态显示/隐藏所述设备及其已用空间和空闲空间。

脚本似乎工作正常:

  • 插入新设备时,它会显示在Conky中。 Inserted

  • 删除后它会消失。 Removed

但是,在删除设备后,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 \1}" <= "45.0"}${color green}${hddtemp /dev/sda}º${color}${else}${color red}${hddtemp /dev/sda}º${color}${endif}]],
    template1 = [[${if_match "${execi \1 sensors | grep 'Core \2' | cut -c16-20}" <= "60.0"}${color green}${execi \1 sensors | grep 'Core \2' | cut -c16-21}${color}${else}${color red}${execi \1 sensors | grep 'Core \2' | cut -c15-21}${color}${endif}]],
    template2 = [[${font FontAwesome}DISK${font}${voffset -2} \1 ${alignr 30} ${fs_used \1}/${fs_size \1} (${fs_used_perc \1}%)]],
    template3 = [[${font FontAwesome}${color orange}MEDIA${color}${font}${voffset -2} \1 ${alignr 30} ${fs_used \2}/${fs_size \2} (${fs_used_perc \2}%)]]
}

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

1 个答案:

答案 0 :(得分:1)

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

我们可以使用脚本检查使用的大小和百分比,并在Conky中注入信息,而不是使用可用的fs_* 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} \1 ${alignr 30} \2/\3 (\4)]]

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