top of page

Oracle Linux Basic Administration Series - Part 10 - How to Schedule Tasks with Cron and Systemd Timers in Oracle Linux

  • Jason Beattie
  • 19 hours ago
  • 2 min read

Introduction


Automation saves time and reduces human error. In Oracle Linux, you can schedule repetitive tasks like backups, log cleanup, or updates using cron or systemd timers.


In this blog, you’ll learn how to:

  • Schedule jobs using cron

  • Use at for one-time tasks

  • Create and manage systemd timers (the modern replacement for cron in many cases)


Step 1: Understanding Cron

Cron is a built-in daemon that executes scheduled commands at specified times.Each user (including root) can have their own cron jobs.

The daemon runs automatically on most systems.

Check its status:

sudo systemctl status crond
ree

If inactive:

sudo systemctl enable --now crond

Step 2: Crontab Basics

To edit the current user’s cron jobs:

crontab -e

To view existing jobs:

crontab -l

To remove all jobs:

crontab -r

Step 3: Cron Syntax


Use the following site.


Example – run a script every day at 2 AM


0 2 * * * /usr/local/bin/backup.sh


Example – run every Monday at 8 AM:

0 8 * * 1 /usr/local/bin/report.sh


Example – run every 15 minutes:

*/15 * * * * /usr/local/bin/monitor.sh


ree

Step 4: System-Wide Cron Directories


In addition to user cron jobs, Oracle Linux provides system cron directories:

Directory

Description

/etc/crontab

Main system cron file

/etc/cron.hourly/

Jobs run every hour

/etc/cron.daily/

Jobs run once daily

/etc/cron.weekly/

Jobs run weekly

/etc/cron.monthly/

Jobs run monthly

For example, place a script in /etc/cron.daily/ and make it executable:

sudo cp cleanup.sh /etc/cron.daily/
sudo chmod +x /etc/cron.daily/cleanup.sh
ree

Step 5: Checking Cron Logs

Cron logs are stored in:

/var/log/cron

To view recent entries:

sudo tail -f /var/log/cron
ree


Step 6: One-Time Jobs with at


If you want to schedule a one-time job, use the at command.

Install it (if not present):

sudo dnf install -y at
sudo systemctl enable --now atd
ree
ree

Schedule a job:

echo "/usr/local/bin/backup.sh" | at 02:00

ree

List pending jobs:

atq
ree

Remove a job:

atrm job_number
ree

Step 7: Modern Scheduling with systemd Timers



systemd timers are more powerful and flexible than cron. They integrate with systemctl, logs, and dependency management.



Timers consist of two files:

  1. A service file (.service) — defines what to run

  2. A timer file (.timer) — defines when to run it

📦 Step 8: Creating a systemd Service

Create a service file:

sudo vi /etc/systemd/system/cleanup.service

Add:

[Unit]
Description=Daily cleanup script

[Service]
Type=oneshot
ExecStart=/usr/local/bin/cleanup.sh
ree

Step 9: Create the Corresponding Timer

Now create the timer file:

sudo vi /etc/systemd/system/cleanup.timer

Add:

[Unit]
Description=Run cleanup script daily

[Timer]
OnCalendar=daily
Persistent=true

[Install]
WantedBy=timers.target

Enable and start the timer:

sudo systemctl enable --now cleanup.timer
ree

Check timer status:

systemctl list-timers

You’ll see output like:


ree

Conclusion


You’ve learned how to automate tasks in Oracle Linux using both cron and systemd timers. Automation ensures consistency and reliability key traits of a professional system admin.



In the next post, we’ll secure your server by learning how to configure and secure SSH access.

 
 
 

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
Post: Blog2 Post
  • LinkedIn

©2023 Proudly created with Wix.com

bottom of page