How to Auto-Start Programs and Schedule Commands on Raspberry Pi OS
- 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
The 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 - @reboot
.
The cron
provides several keywords for common periods like @daily
, @weekly
, @monthly
, @hourly
, etc.
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 @reboot
keyword.
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
Output:
514 ? 00:00:00 sleep
Then you can run the kill
command with the PID (process ID) argument to terminate the sleep
program.
kill 514
More details about crontab
configuration files can be found in the corresponding Debian manpage.
Use Anacron to Schedule Tasks on Raspberry Pi OS
The 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.
The 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.
The 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 #
character.
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 /etc/anacrontab
with sudo
prefixed command.
You can find additional configuration details of anacron
here.
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn FacebookRelated Article - Raspberry Pi
- How to Setup a Raspberry Pi File Server
- How to Setup MySQL in Raspberry Pi
- DNS Server on Raspberry Pi
- How to Use Dropbox on Raspberry Pi
- How to Use Plex Media Player on Raspberry Pi
- Chromium OS on Raspberry Pi