Skip to content

Instantly share code, notes, and snippets.

View akorn's full-sized avatar

Dr. András Korn akorn

  • Budapest, Hungary
View GitHub Profile
@akorn
akorn / find-interface
Last active February 25, 2022 20:04
A script to be called from `pre-up` in `interfaces(5)`: it examines a set of configured candidate interfaces to find the one that's plugged into the network you want, then renames it to a descriptive name you've chosen. Use case: you have many similar physical interfaces and don't want to keep track of which cable you plug into which.
#!/bin/zsh
#
# Copyright (c) 2019-2022 András Korn. License: GPLv3
#
# Take a set of network interfaces and determine which one is plugged into a specific network. Then rename the interface.
#
# This script supplies some generic functions for that purpose; the actual logic comes from configuration (you need to override the detect() function and perhaps the last_resort() function).
#
# The script is concurrency capable. It processes candidate interfaces simultaneously, obtaining a lock for each; other instances skip locked interfaces and add them to the end of their queue.
# Several instances can run concurrently with overlapping candidate POOLs.
@akorn
akorn / list-disk-status
Last active February 25, 2022 20:20
Query the status of all local SATA/SAS hard drives and summarize in a table; also for disks on hpsa controllers
#!/bin/zsh
#
# Copyright (C) 2016, 2022 András Korn. Licensed under the GPL v3.
#
# Purpose: query the status of all local SATA hard drives and summarize in a table.
#
# Not complete, but good enough for my own purposes. Patches welcome.
typeset -A diskattrs
typeset -A diskargs
@akorn
akorn / runcached
Last active April 8, 2024 10:02
Run specified (presumably expensive) command with specified arguments and cache result. If cache is fresh enough, don't run command again but return cached output.
#!/bin/zsh
#
# Purpose: run specified command with specified arguments and cache result. If cache is fresh enough, don't run command again but return cached output.
# Also cache exit status and stderr.
# Copyright (c) 2019-2023 András Korn; License: GPLv3
# Use silly long variable names to avoid clashing with whatever the invoked program might use
RUNCACHED_MAX_AGE=${RUNCACHED_MAX_AGE:-300}
RUNCACHED_IGNORE_ENV=${RUNCACHED_IGNORE_ENV:-0}
RUNCACHED_IGNORE_PWD=${RUNCACHED_IGNORE_PWD:-0}
@akorn
akorn / zfs-properties-backup.zsh
Created October 15, 2018 10:46
Script to back up zfs metadata, also in the form of zfs create commands
#!/bin/zsh
zfs get all -Hp -s local,received >/var/backups/zfs-properties.txt
zpool get all >/var/backups/zpool-properties.txt
for i in $(zfs list -t filesystem,volume -H -o name); do
unset props
zfs get all -o property,value -Hp -s local,received $i | while read prop value; do
props=($props[@] "-o $prop"="'$value'")
done
echo zfs create ${(o)props[@]} $i
done >/var/backups/zfs-create-commands.txt
@akorn
akorn / browser-suspender
Last active May 19, 2020 13:19
A zsh script to automatically suspend browser instances that don't have focus, and unsuspend them when they receive focus. They get to run for a bit every few minutes even if they don't have focus so they can update/display notifications etc.
#!/bin/zsh
#
# Idea from http://log.or.cz/?p=356
#
# Copyright (c) 2017-2020 András Korn. License: GPLv3
#
# Run this from a user-level runsvdir (runit), or from an endless loop, or using systemd, set to restart.
#
# Make sure you start browsers in their own process group (e.g. `chpst -P browser`). (TODO: maybe support cgroups?)
@akorn
akorn / rsync_parallel.sh
Last active January 26, 2024 08:18 — forked from rcoup/rsync_parallel.sh
This script can transfer large directory structures with parallel rsync workers. Example command line: `rsync_parallel . -- -aHSAX --exclude '*13' . /tmp/2/.`
#!/bin/zsh
#
# Copyright (c) 2014, 2020 by Dr. András Korn. Implements the basic idea of a similar script by Robert Coup (2013).
# License: GPLv3
function usage() {
echo 'Usage:
rsync_parallel [--parallel=N] <args to find(1) to generate list of stuff to transfer> -- <args to rsync>