..., and just read that number every 10 seconds.
Ah yeah thanks, see, I'm missing something obvious:(
anyway @flower if you wish you could still use that second function(_ifupd) if you have use for it;)
it counts lines after you check for 'mtime'. So in this case you would only see how many updates there are once every 60 min and the line count before checkupdates if you want to see it's output every 10s.
Exactly, that's the point. Checkupdates doesn't need to be run every 10 seconds. It runs every 60 minutes, and the number of available updates are stored in the temp file.
There is no reason to store the list to the temp file every 60 minutes and count the number of lines every 10 seconds; that count couldn't possibly change. Instead get the list and count the lines every 60 minutes, and just read that number every 10 seconds.
]]>_upd() {
[ -f '/tmp/info_updates' ] && awk 'END{print NR}' /tmp/info_updates
[ -z "$(find /tmp -maxdepth 1 -name info_updates -mmin -60)" ] \
&& checkupdates >| /tmp/info_updates
}
# if an update of interest, put '+' sign after number of updates(former function)
_ifupd() {
[ -f '/tmp/info_updates' ] && grep -Ew 'apache|bash|linux|mariadb|php|systemd' \
/tmp/info_updates && echo +
}
Tested on tmux statusbar
edit: add check
]]>#replace these lines
printf "MEMTOTAL=\"`free -h | awk '(NR==2){print $2}'`\"\n\
MEMUSED=\"`free -h | awk '(NR==2){print $3}'`\"\n\
SWAPTOTAL=\"`free -h | awk '(NR==3){print $2}'`\"\n\
SWAPUSED=\"`free -h | awk '(NR==3){print $3}'`\"\n" > /tmp/info_mem
# with this instead
free -h | awk '
NR==2 { printf "MEMTOTAL=\"%s\"\nMEMUSED=\"%s\"\n", $2, $3; }
NR==3 { printf "SWAPTOTAL=\"%s\"\nSWAPUSED=\"%s\"\n", $2, $3; }
' >/tmp/info_mem
Also, your loop times are quite odd. Why is the outermost process looping every second to regenerate /tmp/info when all of the content used to create /tmp/info is regenerated at much longer intervals? Specifically, the shortest interval of any component is 5 seconds, yet the /tmp/info is regerated 5 times in that time: once with new data, then 4 times for no reason with stale data.
EDIT: that entire script can be replaced by the following:
Don't bother looping in the script and creating a file that you cat from yet another loop. Just run this from the host:
${execpi 10 ssh root@{HOSTNAME} -C /path/to/this_script}
Then "this_script" on the VM:
#!/bin/sh
# if /tmp/info_updates is absent or older than 60 minutes, (re)create it
[ -z "$(find /tmp -maxdepth 1 -name info_updates -mtime -60)" ] && checkupdates | wc -l >| /tmp/info_updates
cat /etc/hostname
if [ -f /tmp/info_updates ]; then
read n < /tmp/info_updates
printf '$alignr${color green}%d Updates$color\n' $n
fi
printf '$hr\n'
free -h | awk '
NR==2 { printf "RAM ${color grey}%s / %s$color ${goto 275} ", $3, $2; }
NR==3 { printf "SWAP ${color grey}%s / %s$color\n", $3, $2; }
'
printf 'CPU ${color grey}%d%% / %d Threads$color ${goto 275} ' $(awk '// { print $2 * 100; }' /proc/loadavg) $(nproc)
df -h / | awk 'NR==2 { printf "HDD ${color grey}%s / %s$color\n", $3, $2; }'
i made a small bash script which monitors my vms. maybe it's useful for you.
vms are expected to run arch and have pacman-contrib installed (this can be changed easily though)
if you place a health.sh script in /root the exit code is evaluated
conkyrc on host:
${execpi 10 ssh root@{HOSTNAME} -C cat /tmp/info}
/etc/systemd/system/sysinfo.service on vm
[Unit]
Description=conkysysinfo
[Service]
ExecStart=/root/info.sh
[Install]
WantedBy=multi-user.target
/root/info.sh on vm
#!/bin/bash
OK="\${color green}"
FAIL="\${color red}"
trap "kill 0" SIGINT
NAME=`cat /etc/hostname`
echo "HEALTH=" > /tmp/info_health
if [ -f /root/health.sh ]; then
(
while :
do
HEALTH=`/root/health.sh && echo \\\${color green} || echo \\\${color red}`
echo "HEALTH=\"\\$HEALTH\"" > /tmp/info_health
sleep 300
done
) &
fi
echo "UPDATES=\"0\"" > /tmp/info_updates
(
while :
do
UPDATES=`checkupdates | wc -l`
echo "UPDATES=\"$UPDATES\"" > /tmp/info_updates
sleep 3600
done
) &
MEMTOTAL="0B"
MEMUSED="0B"
SWAPTOTAL=0B""
SWAPUSED="0B"
echo "" > /tmp/info_mem
(
while :
do
printf "MEMTOTAL=\"`free -h | awk '(NR==2){print $2}'`\"\n\
MEMUSED=\"`free -h | awk '(NR==2){print $3}'`\"\n\
SWAPTOTAL=\"`free -h | awk '(NR==3){print $2}'`\"\n\
SWAPUSED=\"`free -h | awk '(NR==3){print $3}'`\"\n" > /tmp/info_mem
sleep 10
done
) &
CPUCOUNT="0"
CPUPERCENT="0"
echo "" > /tmp/info_cpu
(
while :
do
echo -e "CPUCOUNT=\"`cat /proc/cpuinfo | grep processor | wc -l`\"\nCPUPERCENT=\"`top -bn10 | awk 'FNR == 3 { print $2+$4; }'`\"\n" > /tmp/info_cpu
sleep 5
done
) &
HDDSIZE="0B"
HDDUSED="0B"
echo "" > /tmp/info_hdd
(
while :
do
echo -e "HDDSIZE=\"`df -h | grep vda3 | awk '{print $2}'`\"\nHDDUSED=\"`df -h | grep vda3 | awk '{print $3}'`\"\n" > /tmp/info_hdd
sleep 300
done
) &
(
while :
do
(
. /tmp/info_health
. /tmp/info_updates
. /tmp/info_mem
. /tmp/info_cpu
. /tmp/info_hdd
echo -n $HEALTH$NAME\$color
if [ "$UPDATES" != "0" ]; then
echo -n "\$alignr$OK$UPDATES Updates\$color "
fi
echo
echo "\$hr"
echo "RAM \${color grey}$MEMUSED / $MEMTOTAL\$color \${goto 275} SWAP \${color grey}$SWAPUSED / $SWAPTOTAL \$color"
echo "CPU \${color grey}$CPUPERCENT% / $CPUCOUNT Threads\$color \${goto 275} HDD \${color grey}$HDDUSED / ${HDDSIZE} \$color"
) > /tmp/info
sleep 1
done
) &
wait
result:
https://i.imgur.com/1hkScWV.png
moderator edit -- replaced oversized image with link.
Pasting pictures and code