eolas/zk/Cron.md

92 lines
1.4 KiB
Markdown
Raw Normal View History

2022-04-23 13:26:53 +01:00
---
tags:
- shell
---
2022-09-06 15:44:40 +01:00
# Cron
2022-06-04 12:30:05 +01:00
2022-09-06 15:44:40 +01:00
## `cronie`
2022-06-04 12:30:05 +01:00
In Arch Linux I use `cronie` for cron jobs. (There is no cron service installed
2024-02-26 19:45:24 +00:00
by default). Install `cronie` and then enable it in systemd with:
2022-08-20 12:00:04 +01:00
2022-09-06 15:44:40 +01:00
```bash
2022-06-04 12:30:05 +01:00
systemctrl enable --now cronie.service
```
2022-09-06 15:44:40 +01:00
2022-08-20 12:00:04 +01:00
## commands
2022-04-23 13:26:53 +01:00
2022-05-23 19:30:04 +01:00
### List cron jobs
2022-09-06 15:44:40 +01:00
2022-05-23 19:30:04 +01:00
```
2022-04-23 13:26:53 +01:00
crontab -l
2022-05-23 19:30:04 +01:00
```
2022-04-23 13:26:53 +01:00
2022-05-23 19:30:04 +01:00
### Open cron file
2022-09-06 15:44:40 +01:00
2022-05-23 19:30:04 +01:00
```
2022-04-23 13:26:53 +01:00
crontab -e
2022-05-23 19:30:04 +01:00
```
2022-09-06 15:44:40 +01:00
2022-05-23 19:30:04 +01:00
### Check cron log
2022-09-06 15:44:40 +01:00
2022-05-23 19:30:04 +01:00
```bash
2022-04-23 13:26:53 +01:00
2022-05-23 19:30:04 +01:00
journalctl | grep CRON
# Different distros have different loggers
```
2022-04-23 13:26:53 +01:00
2022-05-23 19:30:04 +01:00
## Syntax
2022-09-06 15:44:40 +01:00
```bash
2022-04-23 13:26:53 +01:00
m h d mon dow command
# minute, hour, day of month, day of week, bash script/args
# 0-59, 0-23, 1-31, 1-12, 0-6
2022-09-06 15:44:40 +01:00
```
2022-05-23 19:30:04 +01:00
2022-04-23 13:26:53 +01:00
**Examples**
2022-09-06 15:44:40 +01:00
Run on the hour every hour
2022-04-23 13:26:53 +01:00
2022-09-06 15:44:40 +01:00
```
2022-04-23 13:26:53 +01:00
0 * * * * mysqlcheck --all-databases --check-only-changed --silent
2022-09-06 15:44:40 +01:00
```
2022-04-23 13:26:53 +01:00
2022-09-06 15:44:40 +01:00
At 01:42 every day:
2022-04-23 13:26:53 +01:00
2022-09-06 15:44:40 +01:00
```
2022-04-23 13:26:53 +01:00
42 1 * * * mysqlcheck --all-databases --check-only-changed --silent
2022-09-06 15:44:40 +01:00
```
Every half hour:
2022-04-23 13:26:53 +01:00
2022-05-23 19:30:04 +01:00
```
0,30 * * * * ${HOME}/bash_scripts/automate_commit.sh
```
2022-04-23 13:26:53 +01:00
**Shorthands**
2022-09-06 15:44:40 +01:00
- `@reboot` Run once, at startup
- `@yearly` Run once a year, “0 0 1 1 \*”.\</>
- `@annually` same as @yearly
- `@monthly` Run once a month, “0 0 1 \* \*”
- `@weekly` Run once a week, “0 0 \* \* 0”
- `@daily` Run once a day, “0 0 \* \* \*”
- `@midnight` same as @daily
- `@hourly` Run once an hour, “0 \* \* \* \*”
2022-04-23 13:26:53 +01:00
**Examples**
2022-09-06 15:44:40 +01:00
```
2022-04-23 13:26:53 +01:00
@hourly mysqlcheck --all-databases --check-only-changed --silent
2022-09-06 15:44:40 +01:00
```
2022-04-23 13:26:53 +01:00
**View the logs**
2022-09-06 15:44:40 +01:00
```bash
2022-04-23 13:26:53 +01:00
sudo grep crontab syslog
2022-09-06 15:44:40 +01:00
```