fix crontabe

This commit is contained in:
Malin Freeborn 2022-11-23 21:36:47 +01:00
parent 28a5f03a60
commit 34ee9055eb
Signed by: andonome
GPG Key ID: 52295D2377F4D70F
2 changed files with 72 additions and 50 deletions

View File

@ -4,32 +4,26 @@ tags: [ "Documentation", "Basics" ]
--- ---
# Cron # Cron
Various services from cron exist, e.g. The crontab program might have various names, like `cronie` or `crond`.
> sudo apt -y install cron > sudo apt search -n ^cron
Start the cronie with Once installed, search for the service name, and start it.
> sudo systemctl list-unit-files | grep cron
> sudo systemctl enable --now cron > sudo systemctl enable --now cron
Specify a cron job with: You can *e*dit your crontab with:
> cron -e > crontab -e
You can run a script with:
*/10 * * * * /home/pi/script.sh ```
... which would run every 10 minutes. 39 */3 * * * /usr/bin/updatedb
To run something as root, do:
> sudo crontab -e
For example, you can update the database, meaning searches with 'locate' command will be faster.
> */30 * * * * /usr/bin/updatedb
```
## Syntax ## Syntax
`* * * * *` `* * * * *`
@ -49,9 +43,70 @@ Doing the same thing, but only in February, would be:
`0 15 * 2 7` `0 15 * 2 7`
# Testing with runparts ### 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
Run-parts runs all executable scripts in a directory. Run-parts runs all executable scripts in a directory.
> run-parts /etc/cron.hourly > 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*:
> $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
```

View File

@ -1,33 +0,0 @@
---
title: "cronie"
tags: [ "Documentation", "System" ]
---
Various services from cron exist, e.g.
> sudo apt -y install cronie
start the cronie with
> sudo Systemctl start cronie
start a cron with
> cron -e
You can run a script with:
*/10 * * * * /home/pi/script.sh
... which would run every 10 minutes.
To run something as root, do:
> sudo crontab -e
For example, you can update the database, meaning searches with 'locate' command will be faster.
> */30 * * * * /usr/bin/updatedb