Information Technology Grimoire

Version .0.0.1

IT Notes from various projects because I forget, and hopefully they help you too.

Examples of Using Crontab

Sample Uses of Cron

If you understand the Basics of Crontab and are ready to create useful tasks, you might wonder what is possible. These are simple 1 liners that you can automate. Here are a few examples of useful crontab tasks:

# basic ping check
ping -q -c1 -w3 site.com 2&>1 /dev/null || echo site.com ping failed | mail -ne -s'Server unavailable - site.com' admin@site.com

# report on current IP
ifconfig eth0 | awk '/inet / {print $2}' | mail -s "Current IP for Site.com" admin@site.com

# send info to syslog for processing
/usr/local/script.sh 2>&1 |/usr/bin/logger -t script_result

# rsync backup (requires keys setup)
rsync -rau /sourcedir X.X.X.X:/backup/destdir

# backup local file
gzip -c ~/.bash_history > ~/.backup/history-save-$(date +\%d-\%m-\%y-\%T).gz

# system backup
for file in $(find /var/backup -name "backup*" -type f |sort -r | tail -n +10); do rm -f $file; done ; tar czf /var/backup/backup-system-$(date "+\%Y\%m\%d\%H\%M-\%N").tgz --exclude /home/dummy /etc /home /opt 2>&- && echo "system backup ok"

# database backup
/usr/bin/mysqldump -uroot -p'<pass>' dbname > /backups/$(date +\%Y-\%m-\%d_\%Hh\%M).sql

# restart a process that died
ps -C thisdaemon || { thisdaemon & }

# notify of website change
HTMLTEXT=$( curl -s http://site.com > /tmp/new.html ; diff /tmp/new.html /tmp/old.html ); if [ "x$HTMLTEXT" != x ] ; then echo $HTMLTEXT | mail -s "Page has changed." admin@site.com ; fi ; mv /tmp/new.html /tmp/old.html
Last updated on 20 Jul 2018
Published on 20 Jul 2018
 Edit on GitHub