chay.dev

While waiting for running processes to end

Getting notifications away from your desk

dev

Context switching while waiting for long processes to end can be costly. Things I like to do when I run terraform apply on my monorepo - which can sometimes take up to 30 minutes to apply fully, especially when doing stuff like creating elasticsearch clusters - include doing some exercises like push-ups, taking a walk to refill my bottle, or work on a separate task, etc. things that take my attention off the running process.

Getting notifications #

I used a silly method to notify myself of the tasks completion, which basically involves sending a curl command to my pushover account:

# ~/.zshrc

function sendpush() {
curl --request POST \
--url https://api.pushover.net/1/messages.json \
--header 'Content-Type: application/json' \
--data '{
"token": "<my_api_token>",
"user": "<my_user_token>",
"message": "done!"
}'

}
# using it
./long_process.sh ; sendpush

All's good, but I can't rely on this after 11.45pm because that's when my iPhone's Bedtime kicks in and I stop receiving notifications. Good thing is I have some smart lights in my room hooked up to Home Assistant, which can be controlled via some APIs.

# ~/.zshrc

function toggle_light {
curl --request POST \
--url http://192.168.0.99:8123/api/services/light/toggle \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{
"entity_id": "light.yeelight_stripe_<id>"
}'

}

Watching processes #

To get such "notifications" on an existing running process, it is a simple matter of watching the process PID and running the action once it's done.

# list all processes, get the pid
ps -a

# wait on the process
while kill -0 <pid> 2> /dev/null; do sleep 3; done; toggle_light

To make it fancier, fzf can be used to make the process of selecting the pid more enjoyable!

# ~/.zshrc

function wa() {
pid=$(ps -a | fzf | cut -d " " -f1)
while kill -0 $pid 2> /dev/null; do sleep 3; done; $1
}