The full Raspberry Pi Workshop in step-by-step format can be found here http://coreelec.io/piworkshop In this video we investigate how you can utilise Cron to schedule scripts to execute at system startup.

Transcript

In this video we're going to look at how we can run programs as the Raspberry Pi is booting and also how we can schedule programs to run periodically. You might want to run programs at startup if they're programs that you need to run immediately, things like say web servers or if you want a certain application to always open at startup because you've installed your Raspberry Pi in some project and that's that's how the project is driven. This is how we're going to do that.

So to make programs are run at startup, we edit a file called rc.local, now it's a txt file just like this, similar to the scripts we've already been running except because it's a system file we need super user permissions to edit it so we need to run sudo nano and then the path to the file which is /root/etc/rc.local,so this might look a bit familiar we've got a hashbang line it's not quite the same as what we've been using and we've got an if statement and we can see that by default this script does nothing so I mean at the moment it's not really doing nothing it is printing the Raspberry PI's IP address if it has one but this is where we are going to enter our commands that we want to run at startup, we do have to enter them before this exit zero line, otherwise they won't run because the script will exit, but let's say that I wanted to run the pulsing LED script from chapter 2 that breathing LED script that would be a nice power indicator to show us that the Raspberry Pi is running if it's enclosed in a box somewhere.

So to do that we would just run the command to run a Python script that's - I wrote it for Python 3 and rather than figure out the file path I'm just going to go into chapter 2 where it was written right click on it and copy the path its a bit of a trick to save some time, now I can paste that path and you can see that it's pasted the absolute file path which is what we need when we're doing things in files like these. So this would run our script but the the breathing led script had an infinite loop in it and anything that has an infinite loops that we're running at startup we need to end the line with an ampersand symbol after a space, the technicalities of that and not super important, suffice it to say anything that has an infinite loop needs to be run this way otherwise your Pi won't boot it'll get stuck in that infinite loop and it won't boot, so you need to be careful when you're declaring scripts to run from rc.local.

So that would be how we can run our Python script at startup so I'm going to hit CTRL+X to save, and yes and strike enter and now next time the Raspberry Pi rebooted, if it's is connected to an LED correctly that LED will have that breathing effect and that'll be a nice power indicator. So now why don't we schedule our archive script to run periodically, we schedule programs using what's called the crontab which is short for the cron table, and we can access it just by typing crontab and then space with the option - e which is for edit and enter. Now if you haven't run the crontab before you might be prompted with an option of whatever to use I've just chosen Nano, which i think is option number two because that but we're most familiar with and we can see this is this is a pretty pretty serious looking introduction but it has a couple of examples and a bit of a description, and that example and this header telling us the syntax that we need to use the commands that we want to run. So let's say that I want to schedule the time-sensitive archive script to run at let's say midnight every day, so to do that we have minutes, hours, day of month, month, day of week and then command and of course this is how we're going to set up the scheduling so because I want to run it at midnight that is the zeroth minute of the zeroth hour and I want to run it every day, every month, and every day of the week, so for the wild-card symbol we just use the asterisks every day of the month every month and every day of the week and then the command that we use let's say that I'm backing up the the chapter two directory it could of course be anything but the command that I'm going to use is tachi so I'm going to copy that path because remember we have to use absolute file paths and I'm going to paste that so that's the commands that I'm running and then after a space I can enter in the argument which is the directory that I want to copy. So I can copy the the chapter two path and paste that here so now at 0 0 which is 12:00 midnight we're going to run the time set that is archive script and we're going to give it this directory to backup so that's that's one item or one cron job. If you want to queue up some other things you just put it on a new line for instance just as an example what if we wanted to run that sudo apt-get command to keep our our packages up-to-date we could do that let's say every week every month, well let's just take a look.

So we're already doing this at midnight, so maybe I want to run the Apt-get command, how about we do it on every Monday at 3:00 a.m. so that would be the zeroth minute there's 3:00 in the morning of course this is 24 hour time we don't care about the day of the month and we don't care about the month. But we want to run it on Monday which is day number one and I want to run to the apt-get update that will update our package list now that doesn't actually install any upgrades what we can do with Chrome is we can stack and in fact with every shell script even in the terminal we can stack commands up together using two ampersands so that means run this and then run this so we can run update and then we can run sudo apt-get upgrade so if you remember that's how we can keep our packages our software packages up-to-date now you might remember that upgrade after a little while it actually prompts you hey do you actually want to install these packages and because I trust whoever's making these these upgrades I'm just going to put in the option - why and that just means assume yeah don't prompt them into my input just run the upgrade and then and this means that every Monday at 3:00 in the morning I'm going to get brand new list of repositories that can where I can download packages from and then it's going to download those packages. You might also want to reboot your Raspberry Pi periodically I'm not sure why but let's say that we want to do that on the first of the month, so we can say well what we shouldn't do it at midnight because we're doing our archive script so maybe because restarts don't take very long maybe we should do that 11:55 so you can say 55:23 and then the day is the month so this is the one we're interested in we can say on the first of every month and then we put in an asterisk for every month and we don't care what day a week it is and we can just run sudo reboot and that would be how we can do that.

I think that all the examples that I had so you can see that it's quite easy to create complicated behavior just by editing the scheduling syntax and that completes our chapter on shell scripting so now we have that our disposal some tools that can help us automate our Raspberry Pi and have it perform complex commands that we otherwise wouldn't want to enter manually each time I'll see you in Chapter Four.

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.