Skip to main content

Cron jobs that don't suck

To run cron jobs with your user env so that it's actually possible for them to run anything, add this to the top of your cron file (edit with crontab -e):

SHELL=/bin/bash
HOME=/home/<username>
BASH_ENV=$HOME/.bashrc

Then look at your ~/.bashrc file and make sure to remove any code (usually at the top) that exits early if found in a non-interactive shell.

Running a TypeScript worker in Node.js

It's really hard to get new Worker() from node:worker_threads to work when running code via tsx. I finally got an example working. Here's how to do it:

import {Worker} from 'node:worker_threads';

const workerPath = import.meta.resolve('./sample-worker.js');
const worker = import.meta.filename.endsWith('.ts')
? new Worker(
`import('tsx/esm/api').then(({ register }) => { register(); import('${workerPath}') })`,
{
eval: true,
},
)
: new Worker(workerPath);

ESM scripts can detect when they're run directly

CJS Node.js scripts have always been able to detect if they're running directly through the CLI or imported from another JS script through various means (the most recent being require.main === module).

However, ESM scripts have not been so lucky. For years the answer to this in ESM has simply been no.

Now it's in Node.js v24! You use it like this:

if (import.meta.main) {
// executed from CLI
} else {
// imported into another JS module
}

Faster Playwright Install on Ubuntu

I noticed recently that running frontend Playwright tests on macOS via GitHub Actions is noticeably faster than on Ubuntu. This is because macOS ships with more browser dependencies out of the box, whereas the base Ubuntu image needs to install them all every run, which takes time. But Playwright has an image with these dependencies already installed! In the end it saves about 30 seconds on Ubuntu.

Recursively convert OGG to MP3

This command will recursively find all OGG files in the currently directory and nested directories and convert them to MP3.

find . -type f -name '*.ogg' -exec bash -c 'for f; do ffmpeg -i "$f" -c:v copy -q:a 2 -map_metadata 0:s:0 "${f%.ogg}.mp3"; done' bash {} +

SoundSource freezing

I can't use macOS anymore without SoundSource. However, it frequently freezes, hangs, crashes, etc. This requires a not-too-lengthy but still annoying manual flow of opening up Activity Monitor to force kill it, then opening it again. So I made it easier and automated.

pm2 Quick Start

I couldn't find a single consolidated place that explains how to setup pm2 from scratch (assuming you've already installed Node.js), and their own "quick start" didn't help. I was finally able to piece together how to get it working.