Today I Learned

A Hashrocket project

Ready to join Hashrocket? Find Openings here and apply today.

Kill a Program with pkill

I have a cronjob to open macOS’s Photo Booth every weekday so I can take a picture of my work life. Unfortunately, it opens the program every weekday; I’d rather it quickly closes itself if I’m not there or otherwise occupied. Today I used pkill in the cronjob to terminate the program five minutes after opening:

20 9 * * 1-5  pkill "Photo Booth"

pkill kills a process by name. You can figure out how to make pkill effective using pgrep, a companion program that searches for running processes by name. Using it, I learned that the string “Photo Booth” was specific enough to find and kill Photo Booth:

$ pgrep -l "Photo"
292 Photo Booth

“Photo Booth”, PID 292 (today), is the process I programmatically kill every weekday at 9:20 AM.