个人工具

“UbuntuHelp:AutoWeeklyUpdateHowTo”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
第2行: 第2行:
 
{{Languages|UbuntuHelp:AutoWeeklyUpdateHowTo}}
 
{{Languages|UbuntuHelp:AutoWeeklyUpdateHowTo}}
 
== Automatic Package Updates Using cron-apt ==
 
== Automatic Package Updates Using cron-apt ==
 
 
The package cron-apt is designed to automatically update the package list and download upgraded packages. Therefore it basically calls the commands  
 
The package cron-apt is designed to automatically update the package list and download upgraded packages. Therefore it basically calls the commands  
 
<pre><nowiki>
 
<pre><nowiki>
第10行: 第9行:
 
apt-get dist-upgrade -d
 
apt-get dist-upgrade -d
 
</nowiki></pre>.
 
</nowiki></pre>.
 
 
=== Installing Cron-apt ===
 
=== Installing Cron-apt ===
 
You can get cron-apt from the Universe Repositories.  
 
You can get cron-apt from the Universe Repositories.  
 
 
=== Making cron-apt execute regularly ===
 
=== Making cron-apt execute regularly ===
 
 
To make cron-apt be executed regularly, create a link in '''/etc/cron.daily''' or '''/etc/cron.weekly''' with  
 
To make cron-apt be executed regularly, create a link in '''/etc/cron.daily''' or '''/etc/cron.weekly''' with  
 
<pre><nowiki>
 
<pre><nowiki>
 
sudo ln -s /usr/sbin/cron-apt /etc/cron.daily/
 
sudo ln -s /usr/sbin/cron-apt /etc/cron.daily/
 
</nowiki></pre>.  
 
</nowiki></pre>.  
 
 
Cron-apt only downloads new packages, to install them you need to execute  
 
Cron-apt only downloads new packages, to install them you need to execute  
 
<pre><nowiki>
 
<pre><nowiki>
 
sudo apt-get dist-upgrade
 
sudo apt-get dist-upgrade
 
</nowiki></pre> manually.
 
</nowiki></pre> manually.
 
 
* When using cron-apt you do not have to follow the steps below.
 
* When using cron-apt you do not have to follow the steps below.
 
 
== Automatic Weekly Package Updates Using Cron And Apt-Get ==
 
== Automatic Weekly Package Updates Using Cron And Apt-Get ==
 
 
'''WARNING:''' As with any system changes, the potential for creating new or additional problems may occur.  
 
'''WARNING:''' As with any system changes, the potential for creating new or additional problems may occur.  
 
Please be sure to backup your data and configurations! Use this document at your own risk.  
 
Please be sure to backup your data and configurations! Use this document at your own risk.  
 
 
Keeping your Ubuntu (or Debian based) system packages regularly updated not only helps to better secure your system, but also helps keep it running bug free. This howto discusses how to setup a very easy and simple Cron job on your system that will update your system packages weekly, and remove any old unused packages no longer installed after updating.
 
Keeping your Ubuntu (or Debian based) system packages regularly updated not only helps to better secure your system, but also helps keep it running bug free. This howto discusses how to setup a very easy and simple Cron job on your system that will update your system packages weekly, and remove any old unused packages no longer installed after updating.
 
 
(This document assumes an always or mostly always on system with a broadband, not dial-up, Internet connection.)
 
(This document assumes an always or mostly always on system with a broadband, not dial-up, Internet connection.)
 
 
=== Creating the Weekly Cron Job File ===
 
=== Creating the Weekly Cron Job File ===
 
 
First you will need to create the cron job file. You can use a simple text editor to create the file and save it in your home directory. In Ubuntu, open Applications > Accessories > Text Editor. In the Text Editor, type the following lines:
 
First you will need to create the cron job file. You can use a simple text editor to create the file and save it in your home directory. In Ubuntu, open Applications > Accessories > Text Editor. In the Text Editor, type the following lines:
 
 
<pre><nowiki> #!/bin/bash
 
<pre><nowiki> #!/bin/bash
 
apt-get update
 
apt-get update
 
apt-get upgrade -y
 
apt-get upgrade -y
 
apt-get autoclean</nowiki></pre>
 
apt-get autoclean</nowiki></pre>
 
 
Now click Save and name the file something like "autoupdt". The default directory should be your home directory, but verify to be certain. (The steps following this will assume the file has been saved in your home directory.)
 
Now click Save and name the file something like "autoupdt". The default directory should be your home directory, but verify to be certain. (The steps following this will assume the file has been saved in your home directory.)
 
 
=== Moving the Cron Job File to Cron.Weekly ===
 
=== Moving the Cron Job File to Cron.Weekly ===
 
 
Now that you have created the cron job file, it needs to be moved into the weekly cron directory so that it will be run automatically on a weekly basis. To do this, we first need to open a command line terminal. In Ubuntu, click Applications > System Tools > Terminal. Now you should see your terminal prompt. At the prompt type "ls" and press Enter (or Return on some keyboards). In the list you should see your newly created file "autoupdt".
 
Now that you have created the cron job file, it needs to be moved into the weekly cron directory so that it will be run automatically on a weekly basis. To do this, we first need to open a command line terminal. In Ubuntu, click Applications > System Tools > Terminal. Now you should see your terminal prompt. At the prompt type "ls" and press Enter (or Return on some keyboards). In the list you should see your newly created file "autoupdt".
 
 
<pre><nowiki>user@system:~$cd ~
 
<pre><nowiki>user@system:~$cd ~
 
user@system:~$ls</nowiki></pre>
 
user@system:~$ls</nowiki></pre>
 
 
Now that we know the file is there, we need to move the file to the proper directory. Type the following command at the command line promt to move the file:
 
Now that we know the file is there, we need to move the file to the proper directory. Type the following command at the command line promt to move the file:
 
 
<pre><nowiki>user@system:~$sudo mv autoupdt /etc/cron.weekly
 
<pre><nowiki>user@system:~$sudo mv autoupdt /etc/cron.weekly
 
</nowiki></pre>
 
</nowiki></pre>
 
 
(You may be prompted to enter your sudo password, which is YOUR password.) Now we need to move to the Cron directory to verify the file was moved. At the command line prompt type:
 
(You may be prompted to enter your sudo password, which is YOUR password.) Now we need to move to the Cron directory to verify the file was moved. At the command line prompt type:
 
 
<pre><nowiki>user@system:~$cd /etc/cron.weekly
 
<pre><nowiki>user@system:~$cd /etc/cron.weekly
 
user@system:~$ls</nowiki></pre>
 
user@system:~$ls</nowiki></pre>
 
 
You should see the "autoupdt" file in the list. (If not, try the previous move command again.)
 
You should see the "autoupdt" file in the list. (If not, try the previous move command again.)
 
 
=== Making the Cron Job File Executable ===
 
=== Making the Cron Job File Executable ===
 
 
Now that the file is created and ready to be run weekly by cron, we still need to make the file executable in order for cron to be able to run it. Since you are already in the cron.weekly directory, all you need to do is enter this command at the prompt:
 
Now that the file is created and ready to be run weekly by cron, we still need to make the file executable in order for cron to be able to run it. Since you are already in the cron.weekly directory, all you need to do is enter this command at the prompt:
 
 
<pre><nowiki>user@system:~$sudo chmod 755 autoupdt
 
<pre><nowiki>user@system:~$sudo chmod 755 autoupdt
 
</nowiki></pre>
 
</nowiki></pre>
 
 
(Again you may be prompted for your sudo password.)  
 
(Again you may be prompted for your sudo password.)  
 
 
=== Finished ===
 
=== Finished ===
 
 
Now that the file is executable, you are finished. The cron job will run weekly and update your source list (to see if there are any new package updates), update your packages as found and needed, then clean out any old unused no longer installed packages. You can still update using Synaptic Package Manager or apt-get at the command line, but now you can relax a bit knowing your system will be updated automatically on a weekly basis.
 
Now that the file is executable, you are finished. The cron job will run weekly and update your source list (to see if there are any new package updates), update your packages as found and needed, then clean out any old unused no longer installed packages. You can still update using Synaptic Package Manager or apt-get at the command line, but now you can relax a bit knowing your system will be updated automatically on a weekly basis.
 
 
=== Alternatively: Package Updates with Logging using Aptitude ===
 
=== Alternatively: Package Updates with Logging using Aptitude ===
 
 
Apt-get doesn't write details of changes it makes to log files. Aptitude logs to /var/log/aptitude, and can mail a report  to root, which can be redirected to the sudo user if Postfix is set up to do this. As with the examples above, create this script as /etc/cron.weekly/autoupdt and make it executable:
 
Apt-get doesn't write details of changes it makes to log files. Aptitude logs to /var/log/aptitude, and can mail a report  to root, which can be redirected to the sudo user if Postfix is set up to do this. As with the examples above, create this script as /etc/cron.weekly/autoupdt and make it executable:
 
 
<pre><nowiki>#!/bin/bash
 
<pre><nowiki>#!/bin/bash
 
 
tmpfile=$(mktemp)
 
tmpfile=$(mktemp)
 
 
echo "aptitude update" >> ${tmpfile}
 
echo "aptitude update" >> ${tmpfile}
 
aptitude update >> ${tmpfile} 2>&1
 
aptitude update >> ${tmpfile} 2>&1
第96行: 第63行:
 
echo "aptitude clean" >> ${tmpfile}
 
echo "aptitude clean" >> ${tmpfile}
 
aptitude clean >> ${tmpfile} 2>&1
 
aptitude clean >> ${tmpfile} 2>&1
 
 
mail -s "Aptitude cron $(date)" root < ${tmpfile}
 
mail -s "Aptitude cron $(date)" root < ${tmpfile}
 
rm -f ${tmpfile} </nowiki></pre>
 
rm -f ${tmpfile} </nowiki></pre>
 
 
Here is a script (based on the above) that is useful if you don't have sendmail or postfix installed on your computer. It collects all the output from aptitude and manually sends an email using a mail server you specify. You must also specify the recipient of the email. As with the examples above, create this script as /etc/cron.weekly/autoupdt and make it executable:
 
Here is a script (based on the above) that is useful if you don't have sendmail or postfix installed on your computer. It collects all the output from aptitude and manually sends an email using a mail server you specify. You must also specify the recipient of the email. As with the examples above, create this script as /etc/cron.weekly/autoupdt and make it executable:
 
 
<pre><nowiki>
 
<pre><nowiki>
 
#!/bin/bash
 
#!/bin/bash
第108行: 第72行:
 
# changes.
 
# changes.
 
#
 
#
 
 
#
 
#
 
# variables to change
 
# variables to change
 
#
 
#
 
 
# address to send results to
 
# address to send results to
 
MAILTO=your@email.address
 
MAILTO=your@email.address
 
# host name of smtp server
 
# host name of smtp server
 
MAIL=mail.yourisp.com
 
MAIL=mail.yourisp.com
 
 
 
#
 
#
 
# script is below here (do not change)
 
# script is below here (do not change)
 
#
 
#
 
 
tmpfile=$(mktemp)
 
tmpfile=$(mktemp)
 
 
#
 
#
 
# smtp setup commands
 
# smtp setup commands
 
#
 
#
 
 
echo "helo $(hostname)" >> ${tmpfile}
 
echo "helo $(hostname)" >> ${tmpfile}
 
echo "mail from: root@$(hostname)" >> ${tmpfile}
 
echo "mail from: root@$(hostname)" >> ${tmpfile}
第134行: 第91行:
 
echo 'data'>> ${tmpfile}
 
echo 'data'>> ${tmpfile}
 
echo "subject: Aptitude cron $(date)" >> ${tmpfile}
 
echo "subject: Aptitude cron $(date)" >> ${tmpfile}
 
 
#
 
#
 
# actually run aptitude to do the updates, logging its output
 
# actually run aptitude to do the updates, logging its output
 
#
 
#
 
 
echo "aptitude update" >> ${tmpfile}
 
echo "aptitude update" >> ${tmpfile}
 
aptitude update >> ${tmpfile} 2>&1
 
aptitude update >> ${tmpfile} 2>&1
第147行: 第102行:
 
echo "aptitude clean" >> ${tmpfile}
 
echo "aptitude clean" >> ${tmpfile}
 
aptitude clean >> ${tmpfile} 2>&1
 
aptitude clean >> ${tmpfile} 2>&1
 
 
#
 
#
 
# i get a lot of escaped new lines in my output. so the following
 
# i get a lot of escaped new lines in my output. so the following
 
# removes them. this could be greatly improved
 
# removes them. this could be greatly improved
 
 
tmpfile2=$(mktemp)
 
tmpfile2=$(mktemp)
 
cat ${tmpfile} | sed 's/\r\r/\n/g'|sed 's/\r//g' > ${tmpfile2}
 
cat ${tmpfile} | sed 's/\r\r/\n/g'|sed 's/\r//g' > ${tmpfile2}
 
mv ${tmpfile2} ${tmpfile}
 
mv ${tmpfile2} ${tmpfile}
 
 
#
 
#
 
# smtp close commands
 
# smtp close commands
 
#
 
#
 
 
echo >> ${tmpfile}
 
echo >> ${tmpfile}
 
echo '.' >> ${tmpfile}
 
echo '.' >> ${tmpfile}
 
echo 'quit' >> ${tmpfile}
 
echo 'quit' >> ${tmpfile}
 
echo >> ${tmpfile}
 
echo >> ${tmpfile}
 
 
#
 
#
 
# now send the email (and ignore output)
 
# now send the email (and ignore output)
 
#
 
#
 
 
telnet $MAIL 25 < ${tmpfile} > /dev/null 2> /dev/null
 
telnet $MAIL 25 < ${tmpfile} > /dev/null 2> /dev/null
 
 
#
 
#
 
# and remove temp files
 
# and remove temp files
 
#
 
#
 
 
rm -f ${tmpfile}
 
rm -f ${tmpfile}
 
</nowiki></pre>
 
</nowiki></pre>
 
 
----
 
----
  
 
[[category:UbuntuHelp]]
 
[[category:UbuntuHelp]]

2007年11月30日 (五) 16:11的版本


Automatic Package Updates Using cron-apt

The package cron-apt is designed to automatically update the package list and download upgraded packages. Therefore it basically calls the commands

apt-get update
and
apt-get dist-upgrade -d
.

Installing Cron-apt

You can get cron-apt from the Universe Repositories.

Making cron-apt execute regularly

To make cron-apt be executed regularly, create a link in /etc/cron.daily or /etc/cron.weekly with

sudo ln -s /usr/sbin/cron-apt /etc/cron.daily/
.

Cron-apt only downloads new packages, to install them you need to execute

sudo apt-get dist-upgrade
manually.
  • When using cron-apt you do not have to follow the steps below.

Automatic Weekly Package Updates Using Cron And Apt-Get

WARNING: As with any system changes, the potential for creating new or additional problems may occur. Please be sure to backup your data and configurations! Use this document at your own risk. Keeping your Ubuntu (or Debian based) system packages regularly updated not only helps to better secure your system, but also helps keep it running bug free. This howto discusses how to setup a very easy and simple Cron job on your system that will update your system packages weekly, and remove any old unused packages no longer installed after updating. (This document assumes an always or mostly always on system with a broadband, not dial-up, Internet connection.)

Creating the Weekly Cron Job File

First you will need to create the cron job file. You can use a simple text editor to create the file and save it in your home directory. In Ubuntu, open Applications > Accessories > Text Editor. In the Text Editor, type the following lines:

 #!/bin/bash
apt-get update
apt-get upgrade -y
apt-get autoclean

Now click Save and name the file something like "autoupdt". The default directory should be your home directory, but verify to be certain. (The steps following this will assume the file has been saved in your home directory.)

Moving the Cron Job File to Cron.Weekly

Now that you have created the cron job file, it needs to be moved into the weekly cron directory so that it will be run automatically on a weekly basis. To do this, we first need to open a command line terminal. In Ubuntu, click Applications > System Tools > Terminal. Now you should see your terminal prompt. At the prompt type "ls" and press Enter (or Return on some keyboards). In the list you should see your newly created file "autoupdt".

user@system:~$cd ~
user@system:~$ls

Now that we know the file is there, we need to move the file to the proper directory. Type the following command at the command line promt to move the file:

user@system:~$sudo mv autoupdt /etc/cron.weekly

(You may be prompted to enter your sudo password, which is YOUR password.) Now we need to move to the Cron directory to verify the file was moved. At the command line prompt type:

user@system:~$cd /etc/cron.weekly
user@system:~$ls

You should see the "autoupdt" file in the list. (If not, try the previous move command again.)

Making the Cron Job File Executable

Now that the file is created and ready to be run weekly by cron, we still need to make the file executable in order for cron to be able to run it. Since you are already in the cron.weekly directory, all you need to do is enter this command at the prompt:

user@system:~$sudo chmod 755 autoupdt

(Again you may be prompted for your sudo password.)

Finished

Now that the file is executable, you are finished. The cron job will run weekly and update your source list (to see if there are any new package updates), update your packages as found and needed, then clean out any old unused no longer installed packages. You can still update using Synaptic Package Manager or apt-get at the command line, but now you can relax a bit knowing your system will be updated automatically on a weekly basis.

Alternatively: Package Updates with Logging using Aptitude

Apt-get doesn't write details of changes it makes to log files. Aptitude logs to /var/log/aptitude, and can mail a report to root, which can be redirected to the sudo user if Postfix is set up to do this. As with the examples above, create this script as /etc/cron.weekly/autoupdt and make it executable:

#!/bin/bash
tmpfile=$(mktemp)
echo "aptitude update" >> ${tmpfile}
aptitude update >> ${tmpfile} 2>&1
echo "" >> ${tmpfile}
echo "aptitude dist-upgrade" >> ${tmpfile}
aptitude -y dist-upgrade >> ${tmpfile} 2>&1
echo "" >> ${tmpfile}
echo "aptitude clean" >> ${tmpfile}
aptitude clean >> ${tmpfile} 2>&1
mail -s "Aptitude cron $(date)" root < ${tmpfile}
rm -f ${tmpfile} 

Here is a script (based on the above) that is useful if you don't have sendmail or postfix installed on your computer. It collects all the output from aptitude and manually sends an email using a mail server you specify. You must also specify the recipient of the email. As with the examples above, create this script as /etc/cron.weekly/autoupdt and make it executable:

#!/bin/bash
#
# use aptitude to automatically install updates. log and email any
# changes.
#
#
# variables to change
#
# address to send results to
MAILTO=your@email.address
# host name of smtp server
MAIL=mail.yourisp.com
#
# script is below here (do not change)
#
tmpfile=$(mktemp)
#
# smtp setup commands
#
echo "helo $(hostname)" >> ${tmpfile}
echo "mail from: root@$(hostname)" >> ${tmpfile}
echo "rcpt to: $MAILTO" >> ${tmpfile}
echo 'data'>> ${tmpfile}
echo "subject: Aptitude cron $(date)" >> ${tmpfile}
#
# actually run aptitude to do the updates, logging its output
#
echo "aptitude update" >> ${tmpfile}
aptitude update >> ${tmpfile} 2>&1
echo "" >> ${tmpfile}
echo "aptitude dist-upgrade" >> ${tmpfile}
aptitude -y dist-upgrade >> ${tmpfile} 2>&1
echo "" >> ${tmpfile}
echo "aptitude clean" >> ${tmpfile}
aptitude clean >> ${tmpfile} 2>&1
#
# i get a lot of escaped new lines in my output. so the following
# removes them. this could be greatly improved
tmpfile2=$(mktemp)
cat ${tmpfile} | sed 's/\r\r/\n/g'|sed 's/\r//g' > ${tmpfile2}
mv ${tmpfile2} ${tmpfile}
#
# smtp close commands
#
echo >> ${tmpfile}
echo '.' >> ${tmpfile}
echo 'quit' >> ${tmpfile}
echo >> ${tmpfile}
#
# now send the email (and ignore output)
#
telnet $MAIL 25 < ${tmpfile} > /dev/null 2> /dev/null
#
# and remove temp files
#
rm -f ${tmpfile}