Friday, May 2, 2014

Scheduled updates with cron job

If you want to install updates on a server you may want it done at night when users are sleeping (hopefully).

That is not that difficult with a shell script and a cron job. The shell script takes care of calling all the update commands and the cron job schedules the executing and log the output to a custom log file.

First the shell script:

echo "\nUpdate on: $(date)\n"
echo 'Update the packages list\n'
sudo /usr/bin/apt-get update >> /dev/null 2>&1
echo '\nUpgrade to latest package version\n'
sudo /usr/bin/apt-get upgrade --yes
echo '\nUpgrade the distribution\n'
sudo apt-get dist-upgrade --yes
echo '\nCleanup\n'
sudo /usr/bin/apt-get autoremove --yes
sudo /usr/bin/apt-get autoclean --yes
sudo /usr/bin/apt-get clean --yes
# The End

The script executes after all a bit of cleanup.

Put the "" script in a folder and make it executable:

$ chmod +x

Now the cron job rule:

open crontab as root:

$ sudo crontab -e

Add a line to call the "" script:

30 4 3 5 *  /home/administrator/ > /home/administrator/myupdates.log

In the line above I call the "" in my administrator folder at 04:30 on May the 3rd. The output of the script will be sent to "myupdates.log" file.

After the job is executed you can check the log file and comment out the line in crontab with a #

I hope it is useful to you.

No comments:

Post a Comment