Raspberry Pi - Having a script execute on boot

I was wanting to either be sent an email or at least look at a simple log that included everytime the OS booted (or rebooted). I have a text file in the home directory where I store the output of bash scripts and mono applications that I've scheduled through cron.

I was initialy hoping to use a cron job with the @reboot variable but I quickly found out that this wasn't working on the version of Raspbian I was running for some reason. I was hoping to the cron version would work as it would be a nice non-blocking way to have something run when the OS boots (by non blocking I mean that the entire OS wouldn't freeze up until it was done).

I ended up editing the /etc/rc.local file which did successfully run. In the stock file you'll want to put your bash code above the line that exits with an exit code of 0. You will also want to remember to end your entry with "&" so it runs in the background and doesn't block the rest of the system from booting (if you do this wrong it's my understanding that you'll never get to the login prompt because the system will halt on your process, I didn't have this issue but you should be careful of that). If that would be an issue you can probably mount your SD card on another computer and edit the /etc/rc.local file to remove your changes.

For reference, I added a single line that echoed what time the OS started and went ahead and dumped it into the log I use for output from cron jobs (even though I didn't technically start it from cron). One less place to look.

Bash (Excerpt)

bash -c "echo -e [$(date '+%d/%m/%Y %H:%M:%S')] [/etc/rc.local] OS Started'\n' >> /home/pi/cron-log.txt" &

Leave a comment

Please note that we won't show your email to others, or use it for sending unwanted emails. We will only use it to render your Gravatar image and to validate you as a real person.