Skip to content

Instantly share code, notes, and snippets.

@jcttrll
jcttrll / timeout.sh
Last active May 31, 2025 18:27
Pure Bash implementation of dual-signal timeout function; unlike /usr/bin/timeout from CoreUtils, command can be another Bash function
# Usage: timeout [ OPTIONS ] CMD [ ARG... ]
#
# Runs CMD and waits for it to terminate, signalling it with a primary signal after a timeout, and optionally signalling
# it with a secondary signal after an additional timeout.
#
# Termination of any subprocesses launched by CMD is not guaranteed: it depends on the signals sent to CMD and the
# behavior of CMD (such as propagating signals to subprocesses). CMD may be made the process group leader, with signals
# correspondingly sent to all processes in the group, with the -g option. Even in this case, a signal causing CMD to
# terminate may not cause a CMD subprocess to terminate, if the two processes handle signals differently. timeout only
# waits for CMD to terminate, not its subprocesses. If CMD terminates before the primary signal timeout, neither
@jcttrll
jcttrll / randomString.sh
Last active May 2, 2025 16:53
Generate a 21-character random string composed of URL-safe base64 (RFC4648) characters, using only /dev/urandom and Bash built-ins. String always starts with a letter and represents 125 bits of randomness (for comparison, version 4 UUID has 122 bits of randomness).
#!/usr/bin/env bash
randomString() {
local LC_ALL=C IFS=
local i byte word string
local -ar alphabet=(
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
a b c d e f g h i j k l m n o p q r s t u v w x y z
0 1 2 3 4 5 6 7 8 9 - _
)
@jcttrll
jcttrll / uuidV4.sh
Last active May 2, 2025 16:52
Generate version 4 UUID using only /dev/urandom and Bash built-ins
#!/usr/bin/env bash
uuidV4() {
local LC_ALL=C IFS=
local byte i
local -a bytes
for ((i = 0; i < 16; i++)); do
read -r -d '' -n1 byte && printf -v byte '%d' "'$byte"
bytes[i]=$byte
@jcttrll
jcttrll / proc.sh
Created February 19, 2024 13:12
Pure Bash CPU/core/thread count from /proc/cpuinfo
#!/bin/bash -eE
main() {
local -A physicalIds coreIds
local count=0
local name _ value
while IFS=$' \t' read -r name _ value; do
case "$name" in
processor) count=$((count + 1)) ;;
@jcttrll
jcttrll / wait-port-open.sh
Last active June 12, 2023 13:39
Attempt to use PID namespace to control process with timeout, while ensuring child cleanup
#!/bin/bash
waitPortOpen() {
local host=$1
local port=$2
local timeoutSeconds=$3
local intervalSeconds=$4
local connectTimeoutSeconds=$5
if [[ -z "$intervalSeconds" ]]; then
@jcttrll
jcttrll / error-handler-snippet.sh
Created February 29, 2020 13:22
Bash error handler, with stacktrace with clickable file:line links (in IntelliJ)
trap onError ERR
onError() {
local exitCode=$? command=(${BASH_COMMAND})
if [[ ${BASH_SUBSHELL} -gt 0 ]]; then
exit ${exitCode}
fi
echo -e "\nERROR: Failed with exit code $exitCode when executing: ${command[@]}" >&2
@jcttrll
jcttrll / WaitSpinLocksExample.java
Created January 3, 2020 02:28
Java wait()/notify() example showing and explaining the use of a spinlock around the wait() call
public class WaitSpinLocksExample {
public static void main(String[] args) {
Thread thread1, thread2;
Base bad = new BadImplementationSubjectToSpuriousWakeup();
thread1 = new Thread(bad::print);
thread2 = new Thread(bad::calculate);
thread1.start();
thread2.start();
@jcttrll
jcttrll / SimpleIoTest.java
Created February 4, 2019 04:04
Quick 'n' dirty write performance test
import java.io.*;
import java.util.*;
import java.security.SecureRandom;
public class SimpleIoTest {
private static final int BLOCK_SIZE = 1024 * 1024;
private static final int TOTAL_SIZE = 1024 * 1024 * 1024;
private static final int SEQUENTIAL_ITERATIONS = 32;
private static final int RANDOM_ITERATIONS = 16;
@jcttrll
jcttrll / power-delete.psm1
Last active March 6, 2022 17:02
PowerShell module to take ownership of a file/directory, assign permissions allowing deletion, and then delete the item (recursively in the case of directories, and optionally including deletion of all hardlinks to files)
function Power-Delete {
[CmdletBinding()]
param(
[parameter(Position = 0, ValueFromRemainingArguments = $true, ValueFromPipelineByPropertyName = $true)]
$FullName,
[switch]
$AllHardLinks
)
process {
@jcttrll
jcttrll / winkey.sh
Created November 10, 2018 15:49
Show Windows product key embedded in ACPI (for use in VMs)
sudo tail -c 29 /sys/firmware/acpi/tables/MSDM && echo