Skip to content

2nd April 2022

Computer Science

GNULinux

Adjust external monitor brightness

  • New: Script to adjust external monitor brightness.

    If you want to adjust the external monitor brightness according to the main display, you can run the following script after every brightness change to the main monitor.

    xrandr -q | grep -q "DisplayPort-4 connected" || exit 0
    
    (
      # Wait for lock on /var/lock/adjust_external_monitors_brightness.exclusivelock (fd 200) for 20 seconds
      flock -x -w 20 200 || exit 1
    
      # main screen brightness (from 0 to 255)
      main=$(cat /sys/class/backlight/amdgpu_bl0/brightness)
    
      # external monitor
      l27_max=100 # max brightness
      l27_0=40 # equivalent brightness of main for l27 min brightness
      l27_100=255 # equivalent brightness of main for l27 max brightness
    
      # equivalent brightness for the external monitor
      l27=$(( 100 * ( (main > l27_0 ? main : l27_0) - l27_0 ) / (l27_100 - l27_0) ))
    
      # try to set it until success
      until ddccontrol -r 0x10 -w $l27 dev:/dev/i2c-11 | grep -q "Writing 0x10";
      do
        sleep 1;
      done
    
    ) 200>/var/lock/adjust_external_monitors_brightness.exclusivelock
    

    You will need to make a few adjustments:

    • Change DisplayPort-4 to whatever corresponds to your external monitor.
    • Change /sys/class/backlight/amdgpu_bl0/brightness if you use Intel.
    • Adjust the variables (l27_max, l27_0, l27_100).
    • Get the corresponding i2c device (change i2c-11) by running ddccontrol -p.

Bash

  • New: Get min or max from two variables.

    Use the ternary operator. For example, for MAX(10, $VAR) do:

    $((VAR > 10 ? VAR : 10))
    
  • New: Run a command until the ouptut contains some text.

    until somecommand | grep -q "Up to date";
    do
      sleep 1;
    done
    
  • New: Flcok.

    To ensure only one instance of the script runs some code, you can use flock as follows:

    (
      # Wait for lock on /var/lock/.myscript.exclusivelock (fd 200) for 10 seconds
      flock -x -w 10 200 || exit 1
    
      # Do stuff
    
    ) 200>/var/lock/.myscript.exclusivelock
    

Git

  • New: Push tags.

    To push the local tags to the remote server:

    git push --tags
    

GPG

  • New: Generate a key.

    Use gpg --gen-key or gpg --full-gen-key for more options.

  • New: Export public key.

    gpg --output public.pgp --armor --export username@email
    

PostgreSQL

  • New: Stop remote connections.

    Some operations, like dropping a database, require that the database has no connections. To stop all remote connections and prevent them from connecting again, do:

    ALTER DATABASE somedatabase ALLOW_CONNECTIONS = OFF;
    
    SELECT pg_terminate_backend(pid)
    FROM pg_stat_activity
    WHERE datname = 'somedatabase';
    

    But if you are using PostgreSQL 13 or above, for dropping a table you can directly do:

    DROP DATABASE db_name WITH (FORCE);
    
  • New: List databases.

    \l
    

Programming

pip

  • New: Install.

    Options:

    • --ignore-installed: Ignore the installed packages, overwriting them.

Other

  • Reorganization: Renamed duplicated filenames.