Skip to content

Instantly share code, notes, and snippets.

Avatar

Dr. András Korn akorn

  • Budapest, Hungary
View GitHub Profile
@akorn
akorn / find-interface
Last active May 19, 2020
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.
View find-interface
#!/bin/zsh
#
# Copyright (c) 2019-2020 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.
View list-disk-status
#!/bin/zsh
#
# Copyright (C) 2016 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 Mar 20, 2021
Run speficied (presumably expensive) command with specified arguments and cache result. If cache is fresh enough, don't run command again but return cached output.
View runcached
#!/bin/zsh
#
# Purpose: run speficied 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-2020 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 Oct 15, 2018
Script to back up zfs metadata, also in the form of zfs create commands
View zfs-properties-backup.zsh
#!/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
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.
View browser-suspender
#!/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 Apr 21, 2021 — 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/.`
View rsync_parallel.sh
#!/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>