- Use Cron Daemon to Schedule Tasks on Raspberry Pi OS
- Use Anacron to Schedule Tasks on Raspberry Pi OS
This article will introduce several methods for automatically scheduling programs and commands at specific times on Raspberry Pi OS.
Use Cron Daemon to Schedule Tasks on Raspberry Pi OS
cron daemon basics are covered in one of our articles, and we suggest you read first if you’re unfamiliar with it. In this case, we will demonstrate how to auto-start programs right after the operating system successfully boots.
Remember that the
crontab file includes one line for each scheduled job, where the first five items are time-date fields. In contrast, when we want to schedule a cron job to run on the system startup, we have to use a special keyword specifier -
cron provides several keywords for common periods like
The following crontab entry defines a cron job to run the
sleep command on system startup. Notice that all five date-time fields are replaced using the
You won’t specify additional fields when special keywords are used in the same cron job entry.
@reboot sleep 30m
In this scenario, we chose the
sleep command because it’s an easy method for creating a long-running process that can be later be inspected using different commands.
The above sample command runs the
sleep program for 30 minutes and then exits automatically.
You can explicitly terminate it by delivering a signal. First, you need to find the process ID using the following command.
ps -A | grep sleep
514 ? 00:00:00 sleep
Then you can run the
kill command with the PID (process ID) argument to terminate the
More details about
crontab configuration files can be found in the corresponding Debian manpage.
Use Anacron to Schedule Tasks on Raspberry Pi OS
cron is ideal for running systems with no downtime, while regular users often shut down their devices. The latter scenarios are likely to result in missed cron jobs, so we might need a solution to run the jobs once the system is running.
anacron is similar to
cron, which offers asynchronous scheduling tasks, implying that missed ones will eventually run at some point. However,
anacron only supports minimum time intervals of a single day; hence it might not suit many scenarios.
anacron task entries are located in the
/etc/anacrontab configuration file.
The config file usually includes several entries, but you can append user-defined tasks at the end of the file and even add custom comments starting with the
In the following example, we create a daily task to run the
sleep 30m command.
1 10 cron.daily sleep 30m
Notice that the first column (
1) specifies the number of days as intervals between task runs, while the second column indicates the number of minutes to delay the given task if it runs on system startup.
The latter scenario is expected for the missed tasks, as the system was not running at the specified time,
anacron will run them on the next startup. However, you might want to delay some tasks to alleviate the excessive load on the system startup, hence the second column.
One downside of the
anacron is that your user must have administrator privileges to set up tasks. On Raspberry Pi OS,
pi users can set up
anacron tasks by modifying
sudo prefixed command.
You can find additional configuration details of