cheatsheets/cron.md

79 lines
2.1 KiB
Markdown
Raw Permalink Normal View History

2013-10-14 02:36:58 +00:00
---
2013-05-29 12:19:07 +00:00
title: Cron
2015-11-24 05:02:17 +00:00
category: CLI
updated: 2024-03-17
2017-08-29 16:38:47 +00:00
weight: -3
2013-10-14 02:36:58 +00:00
---
2013-05-29 12:19:07 +00:00
2017-08-28 16:58:09 +00:00
## Format
{: .-two-column}
2013-05-29 12:19:07 +00:00
### Format
2017-08-28 16:58:09 +00:00
```
Min Hour Day Mon Weekday
```
{: .-setup}
```
* * * * * command to be executed
```
```
┬ ┬ ┬ ┬ ┬
│ │ │ │ └─ Weekday (0=Sun .. 6=Sat)
│ │ │ └────── Month (1..12)
│ │ └─────────── Day (1..31)
│ └──────────────── Hour (0..23)
└───────────────────── Minute (0..59)
```
{: .-setup.-box-chars}
2013-05-29 12:19:07 +00:00
### Operators
| Operator | Description |
| --- | --- |
| `*` | all values |
| `,` | separate individual values |
| `-` | a range of values |
| `/` | divide a value into steps |
### Special strings
| String | Description |
| --- | --- |
| `@reboot` | every rebot |
| `@hourly` | once every hour - same as `0 * * * *` |
| `@daily` | once every day - same as `0 0 * * *` |
| `@midnight` | once every midnight - same as `@daily` |
| `@weekly` | once every week - same as `0 0 * * 0` |
| `@monthly` | once every month - same as `0 0 1 * *` |
| `@yearly` | once every year - same as `0 0 1 1 *` |
2013-05-29 12:19:07 +00:00
### Examples
| Example | Description |
| --- | --- |
| `0 * * * *` | every hour |
| `*/15 * * * *` | every 15 mins |
| `0 */2 * * *` | every 2 hours |
| `0 18 * * 0-6` | every week Mon-Sat at 6pm |
2021-09-27 01:12:30 +00:00
| `10 2 * * 6,7` | every Sat and Sun on 2:10am |
| `0 0 * * 0` | every Sunday midnight |
2017-08-28 16:58:09 +00:00
### Crontab
```bash
# Adding tasks easily
echo "@reboot echo hi" | crontab
# Open in editor - optional for another user
crontab -e [-u user]
2017-08-28 16:58:09 +00:00
# List tasks - optional for another user
2017-08-28 16:58:09 +00:00
crontab -l [-u user]
# Delete crontab file - optional for another user
crontab -r [-u user]
2017-08-28 16:58:09 +00:00
```