Hammers

:big(I):t is often said that when you wield a hammer, everything begins to look like a nail. Indeed, starting a personal website is one such way to pick up a hammer, and yet (perhaps due to my own ineptitude or general lack of imagination) I am really struggling to find nails.

However, I will force myself to write of anything I find of note. That way, a more educated, more worldly, more cultured, more beloved me can find a useful reference, or perhaps more likely laugh at the Rube Goldberg machine so created to accomplish what may be a very straightforward task. Or this all becomes completely irrelevant due to The Singularity. I hope not.

World’s Smallest Nail

:big(T):he first nail bludgeoned by my oh-so-mighty Mjölnir is a small annoyance that came up again and again when beginning development of this site. This is most likely not an issue for many, because as I understand it, most “people” are completely insane and are fine with twenty or more tabs open at once. Meanwhile I, a patrician, do not like having more than two or three open at any given time. This is similarly the case for running applications. When I am done with an app, I instinctively force quit it.

So, as a Jekyll noob, when initially spinning up my server on localhost:4000 with the absolutely terrible to type bundle exec jekyll serve --livereload, I was frustrated when it locks use of the terminal until the process is killed. Yes, I do use ITerm2 like a functioning human being, but I do not like ⌘T-ing and ⌘W-ing all the time. I want all my work in one place.

I find the ROI of writing a ~10-line bash script to manage processes… unbelievably high. I tend to name them either run or serve depending on the use-case, in this case I went with serve. Perhaps the script has grown by the time I look back on this page in the future, but as I write, it is simply:

1
2
3
4
5
6
# !/bin/bash
pkill -f jekyll
[ "$1" = "stop" ] && { echo "Server stopped"; exit 0; }
[ "$1" = "restart" ] && sleep 1 && $0 && { echo "Server restarted"; exit 0; }
[ -f logs/latest.log ] && mv logs/latest.log logs/$(date -r logs/latest.log +%Y%m%d_%H%M%S).log
bundle exec jekyll serve --livereload > logs/latest.log 2>&1 & echo "Server Started: http://localhost:4000"

After a simple chmod +x serve, I can run ./serve to start the server, ./serve stop to stop the server, or ./serve restart to restart the server, while maintaining full control of my terminal.

Line 5 uses a trick I use often when managing the organization of logfiles. On process start, we make a brand new latest.log while also copying the old latest.log to <timestamp>.log. The timestamp is calculated using the built-in date -r, which retrieves the modification date of the file.