Automation with shell scripts and scheduling transforms mundane, repetitive IT tasks into reliable, hands-free processes. For the EX200 exam, you must prove you can write a basic shell script and use cron and at to have the system run it automatically, saving yourself from typing the same commands every day.
Jump to a section
A simple way to picture Automation with Shell Scripts and Scheduling
Every morning at 5:30 AM, the alarm clock rings in a bakery. The baker has exactly 42 croissants to prepare. Without automation, he must remember to set the timer, check the oven, and write down the order for flour by hand. This manual process works, but one day he forgets to order flour, and the bakery runs out mid-morning. The loss of sales that day is £300. So the baker creates a system: a written schedule pinned to the wall lists tasks like 'preheat oven at 5:15 AM' and 'order flour every Tuesday at 6:00 PM'. He also uses sticky notes for one-off tasks: 'call the supplier at 3:00 PM tomorrow'. This scheduled, repeatable method means he never forgets, and the bakery runs smoothly. In IT, shell scripts are like those written instructions — a file containing commands to run in order. The cron tool is the wall schedule that runs scripts at set times, daily or weekly. The at tool is the sticky note for a one-time future task. Automation with scripts and scheduling means the computer does the repetitive work without needing a person to remember every step.
This real-life bakery system maps directly to the IT world. Your shell script is the baker's written recipe. The cron daemon (a background program) checks the schedule every minute, just as the baker checks his wall chart. The at command queues a one-off job much like a sticky note. When the baker uses this system, he runs his shop with fewer errors and more time. When a system administrator uses shell scripts and cron, they free themselves from manual, repetitive tasks and ensure critical jobs always happen on time.
Shell scripting is the art of writing a series of commands into a plain text file that the shell (the command-line interpreter) executes in order. Think of it as a recipe: you list all the steps, and when you run the file, the computer does them one after another without you typing each line. The shell you use in Red Hat Enterprise Linux is called Bash (Bourne Again SHell), which is the default. Every shell script begins with a special first line called the shebang: it looks like #!/bin/bash. This tells the system which interpreter to use. Without it, the system might try to run the script using the wrong program. The first step in making a script executable is to set the execute permission using the command chmod +x scriptname.sh. Then you can run it by typing ./scriptname.sh.
Why does this matter? Before shell scripts, if you needed to backup a directory every day, you would have to type the same commands manually: cd /var/log, tar -czf backup.tar.gz logs/, and so on. With a script, you write those commands once into a file, and then run the script whenever you need that backup. This saves time and drastically reduces the chance of a typo or missed step. A simple script might look like this:
#!/bin/bash # This script backs up the /var/log directory tar -czf /backup/logs-$(date +%Y%m%d).tar.gz /var/log echo "Backup complete"
In this script, $(date +%Y%m%d) inserts today's date into the filename, so each backup gets a unique name. The echo command prints a message to the terminal so you know it finished.
Now, even a script still requires a human to run it. Scheduling tools automate that. Two key tools are cron and at. cron is for recurring tasks that need to happen at set times: daily, weekly, on the 15th of every month, and so on. at is for one-off tasks that run at a specific future time (like next Tuesday at 3 PM).
To use cron, you edit your personal cron table (crontab) with the command crontab -e. Each line in the crontab has five time fields (minute, hour, day of month, month, day of week) followed by the command to run. For example, the line '0 2 * * * /home/user/backup.sh' means run the backup script at 2:00 AM every day. The asterisks mean 'every'—so every day, every month, every day of the week. There is also a system-wide cron directory /etc/cron.d/ where system administrators put scripts for all users. To view your current crontab, use crontab -l. To remove it, use crontab -r.
For one-off tasks, at is simpler. You run the command 'at 10:00 PM tomorrow', then type the command you want to run, and press Ctrl+D to finish. The at command uses the atd daemon (background service) to run the job at the specified time. You can see all pending at jobs with the atq command, and remove a job with atrm jobnumber.
Both cron and at rely on daemons — background programs that constantly run and check for scheduled jobs. cron checks every minute, at checks continuously. These daemons must be running for the schedules to work. On RHEL, you can verify they are active with 'systemctl status crond' and 'systemctl status atd'.
Why does this replace manual work? In a real IT environment, a sysadmin might need to:
Rotate log files every night at 3:00 AM
Run a security scan every Sunday at 6:00 AM
Send a disk usage report to management on the 1st day of each month
Reboot a server next Thursday at 4:00 AM for a planned update
Doing all of these by hand would require the administrator to be at work at those exact times, including weekends and holidays. With shell scripts and scheduling, the computer does the work. The admin simply writes the scripts and configures the schedule once.
A crucial point: when a cron job runs, it runs with a very limited environment. It does not source your .bash_profile or .bashrc, so it may not have access to your aliases or PATH variable. This is a common pitfall in the EX200 exam. Always use full paths in cron jobs (e.g., /usr/bin/tar instead of just tar) and redirect output if you want to see error messages. For example, '0 2 * * * /home/user/backup.sh > /tmp/backup.log 2>&1' sends both standard output and standard error to a log file.
Variables in scripts are like named containers for data. To set a variable, write VARIABLE_NAME=value (no spaces around the equals sign). To use it, prefix with a dollar sign: $VARIABLE_NAME. For example, BACKUP_DIR="/backup" and then tar -czf $BACKUP_DIR/logs.tar.gz /var/log. You can also capture the output of a command into a variable using backticks or the $() syntax, as shown earlier with $(date +%Y%m%d).
Conditional statements like if and loops like for allow your script to make decisions and repeat actions. A simple if checks whether a condition is true: if [ -f /etc/passwd ]; then echo "File exists"; fi. A for loop runs through a list: for user in alice bob charlie; do echo "Hello $user"; done. These make your scripts intelligent, handling different situations without manual intervention.
To summarise: shell scripts bundle commands into a reusable file. cron and at execute those scripts on a schedule. Together, they automate repetitive system administration tasks, which is exactly what the EX200 exam expects you to demonstrate.
Write the shell script
Open a text editor (like vim or nano) and type the commands you want to automate. Start with #!/bin/bash on the first line. For example, create a script that backs up a directory. Save the file with a .sh extension, such as backup.sh. This is the core of automation — your reusable recipe.
Make the script executable
Run chmod +x backup.sh to add the execute permission. Without this step, the system will refuse to run the script as a command. You can verify the permission with ls -l backup.sh; you should see 'x' in the permission string.
Test the script manually
Run the script by typing ./backup.sh in the terminal. Check that it works correctly — that files are created, commands run without errors. This step catches mistakes before you schedule it.
Schedule with cron for recurring tasks
Type crontab -e to open your crontab in an editor. Add a line like '0 2 * * * /home/user/backup.sh' to run daily at 2:00 AM. Save and exit. The cron daemon will pick up the change automatically. Use crontab -l to verify the job is listed.
Schedule with at for one-off tasks
If you need a single future run, use at. For example, type 'at 3:00 PM tomorrow', then type the command '/home/user/backup.sh', then press Ctrl+D. Verify with atq. The at daemon will execute it exactly once at the specified time.
Check and troubleshoot scheduled jobs
After scheduling, wait for the time to pass (or use a short test time). Check if the expected output or log file was created. If not, look at the system mail (type 'mail') or check the log for cron errors in /var/log/cron. Ensure crond and atd services are active with systemctl.
In a real business, imagine you work as a junior system administrator for a small company with 100 employees. One of your daily chores is to check disk space on the main file server. If the disk fills up, employees cannot save files, causing lost work and angry calls. Doing this manually every hour is impractical. So you write a shell script called /usr/local/bin/check_disk.sh that checks the disk usage of the /data partition and sends an email alert if it goes above 90%.
You write the script using the df command (disk free) and awk to extract the usage percentage. Then you use the mail command (or a similar tool) to send an email to you. The script might look something like:
#!/bin/bash USAGE=$(df /data | awk 'NR==2 {print $5}' | sed 's/%//') if [ $USAGE -gt 90 ]; then echo "Warning: /data is $USAGE% full" | mail -s "Disk Alert" admin@company.com fi
Now, you cannot sit around waiting to run this script. So you add a cron job using crontab -e. You decide to run the script every hour between 9 AM and 6 PM on weekdays, because that is when employees are most active. The crontab entry looks like this:
0 9-18 * * 1-5 /usr/local/bin/check_disk.sh
This means the script runs at minute 0 of every hour from 9 AM to 6 PM, every day of the week from Monday (1) to Friday (5). Now the system checks automatically.
Next, you need to apply security patches to the server. The maintenance window is next Saturday at 2:00 AM. This is a one-time task, so cron is not the right tool — cron would run it every Saturday. Instead, you use at. You log in on Friday afternoon and type:
at 2:00 AM Saturday at> /usr/bin/yum update -y at> /sbin/shutdown -r now at> <Ctrl+D>
This queues the update and reboot to happen exactly once at the scheduled time. You can verify with atq and see job number 5. If you change your mind, you can remove it with atrm 5.
Another common scenario is rotating log files. Logs can grow huge and fill up a partition. Your company already uses logrotate, but you want to ensure custom logs (like from a web application) are also rotated. You write a script that compresses logs older than 7 days and deletes those older than 30 days, and schedule it with cron to run daily at midnight.
Finally, you need to generate a monthly usage report. You write a script that queries the system for CPU, memory, and disk usage, formats it into an email, and sends it to your manager. You schedule that with cron to run on the 1st day of each month at 8:00 AM:
0 8 1 * * /usr/local/bin/monthly_report.sh
These real-world tasks show that automation is not a luxury — it is a necessity to maintain a reliable system without being on call 24/7. The EX200 exam tests your ability to write the scripts and set the schedules, which directly maps to these everyday responsibilities.
The EX200 exam (Red Hat Certified System Administrator) tests objective 5.2 directly. You will face performance-based tasks (not multiple choice) where you must actually write a shell script and configure cron and at on a live RHEL system. You are given a scenario and you must implement the solution on a virtual machine. The exam is hands-on, so you must be comfortable typing commands and editing files.
Specifically, exam tasks include:
Write a shell script that uses variables, a for loop or an if statement, and passes arguments. For example, a script that takes a username as an argument and checks if that user exists in /etc/passwd.
Set the executable permission on the script using chmod +x and run it with ./scriptname.
Create a cron job for your user that runs a given script at a specific time. You will need to know the crontab syntax precisely: minute (0-59), hour (0-23), day of month (1-31), month (1-12), day of week (0-7, where 0 and 7 are Sunday).
Use the at command to schedule a one-time job. You must know that you type 'at time' then the command, then Ctrl+D. You may need to use 'atq' to list jobs and 'atrm' to remove one.
Show that a cron or at job is scheduled correctly by checking with crontab -l or atq.
Common traps in the exam:
Forgetting the shebang line (#!/bin/bash) at the top of the script. Without it, the system may use the wrong shell or fail to run.
Not making the script executable with chmod +x. If you just run ./script.sh without execute permission, you will get a Permission denied error.
Using relative paths in the script or in cron. Cron runs with a minimal PATH, so always use absolute paths like /usr/bin/tar, not just tar.
Not redirecting output in cron jobs. By default, cron sends output via email to the user, but the exam may expect you to redirect output to a log file to capture results or errors.
Misunderstanding the cron time format. For example, '*/5' means every 5 minutes. '0 0 * * 0' means Sunday at midnight. Know that the day of week field can be 0 or 7 for Sunday, and 1 for Monday, etc.
Using at incorrectly: forgetting to press Ctrl+D after typing the command, or typing the time in the wrong format (use 'HH:MM' or keywords like 'now + 1 hour').
Not checking that the cron daemon (crond) and at daemon (atd) are running. If they are not, jobs will not execute. You can check with 'systemctl status crond' and enable them with 'systemctl enable --now crond'.
Key concepts to memorise:
Shebang: #!/bin/bash
chmod +x script.sh
crontab -e (edit), crontab -l (list), crontab -r (remove)
at time, then command, then Ctrl+D
atq to list, atrm job_number to remove
Standard cron syntax: minute hour day month weekday command
Use absolute paths in scripts and cron
Redirect output: command > /path/to/log 2>&1
Variables in scripts: NAME=value, use with $NAME
System-wide cron directories: /etc/cron.d/, /etc/cron.hourly/, /etc/cron.daily/, etc.
The cron daemon checks every minute, atd daemon runs continuously
The exam uses RHEL 9 or similar; commands are the same across recent versions.
Traps set by exam makers:
They might ask you to schedule a cron job to run at '11:30 PM on the last day of every month'. The trick: cron does not support 'last day' directly. You may need to write a script that checks if tomorrow is the 1st, and schedule the cron job to run daily, but only execute the task on the last day. Or they expect you to use a specific time like '0 23 28-31 * *' and handle the date logic in the script. Know that cron is simple; complex scheduling may need a wrapper script.
They might give you a directory with a script and ask you to 'make it run every 15 minutes'. Be precise: '*/15 * * * *' not '*/15 * * * * *' (which has an extra field and is invalid).
They might test your knowledge of environment by having you debug a cron job that does not work. Check: is the daemon running? Is the script executable? Are paths absolute? Is there any error in the mail? The exam environment lets you check these.
To pass, practise writing scripts and setting up cron jobs on your own RHEL lab. Time yourself. The exam expects you to complete these tasks quickly and accurately.
A shell script must start with the shebang line #!/bin/bash to tell the system which interpreter to use.
You must make a script executable with chmod +x before you can run it with ./scriptname.sh.
In a crontab, the five time fields are minute, hour, day of month, month, and day of week, in that order.
Cron runs with a minimal environment, so always use absolute paths and redirect output in cron jobs.
The at command schedules a single future task, while cron handles recurring schedules.
Use crontab -e to edit your personal crontab, crontab -l to list it, and crontab -r to remove it.
Use atq to view pending at jobs and atrm job_number to remove a specific job.
Always test your script manually before scheduling it to avoid silent failures in cron.
Variables in shell scripts are set with NAME=value and used with $NAME — no spaces around the equals sign.
System-wide cron jobs can be placed in /etc/cron.d/, /etc/cron.hourly/, /etc/cron.daily/, etc.
The cron daemon (crond) must be running for cron jobs to execute; check with systemctl status crond.
If a cron job outputs errors, redirect stderr with 2>&1 to a log file to capture them.
These come up on the exam all the time. Here's how to tell them apart.
cron
Used for recurring tasks (daily, weekly, monthly).
Configured via crontab -e, with five time fields.
Can schedule unlimited repeating jobs.
at
Used for a single one-time task in the future.
Configured interactively by typing 'at time' then the command.
Each job runs exactly once and is then removed.
chmod +x script.sh
Adds execute permission to the file.
Does not change existing read/write permissions for others.
Simplest way to make a script runnable.
chmod 755 script.sh
Sets permissions to rwx for owner, rx for group and others.
Explicitly sets all permission bits (owner read/write/execute, others read/execute).
Useful when you need to set specific permissions, but less commonly used for just making a script executable.
Script with full paths
Uses absolute paths like /usr/bin/tar.
Works reliably in cron where PATH is minimal.
Recommended for all production scripts.
Script with relative paths
Uses relative paths like ./backup.tar.gz.
May fail in cron because current directory is the user's home.
Suitable only for manual interactive use.
#!/bin/bash
Explicitly tells the system to use the Bash interpreter.
Required when running script with ./script.sh.
Allows use of Bash-specific features.
No shebang line
System may default to /bin/sh, which is often a different shell (dash).
May cause syntax errors if script uses Bash-specific commands like [[ ]].
Works only if you run the script as 'bash script.sh' explicitly.
Mistake
I don't need the shebang line (#!/bin/bash) if the script works without it on my machine.
Correct
The shebang line is essential. Without it, the system may use the wrong interpreter (like /bin/sh) or fail to run the script when executed as a command. Always include #!/bin/bash as the first line.
Many beginners run scripts by calling bash script.sh directly, which bypasses the shebang. They then assume it is optional. But the exam tests running scripts with ./script.sh, which relies on the shebang.
Mistake
Cron runs with the same environment as my interactive shell.
Correct
Cron runs with a minimal environment. It does not source your .bash_profile or .bashrc, so aliases and custom PATH variables are not available. Always use full paths in cron commands.
Beginners are used to their shell being preconfigured, so they assume cron behaves the same. They write commands like 'myscript' without a full path, and then wonder why cron fails.
Mistake
If I create a script and schedule it with cron, but forget to make it executable, it will still run at the scheduled time.
Correct
No. The script must have execute permission (chmod +x) for cron to execute it directly. Without it, cron will report 'Permission denied' and the job fails. You must explicitly set the permission.
Beginners often focus on the crontab syntax and overlook the file permissions. They assume any script in their home directory is automatically runnable, which is false.
Mistake
The at command is used for recurring tasks, like checking disk space every day.
Correct
at is for one-time tasks only. For recurring tasks, use cron. at queues a single job that runs once at a specified time and then is removed.
The names 'cron' and 'at' are confusing. Beginners mix up which is for repeating and which is for one-off. The exam explicitly tests that distinction.
Mistake
I can schedule a cron job to run at a specific second, like 'every 30 seconds'.
Correct
Cron cannot run jobs more frequently than every minute. The smallest unit is one minute. To run a job every 30 seconds, you must write a script that loops or use a separate tool like systemd timers.
The cron syntax has no seconds field, so beginners incorrectly add a sixth field, which makes the crontab invalid. They also underestimate cron's granularity.
Mistake
When I use at, I must include the command in quotes on the same line after the time.
Correct
With at, you type the time, press Enter, then type the command(s) on separate lines, and finish with Ctrl+D. You do not put the command in quotes on the at command line.
This is a common syntax error. Beginners treat at like echo or crontab where the command is an argument, but at is interactive.
Reveal each answer, then mark whether you got it right. Score 60%+ to unlock the next chapter.
cron is for tasks that repeat on a schedule (e.g., every day at 2 AM). at is for one-off tasks that run only once at a specific future time. Use cron for recurring, at for single-use.
Run the command chmod +x scriptname.sh. This adds the execute permission. Then you can run it with ./scriptname.sh.
Common reasons: the script is not executable, the shebang line is missing, paths are not absolute, the cron daemon is not running, or the time syntax is wrong. Check by looking at /var/log/cron or the mail command.
Run crontab -l. If you have no crontab, it will say 'no crontab for username'. This lists all your scheduled cron jobs.
Five fields: minute (0-59), hour (0-23), day of month (1-31), month (1-12), day of week (0-7, where 0 and 7 are Sunday). Use asterisks for 'every' and */number for 'every N units'.
Cron runs with a minimal environment, so do not rely on your normal shell variables. Always use absolute paths and set any needed variables within the script itself.
First list pending jobs with atq. You will see a job number (e.g., 5). Then run atrm 5 to remove that specific job.
You've finished Automation with Shell Scripts and Scheduling. Continue through the EX200 study guide to build a complete picture of the exam.
Done with this chapter?