by Larry Kraemer 10-23-2013 08:00
Since moving to a rural town, I've been wanting to download updated Linux ISO's between the hours of 00:02 thru 04:58 so the transfered data isn't metered, and
subtracted from my account's 10GB limit. I started searching for an easy way to download specific ISO's from a known URL. I also needed a way to Update & Upgrade
Debian 7 a couple of times a month. I needed to do these transactions between 00:02 and 04:58, and I didn't want to be up during those hours banging the keys on
multiple keyboards.
Finding a way to download the Updated Linux ISO's was easy. I downloaded uGet from the Debian Repo's, and configured it to download the ISO's between
01:00 thru 01:59. That worked GREAT!
The second thing I needed was a way to execute the following commands to Update and Upgrade my current Debian 7 install.
Code: Select all
sudo apt-get update
sudo apt-get upgrade
enable crontab. But, trying to run the command as sudo required my password, and caused the crontab script to abort. I searched until I found a way to bypass entering
my password. The following command is about Line 23 of /etc/sudoers:
The following stanza was added to /etc/sudoers, immediately after the above stanza (about Line 23):# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL
Code: Select all
# set up mycron_debian.sh to execute cronjob as root for apt-get update, etc.
larry ALL=(ALL) NOPASSWD: /home/larry/mycron_debian.sh
mycron_debian.sh contains:-rwxr--r-- 1 root root 114 Oct 23 07:33 mycron_debian.sh
Code: Select all
#dump the Date & Time to file as a marker
zdump CST
sudo apt-get update
sudo apt-get --assume-yes upgrade
#this was needed for distro's such as Linux Mint 15 to update lower Icon on toolbar
sudo apt-get update
Code: Select all
# Create your script with one of these commands:
# crontab /home/larry/mycronfile.txt
# crontab -u larry /home/larry/mycronfile.txt
# edit with crontab -e
#
# use /bin/bash to run commands, instead of the default /bin/sh
SHELL=/bin/bash
MAILTO=""
#MAILTO="someuser"
#
# set the PATH variable to make sure all commands in the crontab are found
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
#
# actual update commands are located in mycron_debian.sh
#
# zdump CST
# sudo apt-get update
# sudo apt-get --assume-yes upgrade
# sudo apt-get update
#
# Send the output of the commands to a log file (as user) /home/larry/crontab/cronrun
#
# Run twice around 1st & 15th of every month at 01:00 on first Sunday and third Sunday, and log to a file at /home/larry/crontab/cronrun
#
00 01 * * 0#1,0#3 sudo /home/larry/mycron_debian.sh >> /home/larry/crontab/cronrun
#
#
# Gotchas!
# 1. When cron job is run from the users crontab it is executed as that user. It does not however source any files in the users home directory
# like their .cshrc or .bashrc or any other file. If you need cron to source (read) any file that your script will need you should do it
# from the script cron is calling. Setting paths, sourcing files, setting environment variables, etc.
# 2. If the users account has a crontab but no useable shell in /etc/passwd then the cronjob will not run. You will have to give the account a
# shell for the crontab to run.
# 3. If your cronjobs are not running check if the cron deamon is running. Then remember to check /etc/cron.allow and /etc/cron.deny files.
# If they exist then the user you want to be able to run jobs must be in /etc/cron.allow. You also might want to check if the
# /etc/security/access.conf file exists. You might need to add your user there.
# 4. Crontab is not parsed for environmental substitutions. You can not use things like $PATH, $HOME, or ~/sbin. You can set things like
# MAILTO= or PATH= and other environment variables the /bin/sh shell uses.
# 5. Cron does not deal with seconds so you can't have cronjob's going off in any time period dealing with seconds. Like a cronjob going off
# every 30 seconds.
# 6. You can not use % in the command area. They will need to be escaped and if used with a command like the date command you can put it in
# backticks. Ex. `date +\%Y-\%m-\%d`
Code: Select all
crontab /home/larry/mycronfile.txt
Code: Select all
crontab -e
Code: Select all
crontab -r
You can use mailx -H to view the mail headers. man mailx will give more command information.
Larry