# 7.19: Cron and Crontab

$$\newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$

$$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$

( \newcommand{\kernel}{\mathrm{null}\,}\) $$\newcommand{\range}{\mathrm{range}\,}$$

$$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$

$$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}[1]{\| #1 \|}$$

$$\newcommand{\inner}[2]{\langle #1, #2 \rangle}$$

$$\newcommand{\Span}{\mathrm{span}}$$

$$\newcommand{\id}{\mathrm{id}}$$

$$\newcommand{\Span}{\mathrm{span}}$$

$$\newcommand{\kernel}{\mathrm{null}\,}$$

$$\newcommand{\range}{\mathrm{range}\,}$$

$$\newcommand{\RealPart}{\mathrm{Re}}$$

$$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$

$$\newcommand{\Argument}{\mathrm{Arg}}$$

$$\newcommand{\norm}[1]{\| #1 \|}$$

$$\newcommand{\inner}[2]{\langle #1, #2 \rangle}$$

$$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\AA}{\unicode[.8,0]{x212B}}$$

$$\newcommand{\vectorA}[1]{\vec{#1}} % arrow$$

$$\newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow$$

$$\newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vectorC}[1]{\textbf{#1}}$$

$$\newcommand{\vectorD}[1]{\overrightarrow{#1}}$$

$$\newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}}$$

$$\newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}}$$

$$\newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$

## Cron

Cron is a standard utility of the Linux operating system that allows for the automation of tasks.  It is a daemon, which is a process that typically starts on boot-up and runs in the background.  The Cron daemon is a time-based scheduler and gets its name the Greek word for time χρόνος (chronos).  A Cron job is a task run by Cron that occurs on boot-up, or during a specified time.  Cron is typically used by system administers to perform periodic processes. Examples of processes that could be scheduled with Cron include:

• Backing up important data and files
• Updating software and packages
• Disk cleanup and defragmentation
• Sending automated emails on a schedule
• Sending an email with IP address on boot-up (our first cron job)

Both Cron and Python can be used to schedule the reading of sensor data and subsequent transmission to the web. The fastest Cron can schedule a job is every minute and so it will not work if you need data faster than that.  The advantage of using the Cron daemon to read sensors and transmit data to the web is that you do not have to load Python into memory for the entire process, only when you are actively calling the program. It also allows you to keep your Python scripts simpler. But you can not use Cron for conditional scheduling (like sending data when a specific sensor value is acquired) or real-time interactions.

## Crontab

Crontab (Cron Table) is a file you can access through the command line and contains the tasks you wish to schedule for Cron.  Each user can have their own Crontab and Cron will read each user's crontab and if it has changed, upload the new list to the cron. So the Crontab is how you interact with Cron.  The crontab essentially tells cron what files you want to run, and when to run them.

To edit crontab type

pi@hostname:~ \$ crontab -e

The first time you do this, you will be prompted to select your favorite editor. We will select 1 for the nano editor.  If a crontab exists you will see its content.  At the beginning of this class we will create a crontab that will email your IP address on bootup.  We will do this because you will need your IP address to access your Raspberry Pi through VNC viewer, and instructions are here.

At a later point in the semester we will create a functional Crontab file (lesson here).  The approach we will use is to download a template called crontab.src that has common tasks commented out. We will then call up various Python scripts by putting the commands into the crontab.  It is very important that your python scripts have complete paths for any files that are called because you will be running them from Cron.

## Resources

7.19: Cron and Crontab is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.