Introduction
Cron is a time-based job scheduler in Unix-like operating systems, designed to execute scripts or commands at specified times. However, it lacks the ability to run tasks more frequently than once per minute, which can be limiting when you need finer granularity, such as every 30 seconds.
This tutorial explores alternatives and solutions for executing tasks with sub-minute precision on Linux systems like Ubuntu. We’ll delve into methods using shell scripting, alternative schedulers, and system services that provide this functionality.
Understanding Cron Limitations
Cron expressions are structured in five fields: minute, hour, day of month, month, and day of week. Each field has a limited range, with the "minute" field only allowing values from 0 to 59. This means cron cannot natively support execution intervals shorter than one minute.
For tasks requiring more frequent execution, such as every 30 seconds, we must look beyond cron or employ creative solutions within its constraints.
Solutions for Sub-Minute Task Scheduling
1. Using Shell Scripts with Background Processes
A practical approach involves using shell scripts to manage timing manually. By leveraging background processes and the sleep
command, you can simulate sub-minute intervals:
#!/bin/bash
# Start on a minute boundary
while [[ "$(date +%S)" != "00" ]]; do true; done
while true; do
# Start a timer in the background
sleep 30 &
# Execute your task
echo "Executing task at $(date)"
# Simulate variable task duration
delay=$((RANDOM % 20 + 1))
echo "Task took ${delay} seconds"
# Wait for the background process to complete before starting the next cycle
wait
done
This script runs a task every 30 seconds by starting a sleep
timer in the background. The main loop waits for this timer, ensuring consistent intervals regardless of task duration.
2. Using Alternative Schedulers
a) fcron
fcron
is an advanced cron-like scheduler that supports more frequent job execution and finer control over scheduling:
- Installation: Typically installed via package managers (
sudo apt-get install fcron
). - Configuration: Jobs can be scheduled every second or sub-second intervals in the configuration file, usually found at
/etc/fcron.d/
.
b) systemd
For systems using systemd (most modern Linux distributions), you can create a timer unit to manage task execution:
-
Create a service file (
/etc/systemd/system/myservice.service
):[Unit] Description=My Frequent Task [Service] Type=oneshot ExecStart=/path/to/task.sh
-
Create a timer unit file (
/etc/systemd/system/myservice.timer
):[Unit] Description=Runs every 30 seconds [Timer] OnBootSec=5s OnUnitActiveSec=30s [Install] WantedBy=timers.target
-
Enable and start the timer:
sudo systemctl enable myservice.timer sudo systemctl start myservice.timer
systemd
provides precise control over task execution, leveraging built-in mechanisms for better reliability and integration.
3. Monitoring and Recovery
Regardless of the method chosen, it’s important to ensure your script or service is resilient:
- Monitoring: Implement checks within your script or use external tools (like
monit
) to restart tasks if they fail. - Logging: Keep logs of task execution for troubleshooting and performance analysis.
Best Practices
- Efficiency: Ensure that the task being executed frequently is optimized for quick completion to avoid overlapping executions.
- Resource Management: Be mindful of system resources, especially when running high-frequency tasks.
- Security: Validate inputs and outputs if your task interacts with external systems or data.
By exploring these methods, you can effectively schedule tasks at sub-minute intervals on Linux systems, overcoming the limitations of traditional cron jobs.