Scheduled Tasks with Cron
Automate recurring server tasks by creating and managing cron jobs, ensuring scripts and commands run at specified intervals.
Scheduled Tasks with Cron is a free Linux Server Deployment & SSH Mastery lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Linux Server Deployment & SSH Mastery learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Automate Server Tasks?
Manual tasks on a server can be repetitive and prone to human error. Automation helps you save time and ensures tasks are performed consistently.
Imagine needing to back up a database every night or clean up old log files every week. Doing this manually is tedious and easy to forget.
This is where scheduling tools come in handy! They allow your server to perform tasks automatically at specific times or intervals, freeing you up for more important work.
Meet Cron: Your Server's Scheduler
Cron is a fundamental utility in Linux for scheduling tasks. It's like a personal assistant for your server, ensuring that commands or scripts run automatically in the background at predetermined times.
These scheduled tasks are called cron jobs. Each user on a system can have their own list of cron jobs, stored in a special file called a crontab.
Editing Your Crontab
To manage your personal cron jobs, you use the crontab command.
- To edit your crontab file, type:
crontab -e - To list your current cron jobs, type:
crontab -l - To remove all your cron jobs, type:
crontab -r(Use with caution!)
When you run crontab -e, it will open your crontab in your default text editor, often nano or vi.
Crontab Syntax: The Time Fields
Each line in your crontab represents a single cron job and follows a specific pattern. It has five time fields, followed by the command to execute.
The format is:
minute hour day_of_month month day_of_week command- Minute: 0-59
- Hour: 0-23 (0 is midnight)
- Day of Month: 1-31
- Month: 1-12 (or Jan-Dec)
- Day of Week: 0-7 (0 or 7 is Sunday, 1 is Monday)
Crontab Syntax: Asterisks & Ranges
You don't always need exact numbers. Cron offers special characters for flexibility:
*(Asterisk): Matches "every" possible value for that field. E.g.,*in the minute field means "every minute".-(Hyphen): Specifies a range. E.g.,9-17in the hour field means "hours 9 through 17".,(Comma): Specifies a list of values. E.g.,Mon,Wed,Frior1,3,5for days of the week./(Slash): Specifies step values. E.g.,*/5in the minute field means "every 5 minutes".
Crontab Syntax - Practical Examples
Let's look at some common examples to solidify your understanding:
* * * * * /path/to/script.sh
Runs every minute.0 3 * * * /usr/bin/backup.sh
Runs daily at 3:00 AM.0 9-17 * * 1-5 /usr/local/bin/check_service.py
Runs every hour from 9 AM to 5 PM, Monday to Friday.*/15 * * * * echo "Hello, CoddyKit!"
Runs every 15 minutes.
Remember to replace /path/to/script.sh with your actual command or script path.
Special Crontab Strings
For very common scheduling needs, Cron provides special strings that are easier to read than the five-field syntax:
@reboot: Run once at startup.@hourly: Run once an hour, at the beginning of the hour.@daily: Run once a day, at midnight.@weekly: Run once a week, at midnight on Sunday.@monthly: Run once a month, at midnight on the first day of the month.@yearly(or@annually): Run once a year, at midnight on January 1st.
These are great for simple, recurring tasks.
Environment Variables & Paths
When cron runs a job, it uses a very minimal set of environment variables. This often means commands that work in your terminal might fail in cron.
To avoid issues:
- Always use absolute paths for commands and scripts (e.g.,
/usr/bin/phpinstead ofphp). - You can define a
PATHvariable right inside your crontab file, e.g.,PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin - Alternatively, set environment variables directly within your script.
Handling Output and Logging
By default, cron attempts to email any output (stdout and stderr) from a cron job to the user who owns the crontab. If you don't have an email system configured, this output might be lost or fill up log files.
It's best practice to explicitly redirect output:
- To discard all output:
command > /dev/null 2>&1 - To append output to a log file:
command >> /var/log/myjob.log 2>&1
The 2>&1 part redirects standard error (2) to the same location as standard output (1).
System-wide Cron vs. User Crontabs
We've focused on personal user crontabs, managed with crontab -e. These are great for individual user tasks.
For system-wide tasks, administrators often use:
/etc/crontab: The main system crontab, allowing tasks to run as specific users./etc/cron.d/: Directory for separate cron files, often installed by applications./etc/cron.hourly/,/etc/cron.daily/, etc.: Directories where scripts are placed to be run at those specific intervals by system cron.
These system-level options offer more control for server-wide automation.
Test Your Cron Knowledge
You've learned the basics of crontab syntax. Let's see if you can decipher this cron job.
Recap: Automate with Cron
You've now mastered the essentials of scheduling tasks with Cron!
Here's what we covered:
- What is Cron: A time-based job scheduler.
- Crontab: Your personal file for defining cron jobs.
- Syntax: Understanding the 5 time fields and special characters (
*,-,,,/). - Special Strings: Shortcuts like
@hourlyand@reboot. - Best Practices: Using absolute paths and handling output.
With Cron, you can automate mundane tasks, ensuring your server runs smoothly and efficiently!
Frequently asked questions
Is the “Scheduled Tasks with Cron” lesson free?
Yes — the full text of “Scheduled Tasks with Cron” is free to read here on the web, and the Linux Server Deployment & SSH Mastery course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Linux Server Deployment & SSH Mastery course, upgrade to CoddyKit PRO.
What will I learn in “Scheduled Tasks with Cron”?
Automate recurring server tasks by creating and managing cron jobs, ensuring scripts and commands run at specified intervals. You practise Linux Server Deployment & SSH Mastery with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Linux Server Deployment & SSH Mastery?
No prior experience is required. Linux Server Deployment & SSH Mastery on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Scheduled Tasks with Cron” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Linux Server Deployment & SSH Mastery lesson?
Yes. Every Linux Server Deployment & SSH Mastery lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Managing Users and Groups
- Monitoring System Processes
- Scheduled Tasks with Cron
- Managing System Services with systemd