The scheduling is easy. "cron" is the thing that handles that for you.
"cron" is a daemon (system program) that runs user programs according to a schedule that you set up. The schedules are store in a "crontab" for each user (and a special one for the system). You can edit your crontab with "crontab -e".
To run a script at 3am every day you would need to add a line like
Code: Select all
0 3 * * * /home/pi/myscriptname 2>&1 >/home/pi/myscriptoutput
The fields are all described in the crontab manual page "man 5 crontab". I'll summarise them briefly in order.
1: Minute past the Hour to run the command on. As you want to run at 3:00, this is 0.
2: Hour of the Day. Obviously this is 3 for 3am.
3: Day of the month. Putting * means run every day.
4: Month of the Year. Again, * for every month.
5: Day of the Week. Every weekday is *.
Everything after that is the command(s) to run.
I've redirected the output of the command to a file so you can check for errors.
Change "myscriptname" and "myscriptoutput" to names to suit you
Now the script. Just for starters, something like
Code: Select all
# First do apt-get update
/usr/bin/sudo apt-get update
# Then the upgrade
/usr/bin/sudo apt-get -y upgrade
# Finally rpi-update
# Trigger a reboot
/usr/bin/sudo shutdown -r
The "#!/bin/bash -e" Says to use the bash interpreter and stop if any errors occur.
I've put "/usr/bin/sudo" instead of just "sudo" as the PATH used by cron isn't the same as that used at the command line.
The "-y" on apt-get upgrade means to assume "yes" answers to any questions.
Ask if you're not sure of any bits there and don't forget the "man" command is your friend