5.7.09

Run updates from crontab

Simple safe-upgrade script to be run from root's crontab -

Reasoning - I was tired of the way the updater was in Ubuntu recently, that whole pop-under thing is annoying and counter productive. So I wrote a script that safe-update's the system and doesn't need a sudo user to install the updates.

Now, I would not advise using this on a server or mission critical desktop. Who knows what could happen even with only safe-update called...

Setup is near the bottom, read this first.

What update.sh does:

$now is the current date in unix time
$check_date is the current date minus one week
(actually 6 days 23 hours 59 minutes and 45 seconds)

Fist step is:
It checks to see if this script has been run before via a if file exists in bash

if yes then:
It checks to see if current date minus one week is greater than or equal to the last update. So if this was last run over a week ago it will start.
If not it was last updated withing a week it will exit. This _should_ not happen, but is a built in 'failsafe'.

If the file does not exist:
It creates the .update_log and goes on its merry way updating.

What is doens't do:
This script does NOT reboot the machine when done, this was done on purpose for the user(s). This way when they are done for the day they and shutdown the computer will be up to date the next time it is turned on.

WARNING:
Firefox will act crazy if not restarted after updating! This may be covered in the next revision.

Setup:
1. Copy between the CUTs to /root/update.sh via vim/vi/nano/gedit etc. (Yes you need to be root)
2. chmod +x /root/update.sh
3. su root (if you sudo'd in)
4. #crontab -e
5. Setup the crontab the way you like it
5a. Here's mine 30 16 * * 2 /root/update.sh
6. :wq <-- to save the crontab

You may also want crontab to shutdown or reboot the machine after, but that is up to you.


//----------------CUT--------------------------//
#!/bin/bash
# update.sh - DTM
# Get root's paths (as root 'grep bin /root/*')
# this will give the correct shell also
PATH=/usr/local/sbin:"${PATH}"
PATH=/usr/sbin:"${PATH}"
PATH=/sbin:"${PATH}"

# Check last update time
#-----------------------
# Check if file exists get information on last modify time or just create one

# Define vars using Unix time - makes it easier
now=`date +%s`
check_date=$(($now-604785))

# See if file exists
if [ -f /root/.update_log ];
then
last_update=`cat /root/.update_log`
if [ $check_date -ge $last_update ];
then
echo $now > /root/.update_log && aptitude update && aptitude -y safe-upgrade
else
exit
fi # //END COMPARE LOGS
else
echo $now > /root/.update_log && aptitude update && aptitude -y safe-upgrade
fi # //END MAIN IF
//-------------------------CUT--------------//

No comments: