lk/basics/cron.md

113 lines
2.3 KiB
Markdown
Raw Normal View History

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
---
2021-05-15 14:41:45 +00:00
# Cron
2020-01-02 00:04:35 +00:00
2022-11-23 20:36:47 +00:00
The crontab program might have various names, like `cronie` or `crond`.
2020-01-02 00:04:35 +00:00
2022-11-23 20:36:47 +00:00
> 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
2022-11-23 20:36:47 +00:00
> sudo systemctl list-unit-files | grep cron
2020-01-02 00:04:35 +00:00
2022-11-23 20:36:47 +00:00
> sudo systemctl enable --now cron
2020-01-02 00:04:35 +00:00
2022-11-23 20:36:47 +00:00
You can *e*dit your crontab with:
2020-01-02 00:04:35 +00:00
2022-11-23 20:36:47 +00:00
> crontab -e
2020-01-02 00:04:35 +00:00
2022-11-23 20:36:47 +00:00
```
2020-01-02 00:04:35 +00:00
2022-11-23 20:36:47 +00:00
39 */3 * * * /usr/bin/updatedb
2020-01-02 00:04:35 +00:00
2022-11-23 20:36:47 +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:
`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`
2022-11-23 20:36:47 +00:00
### Full Paths
Executing something requires the full path to where it is, so you cannot simply use `apt update -y`, because cron does not know where `apt` is.
Instead, find out where it is:
> type -P apt
`/usr/bin/apt`
Then put that into the crontab:
> sudo crontab -e
`40 */3 * * * /usr/bin/apt update -y`
This will run `apt update -y` as root every 3 hours, at 40 minutes past the hour, e.g. 00:40, 03:40, 06:40.
## Directories
You can execute a script as root by putting it into a directory, instead of in the tab.
Look at the available cron directories:
> ls /etc/cron.\*
### Testing with runparts
2020-01-02 00:04:35 +00:00
Run-parts runs all executable scripts in a directory.
> run-parts /etc/cron.hourly
2022-11-23 20:36:47 +00:00
## Tips
### Variables
Add your `$HOME` to crontab to use scripts.
First add `HOME=/home/user`, then you can use syntax like this:
0 * * * * $HOME/.scripts/myScript.sh
*Remember to test the script by executing that line first*:
> $HOME/.scripts/myScript.sh
You can also add your regular path to your crontab as a variable (see example below).
If you're using vim as the editor, just run this at the top of your crontab:
> :r!echo $PATH
### Location
The crontab files are in `/var/spool/cron/`, so you can backup or restore them.
# Example
```
HOME=/home/user
PATH=/usr/condabin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/home/user/.local/bin:/home/user/.scripts/:/home/user/.local/bin:/home/user/.scripts/
1 0 1 * * /usr/bin/mkdir -p $HOME/arc/$(date +%Y/%m)
18 0 1 */3 * $HOME/.scripts/mail-clean.sh
* * * * * ping -c 1 home || mail-pull.sh
50 18 * * * /usr/bin/timeout 30m /usr/bin/syncthing
```