Skip to content

Instantly share code, notes, and snippets.

@benjaminblack
benjaminblack / high-resolution-timers-in-node.md
Last active November 16, 2017 23:01
High-resolution timers in Node
function longrunning() {
    const timestamp = process.hrtime(); // returns high-resolution array pair: [seconds, nanoseconds]
    // ...
    // do something
    // ...
    const elapsed = (([sec, ns] = process.hrtime(timestamp)) => ((sec + (ns / 1e9)) * 1e3))(); // converts [seconds, nanoseconds] into milliseconds
}
@benjaminblack
benjaminblack / os-x-add-trusted-certificate-to-system-chain
Created April 4, 2016 01:59
OS X: Add a trusted certificate to the system chain
sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain site.crt
@benjaminblack
benjaminblack / bash-functions-implementing-working-directory-history
Last active November 26, 2015 02:31
Bash functions implementing working directory history
#!/bin/bash
export WDHIST=( $(pwd) )
function cd {
builtin cd $*
if [ $? -eq 0 ]; then
WDHIST[${#WDHIST[*]}]=$(pwd)
fi
}
@benjaminblack
benjaminblack / gist:72b30baa0e5dbc73914f
Created October 10, 2015 23:42 — forked from saetia/gist:1623487
Clean Install – OS X 10.11 El Capitan

OS X Preferences


most of these require logout/restart to take effect

# Enable character repeat on keydown
defaults write -g ApplePressAndHoldEnabled -bool false

# Set a shorter Delay until key repeat
@benjaminblack
benjaminblack / apache-ssi-server-side-includes-matching-variables-with-regular-expressions-using-the-v-function
Last active September 24, 2015 18:01
Apache SSI (Server Side Includes) matching variables with regular expressions using the 'v' function
<!--#if expr="v('REQUEST_URI') =~ m#^/contact#" -->
<!--#set var="pathclass" value="contact" -->
<!--#elif expr="v('REQUEST_URI') =~ m#^/work#" -->
<!--#set var="pathclass" value="work" -->
<!--#elif expr="v('REQUEST_URI') =~ m#^/$#" -->
<!--#set var="pathclass" value="about" -->
<!--#endif -->
<html lang="en-US" class="<!--#echo var="pathclass" -->">
@benjaminblack
benjaminblack / recursively-copy-a-remote-directory-with-rsync
Last active September 22, 2015 14:16
Recursively copy a remote directory with rsync
rsync --checksum --human-readable --archive --verbose --compress --partial --progress --rsh=ssh <host>:<remote-path> <local-path>
* Note: if <path> ends with /, the specified directory is not included in the copy, just its contents (recursively). I.e., to copy a remote "folder" to a local "folder", without copying the folder itself, include a trailing "/" in the remote path.
Options:
--checksum: skip based on checksum, not mod-time & size
--human-readable: output numbers in a human-readable format
--archive: archive mode; same as -rlptgoD (no -H)
-r, --recursive: recurse into directories
-l, --links: copy symlinks as symlinks
@benjaminblack
benjaminblack / counting-unique-404s-in-apache-log-files
Last active September 22, 2015 14:16
Counting unique 404s in Apache log files
Tl;dr:
gunzip --to-stdout logfile-*.gz | grep " 404 " | cut -d " " -f 7 | sort | uniq -c | sort --numeric-sort --reverse > unique-404s.txt
Starting with a bunch of gzipped log files, like "logfile-*.gz".
First, uncompress to stdout:
gunzip --to-stdout logfile-*.gz
@benjaminblack
benjaminblack / installing-debian-with-an-encrypted-boot-partition
Last active March 5, 2021 15:17
Installing Debian with an encrypted boot partition
Create "rescue" partition, do minimal installation, boot (replace ? with root partition device):
[Grub console:]
> set root=(hd0,gptX)
> linux /vmlinuz root=/dev/?
> initrd /initrd.img
> boot
Bring it up to date and install cryptsetup.