Surprising Fact: Cron Jobs aren’t in a Shell

GeneralDev.jpg

Today I was surprised to learn that cron jobs are not run in a shell. That was really a surprise. The reason it shocked me was that I had the following in a crontab:

  15 05 * * mon-fri  $HOME/bin/startQL | $HOME/bin/campfire -p

where I'd written a nice little bash script to send a piped message to our Campfire room, and I wanted to pipe the output of the startQL command to that room so we'd be able to see what happened without having to check our email.

The script takes stdin and sends it on as a pass-through, but with a copy being send to Campfire. It's a pretty nice little script that just uses the Campfire curl API:

What I found was that the pipe wasn't working because we weren't in a real shell. It was easy enough to fix - just put the commands in a new file and call that, or use a subshell:

  15 05 * * mon-fri  ($HOME/bin/startQL | $HOME/bin/campfire -p)

Learn something new every day!