This commit is contained in:
JMARyA 2023-12-28 23:58:43 +01:00
parent c555a0b664
commit f7e5b8f9f6
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263

39
technology/linux/cron.md Normal file
View file

@ -0,0 +1,39 @@
---
obj: application
wiki: https://en.wikipedia.org/wiki/Cron
arch-wiki: https://wiki.archlinux.org/title/Cron
---
# Cron
Cron is a time-based job scheduler in Unix-like operating systems. It allows users to schedule tasks (commands or scripts) to run periodically at fixed intervals, providing automation and simplifying repetitive tasks. Cron is an essential tool for system administrators, developers, and anyone who needs to automate routine activities. A modern replacement for cron are [Systemd-Timers](Systemd-Timers.md).
## Syntax
The syntax for a cron expression consists of five fields, representing the minute, hour, day of the month, month, and day of the week. Additionally, an optional sixth field allows for a specific user context. See [this website](https://crontab.guru) for help creating crontabs.
```
* * * * * command-to-be-executed
```
- **Minute (0 - 59):** The minute when the command will run.
- **Hour (0 - 23):** The hour when the command will run.
- **Day of the month (1 - 31):** The day of the month when the command will run.
- **Month (1 - 12):** The month when the command will run.
- **Day of the week (0 - 7, where both 0 and 7 represent Sunday):** The day of the week when the command will run.
### Commonly Used Expressions
- Asterisk (*): Represents all possible values for a field.
- Comma (,): Specifies a list of values.
- Hyphen (-): Specifies a range of values.
- Forward slash (/): Specifies step values for a field.
### Examples
- Run a script every day at 3:30 AM: `30 3 * * * /path/to/script.sh`
- Run a command every hour: `0 * * * * /path/to/command`
- Run a task every Monday at 8 PM: `0 20 * * 1 /path/to/task`
## Usage
A cron job is a scheduled task associated with a specific cron expression. These jobs can range from simple commands to complex scripts or programs. Cron jobs are stored in crontab files, and each user typically has their own crontab.
- View existing cron jobs: `crontab -l`
- Edit the current user's crontab: `crontab -e`
- Remove all cron jobs: `crontab -r`