How to run a script at start-up on a Raspberry Pi using crontab

Do you need to run a script whenever your Raspberry Pi turns on? Here’s Estefannie to explain how to edit crontab to do exactly that.

Running script on startup

While there are many ways of asking your Raspberry Pi to run a script on start-up, crontab -e is definitely one of the easiest.

AND, as Estefannie explains (in part thanks to me bugging asking her to do so), if you create a run folder on your desktop, you can switch out the Python scripts you want to run at start-up whenever you like and will never have to edit crontab again!

Weeeeee!

Now go write some wonderful and inspiring festive scripts while I take a well-earned nap. I just got off a plane yet here I am, writing blog posts for y’all because I love you THAT DARN MUCH!

A fluffy cat

This is Teddy. Teddy is also in the video.

And don’t forget to like and subscribe for more Estefannie Explains it All goodness!

21 comments

Mikko Tuumanen avatar

I don’t want to watch youtube. Google has enough money and power already. How about writing your raspberry pi tweaking hints directly to the blog?

Joseph Alway avatar

You could just look at the documentation here:
https://www.raspberrypi.org/documentation/linux/usage/cron.md

Milliways avatar

Unfortunately while this explains how to edit a crontab entry, it fails to even mention the major hurdles which cause most cron script to fail!

Little things like environment, dependencies on other startup services.

AndrewS avatar

Github Pull Requests suggesting improvements are always welcome :-)
https://github.com/raspberrypi/documentation

Ted MacDonald avatar

Give me a break. And don’t complain about free advice/videos.

Marek avatar

Hi.
Will it work on model A 256 MB?
Can it run a program instead?

Joseph Alway avatar

Yes, except in some (frequent? / rare?) cases where it won’t work. It takes a command and runs it. Such a command could be starting a program.

Milliways avatar

This has to be the ultimate farce!

A video explaining how to write a text file!

Gryffyn avatar

It explains how to write to a specific system text file, the exact reasons behind the different parts of the command and why it’s more convenient and sometimes better than other options. It’s a short, to the point, video with exactly the information someone needs to do what’s described. Just because it’s simple doesn’t mean it’s obvious, particularly to people new to linux.

Milliways avatar

You completely miss my point!

A set of written instructions on how to edit a file would be simpler, more compact and easier to read. It is also easier to pace the instructions.

Videos may have their place but this is not one of them. Watching someone waving their hands in front of a camera is hardly helpful.

Mark Tomlin avatar

It was only two minutes and to be fair for YouTubers they need the views to make it worthwhile for them to make content.

MrAjAnderson avatar

TL;DR/V

sudo crontab -e

@reboot python home/pi/desktop/startup/*.py &

Dump python script in startup folder on desktop.

It’s worth watching the video to be fair, she delivers well. Reminds me of Nixie Pixel.

Arpi Potcask avatar

Thanks, Mike, for showing how to do it correctly.

There are some EXTENSIONS in some crontab, that are doubtlessly helpful when using them, but not for learning. Use “man 5 crontab” for understanding the regular syntax. “@reboot” is the only exception. Thanks for this.

pcmanbob avatar

Do you really need to run every python program as the root user ?

Would it not be better to use crontab -e and run them as the pi user which would be the more secure option.

Ameyring avatar

I learned how to do this from Matt Richardson and Shawn Wallace’s Make: book Getting Started in Raspberry Pi.

konni avatar

Just write systemd user services. They are way better to handle, come with dependencies and logs.

m avatar

I knew about crontab, but, oddly, didn’t know about creating a startup/run directory.

Thank you Esteffanie and MrAjAnderson. (And thanks also to pcmanbob for the reminder to NOT run everything as root.)

Robert Smith avatar

I run a NAS on an RPi3 and having my firewall start up without me manually initiating it is a godsend. Using crontab never crossed my mind.

Thomas Andersson avatar

sudo apt install gnome-schedule

This will give the response “E: Could’nt find the package.”

evelyn avatar

simple tips, but yeah still helpfull

Comments are closed