GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
* Sign Up! *

Support
Customer Portal
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Scheduled Tasks: cron and Automation

bullet Introduction

cron is the Unix scheduler: it runs commands automatically at times you specify — a nightly database backup, a five-minute health check, a weekly report. It is the engine behind several routines elsewhere in this documentation, from regenerating web statistics to updating spam rules. cron works the same way on FreeBSD 15 (where it is part of the base system) and Rocky Linux 10 (where it is provided by the cronie package), and the crontab format described here is identical on both.

 

bullet Editing Your Crontab

Each user has a personal schedule called a crontab. Edit yours with:

$ crontab -e         # edit your crontab in your default editor
$ crontab -l         # list your current entries
$ crontab -r         # remove your crontab entirely (careful)

Every line in the file is one of three things: a comment (anything after a #), an environment-variable setting of the form NAME=VALUE (see below), or a scheduled job — five time fields followed by the command to run:

# minute  hour  day-of-month  month  day-of-week   command
15 2 * * *   /home/youruser/bin/backup.sh

That reads “at 2:15 every day, run the backup script.” The sections below explain what each field may contain, the special characters that build a schedule, and the environment variables that control how jobs run.

 

bullet The Five Time Fields and Their Allowed Values

A job runs whenever the current time matches all five time fields. Their allowed values are:

 Field 
 Allowed values 
 Minute  0–59 
 Hour  0–23 (0 is midnight, 23 is 11 PM) 
 Day of month  1–31 
 Month  1–12, or names jan feb mar apr may jun jul aug sep oct nov dec 
 Day of week  0–7, or names sun mon tue wed thu fri sat (both 0 and 7 mean Sunday) 
 Command  The sixth and final field: the command line to run, with any arguments 

Within any of the five time fields you can use these special forms:

*          every value in the field        (e.g. * in the hour field = every hour)
a-b        a range                          (e.g. 8-11 = 8, 9, 10, 11)
a,b,c      a list                           (e.g. 1,3,5)
*/n        a step over every value          (e.g. */2 in minutes = every 2nd minute)
a-b/n      a step over a range              (e.g. 0-30/10 = 0, 10, 20, 30)

NOTE: When you restrict both day-of-month and day-of-week (neither is *), the job runs when either one matches, not only when both do. For example, 30 4 1,15 * fri runs at 4:30 on the 1st, on the 15th, and on every Friday.

 

bullet Environment Variables: MAILTO, SHELL, and PATH

cron runs your jobs in a deliberately minimal environment. A few NAME=VALUE lines at the top of the crontab control it — they apply to every job listed below them:

SHELL=/bin/sh
PATH=/usr/local/bin:/usr/bin:/bin
MAILTO="you@example.com"

MAILTO decides where a job’s output is mailed. Any output a job produces (anything it prints, plus errors) is emailed automatically — by default to the crontab owner’s local mailbox. Set MAILTO to an address to send it elsewhere, or set it to an empty string to send no mail at all:

MAILTO=""            # discard all job output -- send no mail

(For a single job you can instead redirect its output in the command itself, e.g. >/dev/null to discard normal output while still being emailed about errors, or >> /path/to/log 2>&1 to append everything to a log file.) For mail to reach you at all, mail delivery must be working — see The Mail Server.

SHELL sets which shell cron uses to run commands. It defaults to /bin/sh; set it to /bin/bash (or /usr/local/bin/bash on FreeBSD, where bash is installed from packages) if your commands rely on bash features. PATH is short by default, which is why a command that works when you type it can fail under cron. Either set PATH as above, or give the full path to every command — see the tip below. cron also sets HOME, LOGNAME, and USER for you automatically.

 

bullet Examples

A complete crontab, showing the field syntax and environment variables together:

# Environment for every job below
SHELL=/bin/sh
PATH=/usr/local/bin:/usr/bin:/bin
MAILTO="you@example.com"      # email all output here; "" would discard it

# minute hour day-of-month month day-of-week   command

# 2:15 AM every day -- nightly backup
15 2 * * *            /home/youruser/bin/backup.sh

# 11:40 PM on the 1st of every month -- monthly report
40 23 1 * *           /home/youruser/bin/monthly-report.sh

# Every 10 minutes during the first half-hour of 9 AM and 5 PM, Mon-Fri
0-30/10 9,17 * * 1-5  /home/youruser/bin/poll.sh

# Every 4 hours (midnight, 4, 8, ...) on Sundays in January
0 */4 * jan sun       /home/youruser/bin/archive.sh

# 4:30 AM on the 1st, the 15th, and every Friday
30 4 1,15 * fri       /home/youruser/bin/snapshot.sh

# Midnight on August 19 (numeric or named month -- both lines are equivalent)
0 0 19 8 *            /home/youruser/bin/yearly.sh
0 0 19 aug *          /home/youruser/bin/yearly.sh

# Every 5 minutes -- health check; discard normal output, mail only on error
*/5 * * * *           /usr/local/bin/curl -fsS https://example.com/ >/dev/null

 

bullet System Schedulers

Besides per-user crontabs, each system has a built-in housekeeping scheduler. FreeBSD runs daily, weekly, and monthly periodic jobs (security summaries, log rotation, and the like) via /etc/crontab, configured in /etc/periodic.conf; its output is emailed to root. Rocky Linux drops scripts into /etc/cron.daily/, /etc/cron.weekly/, and /etc/cron.monthly/, and increasingly uses systemd timers for new work — list them with systemctl list-timers. So that the overnight summaries actually reach you, alias root’s mail to your own account (root: youruser in the aliases file, then newaliases).

 

bullet Tips

TIP: cron runs with a minimal environment and a bare PATH, so always give the full path to every command (/usr/local/bin/curl, not just curl) and to every file, or set PATH at the top of the crontab. If a job works by hand but not from cron, a missing path is the usual cause. Test the exact command line first, then paste it into the crontab — and make sure the file ends with a newline, since a final entry without one may be ignored.

 

bullet Documentation

See the crontab(1) and crontab(5) manual pages for the command and the file format, periodic(8) on FreeBSD, and systemd.timer(5) on Rocky Linux.


Toll Free 1-866-GSP-4400 • 1-301-464-9363 • service@gsp.com
Copyright © 1994-2026 GSP Services, Inc.