2022-01-16 18:20:39 +00:00
|
|
|
---
|
|
|
|
title: "cron"
|
|
|
|
tags: [ "Documentation", "basics" ]
|
|
|
|
---
|
2021-05-15 14:41:45 +00:00
|
|
|
# Cron
|
2020-01-02 00:04:35 +00:00
|
|
|
|
|
|
|
Various services from cron exist, e.g.
|
|
|
|
|
2021-06-09 00:44:55 +00:00
|
|
|
> sudo apt -y install cron
|
2020-01-02 00:04:35 +00:00
|
|
|
|
2021-05-15 14:41:45 +00:00
|
|
|
Start the cronie with
|
2020-01-02 00:04:35 +00:00
|
|
|
|
2021-06-09 00:44:55 +00:00
|
|
|
> sudo systemctl enable --now cron
|
2020-01-02 00:04:35 +00:00
|
|
|
|
2021-05-15 14:41:45 +00:00
|
|
|
Specify a cron job with:
|
2020-01-02 00:04:35 +00:00
|
|
|
|
|
|
|
> cron -e
|
|
|
|
|
|
|
|
You can run a script with:
|
|
|
|
|
|
|
|
*/10 * * * * /home/pi/script.sh
|
|
|
|
|
|
|
|
... which would run every 10 minutes.
|
|
|
|
|
|
|
|
To run something as root, do:
|
|
|
|
|
|
|
|
> sudo crontab -e
|
|
|
|
|
|
|
|
For example, you can update the database, meaning searches with 'locate' command will be faster.
|
|
|
|
|
|
|
|
> */30 * * * * /usr/bin/updatedb
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
`0 15 * * 7`
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
`0 15 * 2 7`
|
|
|
|
|
2020-01-02 00:04:35 +00:00
|
|
|
# Testing with runparts
|
|
|
|
|
|
|
|
Run-parts runs all executable scripts in a directory.
|
|
|
|
|
|
|
|
> run-parts /etc/cron.hourly
|
|
|
|
|