2022-01-16 18:20:39 +00:00
|
|
|
---
|
|
|
|
title: "cron"
|
2022-01-26 21:29:48 +00:00
|
|
|
tags: [ "Documentation", "Basics" ]
|
2022-01-16 18:20:39 +00:00
|
|
|
---
|
2024-11-27 21:21:59 +00:00
|
|
|
# Cronie
|
2020-01-02 00:04:35 +00:00
|
|
|
|
2024-11-27 21:21:59 +00:00
|
|
|
The `cronie` program is also known as `crond`.
|
|
|
|
|
|
|
|
## Install
|
2020-01-02 00:04:35 +00:00
|
|
|
|
2023-06-17 19:28:20 +00:00
|
|
|
```bash
|
|
|
|
sudo apt search -n ^cron
|
|
|
|
```
|
2020-01-02 00:04:35 +00:00
|
|
|
|
2022-11-23 20:36:47 +00:00
|
|
|
Once installed, search for the service name, and start it.
|
2020-01-02 00:04:35 +00:00
|
|
|
|
2023-06-17 19:28:20 +00:00
|
|
|
```bash
|
|
|
|
sudo systemctl list-unit-files | grep cron
|
2024-03-22 17:58:20 +00:00
|
|
|
sudo systemctl enable --now $NAME
|
2023-06-17 19:28:20 +00:00
|
|
|
```
|
2020-01-02 00:04:35 +00:00
|
|
|
|
2024-11-27 21:21:59 +00:00
|
|
|
## Usage
|
|
|
|
|
|
|
|
Show your current crontab:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
crontab -l
|
|
|
|
```
|
|
|
|
|
|
|
|
You can put this in a file and edit it:
|
2024-03-22 17:58:20 +00:00
|
|
|
|
2023-06-17 19:28:20 +00:00
|
|
|
```bash
|
2024-11-27 21:21:59 +00:00
|
|
|
crontab -l > $filename
|
|
|
|
echo '39 3 */3 * * /bin/tar czf /tmp/etc_backup.tgz /etc/' >> $filename
|
2023-06-17 19:28:20 +00:00
|
|
|
```
|
2020-01-02 00:04:35 +00:00
|
|
|
|
2024-03-22 17:58:20 +00:00
|
|
|
Then apply that crontab:
|
2020-01-02 00:04:35 +00:00
|
|
|
|
2023-06-17 19:28:20 +00:00
|
|
|
```bash
|
2024-11-27 21:21:59 +00:00
|
|
|
crontab $filename
|
|
|
|
rm $filename
|
2022-11-23 20:36:47 +00:00
|
|
|
```
|
2024-11-27 21:21:59 +00:00
|
|
|
|
2024-03-22 17:58:20 +00:00
|
|
|
The `cron` program will check your syntax before adding the tab.
|
|
|
|
|
|
|
|
Your crontab file sits somewhere in `/var/spool/`.
|
|
|
|
Probably in `/var/spool/cron`.
|
2020-01-02 00:04:35 +00:00
|
|
|
|
2021-05-15 14:41:45 +00:00
|
|
|
## Syntax
|
|
|
|
|
|
|
|
`* * * * *`
|
|
|
|
|
|
|
|
These five points refer to:
|
|
|
|
|
|
|
|
`minute hour day month weekday`
|
|
|
|
|
|
|
|
So '3pm every Sunday' would be:
|
|
|
|
|
2024-11-27 21:21:59 +00:00
|
|
|
`0 15 * * 7`
|
2021-05-15 14:41:45 +00:00
|
|
|
|
|
|
|
Here 'Sunday' is indicated by "7", and '3pm' is 'the 15th hour'.
|
|
|
|
The minute is '0' (i.e. '0 minutes past three pm').
|
|
|
|
|
|
|
|
Doing the same thing, but only in February, would be:
|
|
|
|
|
2024-11-27 21:21:59 +00:00
|
|
|
`0 15 * 2 7`
|
|
|
|
|
|
|
|
### Variables
|
2021-05-15 14:41:45 +00:00
|
|
|
|
2024-11-27 21:21:59 +00:00
|
|
|
`cronie` doesn't know where you live, so to put something in your `$HOME` directory, you have to tell it:
|
2022-11-23 20:36:47 +00:00
|
|
|
|
|
|
|
|
2023-06-17 19:28:20 +00:00
|
|
|
```bash
|
2024-11-27 21:21:59 +00:00
|
|
|
echo "HOME=$HOME" > $filename
|
|
|
|
crontab -l >> $filename
|
|
|
|
crontab $filename
|
2023-06-17 19:28:20 +00:00
|
|
|
```
|
2022-11-23 20:36:47 +00:00
|
|
|
|
2024-11-27 21:21:59 +00:00
|
|
|
`cronie` doesn't know where anything lives, including programs.
|
|
|
|
You can give it your usual `$PATH` variable like this:
|
2022-11-23 20:36:47 +00:00
|
|
|
|
|
|
|
|
2023-06-17 19:28:20 +00:00
|
|
|
```bash
|
2024-11-27 21:21:59 +00:00
|
|
|
echo $PATH > $filename
|
|
|
|
crontab -l >> $filename
|
|
|
|
crontab $filename
|
2023-06-17 19:28:20 +00:00
|
|
|
```
|
2022-11-23 20:36:47 +00:00
|
|
|
|
2024-11-27 21:21:59 +00:00
|
|
|
Now instead of doing this
|
2022-11-23 20:36:47 +00:00
|
|
|
|
2024-11-27 21:21:59 +00:00
|
|
|
`40 */3 * * * /usr/bin/du -sh $HOME/* | sort -h > $HOME/sum.txt`
|
2022-11-23 20:36:47 +00:00
|
|
|
|
2024-11-27 21:21:59 +00:00
|
|
|
You can simply do this:
|
|
|
|
|
|
|
|
`40 */3 * * * du -sh $HOME/* | sort -h > $HOME/sum.txt`
|
|
|
|
|
|
|
|
## Run as Root
|
2022-11-23 20:36:47 +00:00
|
|
|
|
|
|
|
You can execute a script as root by putting it into a directory, instead of in the tab.
|
|
|
|
Look at the available cron directories:
|
|
|
|
|
2023-06-17 19:28:20 +00:00
|
|
|
```bash
|
2024-11-27 21:21:59 +00:00
|
|
|
ls -d /etc/cron.*
|
2023-06-17 19:28:20 +00:00
|
|
|
```
|
2022-11-23 20:36:47 +00:00
|
|
|
|
2024-11-27 21:21:59 +00:00
|
|
|
Make a script which runs daily:
|
2020-01-02 00:04:35 +00:00
|
|
|
|
2023-06-17 19:28:20 +00:00
|
|
|
```bash
|
2024-11-27 21:21:59 +00:00
|
|
|
f=apt_update.sh
|
|
|
|
echo '#!/bin/bash' > $f
|
|
|
|
echo 'apt update --yes' >> $f
|
|
|
|
chmod +x $f
|
|
|
|
sudo mv $f /etc/cron.daily/
|
2023-06-17 19:28:20 +00:00
|
|
|
```
|
2020-01-02 00:04:35 +00:00
|
|
|
|
2024-11-27 21:21:59 +00:00
|
|
|
### Testing with runparts
|
2022-11-23 20:36:47 +00:00
|
|
|
|
2024-11-27 21:21:59 +00:00
|
|
|
Run-parts runs all executable scripts in a directory.
|
2022-11-23 20:36:47 +00:00
|
|
|
|
2023-06-17 19:28:20 +00:00
|
|
|
```bash
|
2024-11-27 21:21:59 +00:00
|
|
|
run-parts /etc/cron.hourly
|
2023-06-17 19:28:20 +00:00
|
|
|
```
|
2022-11-23 20:36:47 +00:00
|
|
|
|
2024-11-27 21:21:59 +00:00
|
|
|
# Troubleshooting
|
2022-11-23 20:36:47 +00:00
|
|
|
|
2023-04-13 04:06:54 +00:00
|
|
|
### `date` Commands
|
|
|
|
|
|
|
|
Cron doesn't understand the `%` sign, so if you want to use `date +%R`, then it should be escaped with a backslash: `date +\%R`.
|
|
|
|
|