0 votes
100 views
in Linux by
Is it possible to schedule a cron job to run in seconds, and if so, what is the process for configuring such a job in Linux?

1 Answer

0 votes
by

Cron, the standard scheduler for Linux systems, operates on a schedule of minutes and does not support scheduling jobs in seconds. The smallest unit of time that can be scheduled with cron is a minute. This is because cron reads the schedule from a file called the crontab, and the format of the entries in this file is in the format of minute, hour, day, month, and day of the week.

However, there are alternative ways to schedule cron-like tasks in seconds. One way is to use the sleep command in a script that is scheduled to run every minute. The script can then wait for the desired number of seconds before executing the task.

Another way is to use other scheduler like systemd timers that allow you to schedule tasks in seconds.

For example, if you want to run a script every 2 minutes and 30 seconds, you can schedule a cron job to run every 2 minutes and within the script, include a sleep command to pause the script for 30 seconds before executing the task.

#!/bin/bash

# wait for 30 seconds

sleep 30s

# execute task

command-to-be-executed
*/2 * * * * /path/to/script.sh
Alternatively, you can use systemd-timers to schedule tasks in seconds.
OnCalendar=*-*-* *:*:*/2:30
Both ways will run task every 2 minutes and 30 seconds
Please note that these are just examples, and you will need to adjust the specific commands and paths as needed for your system.
...