pfr Is there a neater/simpler way to get the same output without sed of awk ?

UP=$(uptime)
UP=${UP#*up}
UP=${UP%%,*}
echo $UP
  • pfr replied to this.

    pfr Is there a neater/simpler way to get the same output without sed of awk ?

    $ UP=$(set - $(uptime) ; echo $3 days)
    $ echo $UP
    39 days
    • pfr replied to this.
    • pfr likes this.

      rvp 👍️ This does the trick. The only thing which is strange is, I like to encase each output in [square brackets] however when I do this I get a trailing space after the first bracket like so: [ 14 minutes]. This triggers my OCD slightly 😟 I've just added the bracked to the echo line at the end like this:

      UP=$(uptime)
      UP=${UP#*up}
      UP=${UP%%,*}
      echo [$UP]

      yeti Nice! However, I shutdown my laptop when I'm done using it so no need for "days" 😆

      • yeti replied to this.

        pfr yeti Nice! However, I shutdown my laptop when I'm done using it so no need for "days" 😆

        Without "days" it even is shorter... ;-)
        I only kept it because your original awk/sed dance had it.

        14 days later

        Ok, new query...

        I'd like for my laptop to warn me when my battery is low. Visual notifications are useless and I would rather have an audible beep (which I noticed isn't available on NetBSD) or some other sound once the battery gets below 10%.

        I guess a shell script could do this but would a system service need to be started at boot in order to monitor the battery status?

          pfr Why not run the script from cron every minute or so? And battery level can be checked with envstat I think.

            pfr I would rather have an audible beep (which I noticed isn't available on NetBSD) or some other sound once the battery gets below 10%.

            Call your script which plays a wav file or somesuch in /etc/powerd/scripts/sensor_battery. That script is called with a warning-under event when the SOC reaches 10% and a critical-under event when it reaches 1%. There is also a low-power event there to shutdown the system.

              pfr Is there any way to use a built in hardware beep/bell rather than a .wav file?

              Set device permissions (as root--once only):

              chmod go+rw /dev/speaker

              Then, in your script:

              echo -n CDEF >/dev/speaker

              See spkr(4) for more info.

                rvp
                I assumed I could just add this to the /etc/powerd/scripts/sensor_battery script like so but it didn't work.
                Does it need to be called from a separate script?

                #!/bin/sh -
                #
                #       $NetBSD: sensor_battery,v 1.8 2014/03/13 00:50:55 christos Exp $
                #
                # Generic script for battery sensors.
                #
                # Arguments passed by powerd(8):
                #
                #       script_path device event sensor state_description
                #
                case "${2}" in
                normal)
                        logger -p warning \
                            "${0}: (${3}) capacity reached normal state [${1}]" >&1
                        exit 0
                        ;;
                state-changed)
                        logger -p warning "${0}: (${3}) state changed to ${4} [${1}]" >&1
                        exit 0
                        ;;
                warning-capacity|warning-under)
                        logger -p warning \
                            "${0}: (${3}) capacity below warning limit [${1}]" >&1
                        exit 0
                        ;;
                critical-capacity|critical-under)
                        logger -p warning \
                            "${0}: (${3}) capacity below critical limit [${1}]" >&1
                        echo -n C >/dev/speaker
                        exit 0
                        ;;
                warning-over)
                        logger -p warning \
                            "${0}: (${3}) capacity above warning limit [${1}]" >&1
                        echo -n CD >/dev/speaker
                        exit 0
                        ;;
                        critical-over)
                        logger -p warning \
                            "${0}: (${3}) capacity above critical limit [${1}]" >&1
                        exit 0
                        ;;
                high-capacity)
                        logger -p warning \
                            "${0}: (${3}) capacity above high limit [${1}]" >&1
                        exit 0
                        ;;
                maximum-capacity)
                        logger -p warning \
                            "${0}: (${3}) capacity above maximum limit [${1}]" >&1
                        exit 0
                        ;;
                #
                # This event is _ONLY_ received when all AC Adapters are OFF and all
                # batteries on the system are in CRITICAL or LOW state.
                #
                # It is not recommended to remove the shutdown call.
                #
                low-power)
                        logger -p warning "${0}: LOW POWER! SHUTTING DOWN." >&1
                        /sbin/shutdown -p now "${0}: LOW POWER! SHUTTING DOWN."
                        echo -n CDEF >/dev/speaker
                        exit 0
                        ;;
                *)
                        logger -p warning "${0}: unsupported event ${2} on device ${1}" >&1
                        exit 1
                        ;;
                esac
                • rvp replied to this.

                  pfr I assumed I could just add this to the /etc/powerd/scripts/sensor_battery script like so but it didn't work.

                  1. powerd running? powerd=YES in /etc/rc.conf? (it's enabled by default in /etc/defaults/rc.conf)
                  2. Does the beep work on the command line?

                  The script looks OK, except:

                  1. There is no beep for warning-under (10%)--only critical-under (1%) & warning-over (why this?) beep.
                  2. For the low-power event, the beep should come before shutdown is initiated.

                  pfr Does it need to be called from a separate script?

                  A separate script like /usr/local/bin/alarm.sh (with the event passed to it) would be cleaner.
                  And, I don't understand why you want a pathetic bleep when one can go Dive! Dive! Dive!

                  • pfr replied to this.

                    rvp powerd running? powerd=YES in /etc/rc.conf?

                    Yes.

                    rvp Does the beep work on the command line?

                    Yes

                    rvp The script looks OK, except:
                    There is no beep for warning-under (10%)--only critical-under (1%) & warning-over (why this?) beep.
                    For the low-power event, the beep should come before shutdown is initiated.

                    This is the default script, I only added the calls to /dev/speaker

                    I've changed it to look like this:

                    warning-capacity|warning-under)
                        logger -p warning \
                            "${0}: (${3}) capacity below warning limit [${1}]" >&1
                        echo -n C >/dev/speaker
                        exit 0
                        ;;
                    critical-capacity|critical-under)
                        logger -p warning \
                            "${0}: (${3}) capacity below critical limit [${1}]" >&1
                        echo -n CD >/dev/speaker
                        exit 0
                        ;;

                    &

                    low-power)
                        logger -p warning "${0}: LOW POWER! SHUTTING DOWN." >&1
                        echo -n CDEF >/dev/speaker  
                        /sbin/shutdown -p now "${0}: LOW POWER! SHUTTING DOWN."
                        exit 0
                        ;;

                    I got a beep once rendomly at 4% will keep testing.

                    rvp A separate script like /usr/local/bin/alarm.sh (with the event passed to it) would be cleaner.
                    And, I don't understand why you want a pathetic bleep when one can go Dive! Dive! Dive!

                    How does calling a separate script keep it cleaner? Without it, it's one less script right?

                    And I don't need a fancy alarm, I like to use what tools are built in.

                    • rvp replied to this.

                      The changes now look OK.

                      pfr I got a beep once rendomly at 4% will keep testing.

                      Right. Note that warning-under and critical-under are 10% and 1% of the original design capacity. As the battery ages, and the last full capacity decreases, these percentages will creep upwards. envstat will tell you what the current values are.

                      pfr How does calling a separate script keep it cleaner?

                      Well, you can use the same line in sensor_battery:

                      test -x /usr/local/bin/alarm.sh && /usr/local/bin/alarm.sh ${2}

                      and take different actions based on the event. Then, if you want to take more actions later, you can just edit your script instead of fiddling with the system ones.

                      For instance, let's say you want to sound a klaxon alarm (I'm not saying you should--just hypothetically). Here, you would:

                      $ youtube-dl -f 140 https://www.youtube.com/watch?v=39rw7dgX1wo
                      $ ffmpeg -loglevel error -i Submarine\ Dive\ Alarm-39rw7dgX1wo.m4a -t 8 klaxon.wav

                      and, in the script, you would call:

                      audioplay /some/path/klaxon.wav

                      And, if a klaxon blaring is missed, email the user:

                      mail -s 'Battery critical alert' me <<\EoF
                      Battery is critical. Shutdown system now.
                      Alaaaaarm! Awooga! Awooga! Awooga!
                      EoF

                      Also, pop-up a message:

                      xmessage -fn lucidasans-bold-24 -bg Red -fg Ivory -center \
                      	$'Battery is critical.\nShutdown NOW!'

                      If those are missed, blink the screen:

                      #!/bin/sh
                      N=${1:-3}
                      i=1
                      cmd_off="/opt/intel_backlight/bin/intel_backlight 0"
                      cmd_on="/opt/intel_backlight/bin/intel_backlight 86"
                      while [ $i -le $N ]
                      do	>/dev/null $cmd_off; sleep .75
                      	>/dev/null $cmd_on; sleep .5
                      	i=$((i + 1))
                      done

                      Or, in extreme cases, fill the whole screen red:

                      xterm -fa 'Bitstream Vera Sans Mono:style=Bold' -fs 48 \
                      	-fullscreen -bg Red -fg Ivory -e '
                      tput blink civis
                      printf "Battery critical\nShutdown NOW!!!\n" | centre.awk
                      read x'
                      centre.awk:
                      #!/usr/bin/awk -f
                      {
                      	if (length > MAX)
                      		MAX = length
                      	L[NR] = $0
                      }
                      END {
                      	system("tput clear")
                      	cmd = "stty -f /dev/stderr size"
                      	cmd | getline
                      	close(cmd)
                      	if (($1 + 0 < NR) || ($2 + 0 < MAX))
                      		exit 1
                      	row = ($1 - NR) / 2
                      	col = ($2 - MAX) / 2
                      	for (i = 1; i <= NR; i++) {
                      		cmd = sprintf("tput cup %d %d", row++, col)
                      		system(cmd)
                      		printf("%s", L[i])
                      	}
                      }

                      Endless possibilities...

                        rvp envstat will tell you what the current values are.

                        Ok great, will check that out. Ideally I'd like a warning before I get down to 4%

                        rvp For instance, let's say you want to sound a klaxon alarm (I'm not saying you should--just hypothetically)

                        🤣 You must be a submarine guy? This is certainly very cool, and I may end up doing somehting like this next time I get bored. But for now, a simple beep will suffice.

                        One thing I'm trying to figure out with spkr is how to speed up the tempo of the beeps. I've read the man page but there are no examples so I don't really know what the command should look like.

                        • rvp replied to this.

                          pfr echo 'L32 C D E' >/dev/speaker

                          • pfr replied to this.
                          • pfr likes this.

                            New day, new question. This time it's an error trying to install ueberzug using pip

                            Unfortunately the developer of ueberzug no longer allows for issues to be opened on github, so i'm coming here to see if anyone can help.

                            $ pip install ueberzug 
                            Defaulting to user installation because normal site-packages is not writeable
                            Collecting ueberzug
                              Using cached ueberzug-18.1.9.tar.gz (36 kB)
                            Requirement already satisfied: python-xlib in ./.local/lib/python3.8/site-packages (from ueberzug) (0.29)
                            Requirement already satisfied: pillow in ./.local/lib/python3.8/site-packages (from ueberzug) (8.1.2)
                            Requirement already satisfied: docopt in ./.local/lib/python3.8/site-packages (from ueberzug) (0.6.2)
                            Requirement already satisfied: attrs>=18.2.0 in ./.local/lib/python3.8/site-packages (from ueberzug) (20.3.0)
                            Requirement already satisfied: six>=1.10.0 in ./.local/lib/python3.8/site-packages (from python-xlib->ueberzug) (1.15.0)
                            Using legacy 'setup.py install' for ueberzug, since package 'wheel' is not installed.
                            Installing collected packages: ueberzug
                                Running setup.py install for ueberzug ... error
                                ERROR: Command errored out with exit status 1:
                                 command: /usr/pkg/bin/python3.8 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-772bmp51/ueberzug_b53c21eb3dcb4dedb1ca878cf7509f88/setup.py'"'"'; __file__='"'"'/tmp/pip-install-772bmp51/ueberzug_b53c21eb3dcb4dedb1ca878cf7509f88/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-ntig073p/install-record.txt --single-version-externally-managed --user --prefix= --compile --install-headers /home/dave/.local/include/python3.8/ueberzug
                                     cwd: /tmp/pip-install-772bmp51/ueberzug_b53c21eb3dcb4dedb1ca878cf7509f88/
                                Complete output (51 lines):
                                running install
                                running build
                                running build_py
                                creating build
                                creating build/lib.netbsd-9.1_STABLE-amd64-3.8
                                creating build/lib.netbsd-9.1_STABLE-amd64-3.8/ueberzug
                                copying ueberzug/__init__.py -> build/lib.netbsd-9.1_STABLE-amd64-3.8/ueberzug
                                copying ueberzug/__main__.py -> build/lib.netbsd-9.1_STABLE-amd64-3.8/ueberzug
                                copying ueberzug/action.py -> build/lib.netbsd-9.1_STABLE-amd64-3.8/ueberzug
                                copying ueberzug/batch.py -> build/lib.netbsd-9.1_STABLE-amd64-3.8/ueberzug
                                copying ueberzug/conversion.py -> build/lib.netbsd-9.1_STABLE-amd64-3.8/ueberzug
                                copying ueberzug/files.py -> build/lib.netbsd-9.1_STABLE-amd64-3.8/ueberzug
                                copying ueberzug/geometry.py -> build/lib.netbsd-9.1_STABLE-amd64-3.8/ueberzug
                                copying ueberzug/layer.py -> build/lib.netbsd-9.1_STABLE-amd64-3.8/ueberzug
                                copying ueberzug/library.py -> build/lib.netbsd-9.1_STABLE-amd64-3.8/ueberzug
                                copying ueberzug/loading.py -> build/lib.netbsd-9.1_STABLE-amd64-3.8/ueberzug
                                copying ueberzug/parser.py -> build/lib.netbsd-9.1_STABLE-amd64-3.8/ueberzug
                                copying ueberzug/pattern.py -> build/lib.netbsd-9.1_STABLE-amd64-3.8/ueberzug
                                copying ueberzug/process.py -> build/lib.netbsd-9.1_STABLE-amd64-3.8/ueberzug
                                copying ueberzug/query_windows.py -> build/lib.netbsd-9.1_STABLE-amd64-3.8/ueberzug
                                copying ueberzug/scaling.py -> build/lib.netbsd-9.1_STABLE-amd64-3.8/ueberzug
                                copying ueberzug/terminal.py -> build/lib.netbsd-9.1_STABLE-amd64-3.8/ueberzug
                                copying ueberzug/thread.py -> build/lib.netbsd-9.1_STABLE-amd64-3.8/ueberzug
                                copying ueberzug/tmux_util.py -> build/lib.netbsd-9.1_STABLE-amd64-3.8/ueberzug
                                copying ueberzug/ui.py -> build/lib.netbsd-9.1_STABLE-amd64-3.8/ueberzug
                                copying ueberzug/version.py -> build/lib.netbsd-9.1_STABLE-amd64-3.8/ueberzug
                                copying ueberzug/xutil.py -> build/lib.netbsd-9.1_STABLE-amd64-3.8/ueberzug
                                creating build/lib.netbsd-9.1_STABLE-amd64-3.8/ueberzug/lib
                                copying ueberzug/lib/__init__.py -> build/lib.netbsd-9.1_STABLE-amd64-3.8/ueberzug/lib
                                creating build/lib.netbsd-9.1_STABLE-amd64-3.8/ueberzug/lib/v0
                                copying ueberzug/lib/v0/__init__.py -> build/lib.netbsd-9.1_STABLE-amd64-3.8/ueberzug/lib/v0
                                running egg_info
                                writing ueberzug.egg-info/PKG-INFO
                                writing dependency_links to ueberzug.egg-info/dependency_links.txt
                                writing entry points to ueberzug.egg-info/entry_points.txt
                                writing requirements to ueberzug.egg-info/requires.txt
                                writing top-level names to ueberzug.egg-info/top_level.txt
                                reading manifest file 'ueberzug.egg-info/SOURCES.txt'
                                reading manifest template 'MANIFEST.in'
                                writing manifest file 'ueberzug.egg-info/SOURCES.txt'
                                copying ueberzug/lib/lib.sh -> build/lib.netbsd-9.1_STABLE-amd64-3.8/ueberzug/lib
                                running build_ext
                                building 'Xshm' extension
                                creating build/temp.netbsd-9.1_STABLE-amd64-3.8
                                creating build/temp.netbsd-9.1_STABLE-amd64-3.8/Xshm
                                gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -D_FORTIFY_SOURCE=2 -I/usr/include -I/usr/pkg/include -O2 -D_FORTIFY_SOURCE=2 -I/usr/include -I/usr/pkg/include -O2 -D_FORTIFY_SOURCE=2 -I/usr/include -I/usr/pkg/include -fPIC -I/usr/pkg/include/python3.8 -c Xshm/Xshm.c -o build/temp.netbsd-9.1_STABLE-amd64-3.8/Xshm/Xshm.o
                                Xshm/Xshm.c:5:10: fatal error: X11/Xlib.h: No such file or directory
                                 #include <X11/Xlib.h>
                                          ^~~~~~~~~~~~
                                compilation terminated.
                                error: command 'gcc' failed with exit status 1
                                ----------------------------------------
                            ERROR: Command errored out with exit status 1: /usr/pkg/bin/python3.8 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-772bmp51/ueberzug_b53c21eb3dcb4dedb1ca878cf7509f88/setup.py'"'"'; __file__='"'"'/tmp/pip-install-772bmp51/ueberzug_b53c21eb3dcb4dedb1ca878cf7509f88/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-ntig073p/install-record.txt --single-version-externally-managed --user --prefix= --compile --install-headers /home/dave/.local/include/python3.8/ueberzug Check the logs for full command output.

                            It would appear that Xshm (or lack thereof) may be the issue?

                            EDIT: I could potentially open a new thread for this one, but I just don't to spam the forum with to many questions.

                            • pin replied to this.

                              pfr It would appear that Xshm (or lack thereof) may be the issue?

                              Not really, it can't find libX11 headers. Usually you need to tell the compiler where to find it.

                              • rvp replied to this.
                              • pfr likes this.

                                pin it can't find libX11 headers.

                                Correct.

                                Do: export CPPFLAGS=-I/usr/X11R7/include, then do the (clean) build again. (This is probably not the official python way, but, it should work.)

                                • pfr replied to this.
                                • pfr likes this.