LinkedIn Sourceforge

Vincent's Blog

Pleasure in the job puts perfection in the work (Aristote)

A simple cron for Laptops

Posted on 2024-11-06 22:32:00 from Vincent in OpenBSD vdcron

I was frustrated that recurrent jobs are not running like I want via the cron on my laptops. I was looking for something which trigger actions every hours the machine is running, or once every day, or once every week. For a daily task, it doesn't matter if we are the morning or the evening. For a weekly task doesn't matter if we are saturday, sunday or monday.

So I wrote a quite simple shell script dealing with this job, I've called it "vdcron"

Note of 2024/11/10: This post has few days, and I receive questions about "why not anacron?". So, I've just completed this post with this aspect.


vdcron

vdcron is a scheduling utility designed to replace cron for systems that do not run continuously, such as laptops. Unlike traditional cron, vdcron does not assume a machine is always on and instead runs scheduled tasks when triggered by an external event, such as waking up from sleep.

Source code

You can download it from my git repositroy

Features

Flexible Scheduling: Schedule tasks based on specific dates, intervals, weekdays, or months, similar to cron.
Trigger-Based Execution: vdcron isn't a background daemon. Instead, it's designed to be triggered by external events, such as system resume or user login.
Resilient Task Execution: Ensures tasks run when the schedule is met, even if delayed by system downtime. Failed tasks remain in the schedule for retry.
Dynamic Rescheduling: Tasks can be configured to re-run at intervals, making it ideal for periodic jobs.

How It Works

Since vdcron does not run continuously, it must be triggered by other means. For example:
Resume from Sleep: On OpenBSD, trigger vdcron via the apmd resume script:

obsd:~ # cat /etc/apm/resume
#!/bin/sh
/usr/local/bin/vdcron

Ensure apmd is enabled:

rcctl enable apmd
rcctl start apmd

Other Triggers: vdcron can also be run at startup, on user login, or even periodically with cron using @reboot.

Command-Line Options

-h: Display help information
-d: Debug mode for detailed logs
-s value: Set an initial sleep time (default: 10 seconds)
-f file: Specify an alternative config file (default: /etc/vdcron.conf for root or ~/.vdcron/vdcron.conf for other users)

Configuration File (vdcron.conf)

vdcron uses a configuration file, /etc/vdcron.conf for root or ~/.vdcron/vdcron.conf for regular users. Each line represents a task with a schedule and command. The format options include:

Date-Based: Run at a specific date or time, with optional repetition. Example:

+20240101 echo "Happy New Year!"

Weekday and Month-Based: Run on specific weekdays or months, with optional intervals. Example:

Monday/7 echo "Weekly Reminder"
April/1 sh /etc/monthly_task

Always: Run every time vdcron is triggered:

Always echo "System is active"

Rescheduling Failed Tasks: Tasks with a + prefix retry on failure, ensuring network or dependency issues do not prevent completion.

Example Configuration

+20240101 echo "Happy New Year!"
Sunday sh /etc/weekly_maintenance
Always /usr/local/bin/monitor_script
+20240101/1 sh /etc/daily_task

Logging

Output and errors are logged to /var/log/vdcron.log for root or ~/.vdcron/vdcron.log for other users. Logging can be suppressed with the -q option in the configuration file.

Compatibility

Tested on OpenBSD 6.3+ and on debian.

There is no requiements to install before using it.

Comparing VDcron and Anacron: Addressing Key Pain Points

For those familiar with task scheduling on systems that may not be running continuously, Anacron has been a standard tool. However, VDcron introduces significant improvements to address various limitations of Anacron. Below, I outline some of the pain points of Anacron and highlight how VDcron, a pure shell script, effectively resolves them.

Project Maintenance and Updates

Anacron: The original Anacron project, hosted on SourceForge, has not seen updates since June 22, 2000, with version 2.4.3 being the last one available for OpenBSD. This outdated status can lead to compatibility and security concerns.
VDcron: Being actively developed as a shell script, VDcron offers simplicity and adaptability without relying on external dependencies.

Dependency Management

Anacron: When used on modern systems, especially OpenBSD, dependencies can become a hurdle. Cronie, a project that includes a more recent version of Anacron, is not available on OpenBSD.
VDcron: Written purely in shell script, VDcron has no dependencies, making it lightweight and ideal for OpenBSD and other Unix-like systems.

Scheduling Flexibility

Anacron: A major limitation is that it only supports task execution once per day. This rigidity prevents scheduling more frequent tasks, such as running every hour or multiple times per day.
VDcron: Offers flexibility by allowing tasks to be scheduled for execution at various intervals, including multiple times per day or even hourly, providing better alignment with modern scheduling needs.

Error Handling and Retries

Anacron: If a task fails during execution, Anacron does not retry it. This is problematic for scenarios where temporary issues, such as network instability, cause failures.
VDcron: Incorporates a queue system to retain failed tasks, ensuring they are retried at a later time. This is crucial for reliability in environments where external factors (e.g., network availability) may impact task completion.



3, 2
displayed: 562
Comments:

1. From aisyah on Mon Nov 11 06:45:40 2024

C'est impressionnant de voir comment vdcron comble les lacunes d'Anacron avec une approche légère et adaptable pour les systèmes non continus.




What is the second letter of the word Python?