December 31, 2021

Jar of marbles/Saturdays

Output of a node.js script that estimates how much Saturdays are left in one’s life. It reads: current marble is number 1492 and jar of marbles is 61.74% left.

Jar of marbles

Ever since 2019, one of my new year rituals include estimating the number of Saturdays left in my life. I started this ritual after reading this article about death by Devine Lu Linvega. The point is that the average person lives about 3900 Saturdays in their life, and if we count every Saturday as a marble in a jar of marbles, it can give a better sense of how much limited time we have.

Until last year, I just ran a javascript function in a browser to calculate this since I was lazy to write a proper program. However, since I learned how to automatically schedule tasks using cron recently, I decided to write this into a node.js script and run it every 00:00am of the first day of the year, in my personal laptop.

Code

Below is the node script that I’m using to calculate and store the results as a text file:

marble.js:

// idea from https://wiki.xxiivv.com/#death

// Following John Maxwell's [Jars of Marbles](http://www.johnmaxwell.com/blog/dont-lose-your-marbles), 
// which says that "The average person lives about 75 years, of 52 weeks each, 
// totalling 3,900 Saturdays which the average person has in their lifetime."

const fs = require('fs')

// put your birthday as input
let y = 1993
let m = 4
let d = 26

// calculate current marble
const currentMarble = () => {
    return Math.floor((Date.now()-new Date(y, m, d))/1000/3600/24/7)
}

// calculate percentage of left marbles
const marblesLeft = () => {
    let marbleLeft = (3900-currentMarble())/3900 * 100
    let rounded = Number.parseFloat(marbleLeft).toFixed(2)
    return rounded;
}

// format text
let text = `
    Current marble: #${currentMarble()} [${new Date(Date.now())}]
    Jar of marbles: ${marblesLeft()}% left
`

console.log(text);

// write file as marble.txt
fs.writeFile('./marble.txt', text, 'utf8', function (err) {
    if (err) return console.log(err);
    console.log('marble sucessfully saved!');
});

I also wrote a bash script file so that I can store a log file and include environment variables for crontab (the crontab never worked without this environment variable).

marble.sh:

#!/bin/sh
# add this environment variable in mac os
PATH=/usr/local/bin:/usr/local/sbin:~/bin:/usr/bin:/bin:/usr/sbin:/sbin
node ./marble.js

I also used crontab inside MacOS to make the script to be executed on January 1 00:00AM every year. You can also see crontab.guru to see more information about cron schedule expressions.

crontab -e:

0 0 1 1 * cd ~/marbles-jar && ./marble.sh > ./crontab.log

The code can be also seen in this github repository.

Caveats

After I finished setting up cron, I discovered that cron won’t execute while the computer is asleep and can’t make the computer wake up. This means that my script won’t be executed unless I have my personal laptop turned on at midnight.

Also although it is still being supported, Apple has deprecated cron in MacOS and recommends using launchd instead.

Because of this, I moved this setup from my personal laptop to my DigitalOcean server instead – which is on 24/7 and uses Ubuntu.