crontab notes

May 3, 2024

Just a few quick notes or gotchas that I’ve run into when getting a crontab to run properly. The default shell is sh, and so you should test your commands in sh to see if they run first. You can also apparently set it up to use bash but I haven’t tried that yet. The main gotcha however that I ran into was that apparently % is a special character and it will replace this with a newline unless it’s escaped

So I had a function that is supposed to run randomly with some probability like this

f() { echo "meow" }; (( RANDOM \% 10 == 0 )) && f >> ~/cronlog.txt 2>&1

However this would break if I didn’t escape the % as shown. It would also silently fail by not printing out to the log file, since the % would get replaced by a newline.

Also just as a reminder to myself or anyone, that 2>&1 is what you need to redirect stderr to stdout. I’m thinking of remembering it as “two more and(than) one”. Sorta like how you can remember a she bang based on the exclamation mark being the bang. Anyways hope this is of some help, God bless!


← Back ← Go to all posts