home January 01, 2017

Cron and At


cron and atd turtorial, discussion and examples

Cron is a deamon called "crond" used to schedule and execute jobs or scripts automatically without user intervention. Cron, also referred to as crontab, can help with automated log rotation, scheduled reporting and running of scripts at off times of the day. Cron is primarily used for jobs needing to be executed over and over like log rotation every week or a report email sent out every morning.

An addtitional tool one can use is called "at" and is used to execute a job only once. "at" is very useful, for example if you want run a backup job starting at 8pm and you expect to be leaving at 5:30pm.

In OpenBSD and FreeBSD the daemon cron can handel cron jobs as well at "at" jobs. In Linux the "crond" daemon is used for cron jobs only and a separate daemon "atd" is used for at jobs. Make sure the correct daemon is running for the job scheduler you are looking to use.

Cron Tutorial

To use cron tab there are two important commands:

  crontab -e    edit your crontab entries
  crontab -l    print the entries from crontab

Here is an example of a very easy to reference header for your crontab. You have the descriptions for every time slot and what every slot will accept. This example also specifies the shell and the path making sure the binaries you run can be found. The last line is an example of running "newsyslog" Sunday at midnight. You are welcome to cut/paste this block to the top of your cron tab.

SHELL=/bin/sh
PATH=/bin:/sbin:/usr/bin:/usr/sbin
HOME=/var/log
#
#minute (0-59)
#|   hour (0-23)
#|   |    day of the month (1-31)
#|   |    |   month of the year (1-12 or Jan-Dec)
#|   |    |   |   day of the week (0-6 with 0=Sun or Sun-Sat)
#|   |    |   |   |   commands
#|   |    |   |   |   |
#### rotate logs weekly (Sunday at midnight)
00   0    *   *   0   /usr/bin/newsyslog

Lets take a look at some examples in order of simple to alittle more complex. Notice all of the binaries are using their absolute paths. Cron uses its own PATH variable and it is a safe practice to always use absolute paths in your crontab. This is to avoid confusion.

Rotate logs weekly at 12midnight. (just like the example above)

00   0    *   *   0   /usr/bin/newsyslog

Rotate logs weekly at 12midnight. (instead of 0 for the day of the week we can use Sun for Sunday)

00   0    *   *   Sun /usr/bin/newsyslog

Mail a report to root everyday at 11:59pm (23:59).

59   23  *    *   *   /usr/local/bin/pflogsumm -d today /var/log/maillog | mail -s "mail report" root

Run the backup scripts at 5am on the 3rd (Wed) and 5th (Fri) day of the week. Send any errors to /dev/null

00   5   *    *   3,5 /tools/BACKUP_script.sh >> /dev/null 2>&1

Compress backup files at 6am on the 1st and 15th of the month.

00   6   1,15 *   *   /tools/BACKUP_compress.sh

Refresh the Squid ad blocker server list every 3 days at 12:05am.

05   0    *   *   */3 /tools/ad_servers_newlist.sh

Clear the blocked hosts list at 3:23pm (15:23) every Monday only on even numbered months.

23   15   *   */2 1   /tools/clear_blocked_hosts.sh

Run a script at 8:45pm (20:45) on 2nd and the 16th only in the months of January and April.

45   20   2,16 1,4 *   /tools/a_script.sh

Run a script every day at 8:45pm (20:45) and add a random sleep time between 0 and 300 seconds.

45   20   *   *    *   sleep $(($RANDOM \% 300)); /tools/a_script.sh

Run the script at 23:59 (11:59pm) on the last day of the month.

59   23 28-31 *    *   [ $(date -d +1day +%d) -eq 1 ] && /tools/a_script.sh

REMEMBER: On Linux systems you need to run the daemon "atd" for the "at" jobs to run and "crond" for the "cron" jobs to run. On OpenBSD or FreeBSD machines the "crond" daemon will handle both "cron" and "at" jobs. If you entered jobs into their tabs and they do not run, make sure the daemons are started. Also check that the daemons are started at boot.

At "how to"

To use "at" you need to know the structure and how to complete the command.

  at 5am Oct 20   at "time am/pm" "month" "day"
  atq             lists  the  user's  pending jobs
  atrm            deletes jobs, identified by their job number
  Ctrl-d          once done editing use Ctrl-d to close the "at" entry shell

To run jobs only once it is easier to use "at" than to setup and cron job and then go back and remove it once the job has ran. Remember you need to have the "atd" daemon running on Linux systems to run "at" jobs. On OpenBSD or FreeBSD system the "crond" daemon will handle "cron" and "at" jobs.

To run an "at" job you need to fist tell "at" what time to run the job. Remember to use absolute paths to avoid confusion. Once to execute att with the time and date you will be put into an "at" shell. This is where you will enter the commands you want to execute, one command per line to make it simple.

In this example we will be executing a set of commands at 5am on January 23rd. The backup script will run and then we will send out mail to root. To close the "at" shell and save the job you must type Ctrl-d (the control key with the lowercase d).

user@machine:~$ at 5am Jan 23
at>  /tools/run_backups.sh
at>  echo "job done" | mail -s "backup job finished" root
at>  Ctrl-d
job 1 at 2008-01-23 05:00

Once you have completed entering your commands and type Ctrl-d "at" will respond with the job number and a verification printout of when the job is going to run. If you made a mistake and ran the job at the wrong time you can usr "atrm" to remove the job and re-enter your job with the current time.