Compare commits
2 Commits
8eea348112
...
af52292ef8
Author | SHA1 | Date | |
---|---|---|---|
af52292ef8
|
|||
865b4a2da1
|
129
basics/cron.md
129
basics/cron.md
@@ -1,129 +0,0 @@
|
||||
---
|
||||
title: "cron"
|
||||
tags: [ "basics", "time" ]
|
||||
---
|
||||
# Cronie
|
||||
|
||||
The `cronie` program is also known as `crond`.
|
||||
|
||||
## Install
|
||||
|
||||
```sh
|
||||
sudo apt search -n ^cron
|
||||
```
|
||||
|
||||
Once installed, search for the service name, and start it.
|
||||
|
||||
```sh
|
||||
sudo systemctl list-unit-files | grep cron
|
||||
sudo systemctl enable --now $NAME
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Show your current crontab:
|
||||
|
||||
```sh
|
||||
crontab -l
|
||||
```
|
||||
|
||||
You can put this in a file and edit it:
|
||||
|
||||
```sh
|
||||
crontab -l > $filename
|
||||
echo '39 3 */3 * * /bin/tar czf /tmp/etc_backup.tgz /etc/' >> $filename
|
||||
```
|
||||
|
||||
Then apply that crontab:
|
||||
|
||||
```sh
|
||||
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`.
|
||||
|
||||
## Syntax
|
||||
|
||||
`* * * * *`
|
||||
|
||||
These five points refer to:
|
||||
|
||||
`minute hour day month weekday`
|
||||
|
||||
So '3pm every Sunday' would be:
|
||||
|
||||
`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`
|
||||
|
||||
### Variables
|
||||
|
||||
`cronie` doesn't know where you live, so to put something in your `$HOME` directory, you have to tell it:
|
||||
|
||||
|
||||
```sh
|
||||
echo "HOME=$HOME" > $filename
|
||||
crontab -l >> $filename
|
||||
crontab $filename
|
||||
```
|
||||
|
||||
`cronie` doesn't know where anything lives, including programs.
|
||||
You can give it your usual `$PATH` variable like this:
|
||||
|
||||
|
||||
```sh
|
||||
echo $PATH > $filename
|
||||
crontab -l >> $filename
|
||||
crontab $filename
|
||||
```
|
||||
|
||||
Now instead of doing this
|
||||
|
||||
`40 */3 * * * /usr/bin/du -sh $HOME/* | sort -h > $HOME/sum.txt`
|
||||
|
||||
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:
|
||||
|
||||
```sh
|
||||
ls -d /etc/cron.*
|
||||
```
|
||||
|
||||
Make a script which runs daily:
|
||||
|
||||
```sh
|
||||
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
|
||||
|
||||
Run-parts runs all executable scripts in a directory.
|
||||
|
||||
```sh
|
||||
run-parts /etc/cron.hourly
|
||||
```
|
||||
|
||||
# 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`.
|
||||
|
20
system/clean_your_downloads.md
Normal file
20
system/clean_your_downloads.md
Normal file
@@ -0,0 +1,20 @@
|
||||
---
|
||||
title: "Clean Your Downloads"
|
||||
tags: [ "system", "tmpfs" ]
|
||||
---
|
||||
|
||||
'Downloads` directory always too full of crap?
|
||||
Make it a temporary filesystem!
|
||||
Everything will be deleted whenever you reboot.
|
||||
|
||||
|
||||
```sh
|
||||
rm -rf ~/Downloads # Be brave!
|
||||
mkdir Downloads
|
||||
cp /etc/fstab /tmp/
|
||||
echo "tmpfs $HOME/Downloads tmpfs defaults,size=1G 0 0" | sudo tee -a /etc/fstab
|
||||
sudo systemctl daemon-reload # Ignore this if you don't use systemd
|
||||
sudo mount -a
|
||||
mount | tail -1
|
||||
```
|
||||
|
Reference in New Issue
Block a user