edit crontab

This commit is contained in:
Malin Freeborn 2024-11-27 22:21:59 +01:00
parent 588ce9b0cb
commit f4176a9ddb
Signed by: andonome
GPG Key ID: 52295D2377F4D70F

View File

@ -2,9 +2,11 @@
title: "cron"
tags: [ "Documentation", "Basics" ]
---
# Cron
# Cronie
The crontab program might have various names, like `cronie` or `crond`.
The `cronie` program is also known as `crond`.
## Install
```bash
sudo apt search -n ^cron
@ -17,29 +19,33 @@ sudo systemctl list-unit-files | grep cron
sudo systemctl enable --now $NAME
```
Make a file for your crontab, like this:
## Usage
Show your current crontab:
```bash
echo '39 */3 * * * /usr/bin/updatedb' > "$USER".cron
crontab -l
```
You can put this in a file and edit it:
```bash
crontab -l > $filename
echo '39 3 */3 * * /bin/tar czf /tmp/etc_backup.tgz /etc/' >> $filename
```
Then apply that crontab:
```bash
crontab "$USER".cron
rm "$USER".cron
crontab $filename
rm $filename
```
The `cron` program will check your syntax before adding the tab.
Your crontab file sits somewhere in `/var/spool/`.
Probably in `/var/spool/cron`.
Check how your tab currently looks:
```bash
crontab -l
```
## Syntax
`* * * * *`
@ -50,43 +56,61 @@ These five points refer to:
So '3pm every Sunday' would be:
> 0 15 * * 7
`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
`0 15 * 2 7`
### Full Paths
### Variables
`cronie` doesn't know where you live, so to put something in your `$HOME` directory, you have to tell it:
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:
```bash
type -P apt
echo "HOME=$HOME" > $filename
crontab -l >> $filename
crontab $filename
```
`/usr/bin/apt`
`cronie` doesn't know where anything lives, including programs.
You can give it your usual `$PATH` variable like this:
Then put that into the crontab:
```bash
sudo crontab -e
echo $PATH > $filename
crontab -l >> $filename
crontab $filename
```
> 40 */3 * * * /usr/bin/apt update -y
Now instead of doing this
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.
`40 */3 * * * /usr/bin/du -sh $HOME/* | sort -h > $HOME/sum.txt`
## Directories
You can simply do this:
`40 */3 * * * du -sh $HOME/* | sort -h > $HOME/sum.txt`
## Run as Root
You can execute a script as root by putting it into a directory, instead of in the tab.
Look at the available cron directories:
```bash
ls /etc/cron.\*
ls -d /etc/cron.*
```
Make a script which runs daily:
```bash
f=apt_update.sh
echo '#!/bin/bash' > $f
echo 'apt update --yes' >> $f
chmod +x $f
sudo mv $f /etc/cron.daily/
```
### Testing with runparts
@ -97,47 +121,9 @@ Run-parts runs all executable scripts in a directory.
run-parts /etc/cron.hourly
```
## 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*:
```bash
$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:
```bash
:r!echo PATH=$PATH
```
# Troubleshooting
### `date` Commands
Cron doesn't understand the `%` sign, so if you want to use `date +%R`, then it should be escaped with a backslash: `date +\%R`.
### File 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
```