cron - linux
Cron Gotchas / Things to be aware of
1. On remote servers, (in my experience) cron runs at UTC time; Verify this fact before deploying a cronjob
Timing Of Commands
# On remote servers, (in my experience) cron runs at UTC time; Verify this fact before deploying a cronjob
# Check UTC time here
Use this website to easily create a timing scheme
Days of week are:
0 1 2 3 4 5 6
S M T W T F S
Hours are 0-23 0 being 12 AM
Get output from cron command
* * * * * command >> output_file.log 2>&1
# The 2>&1 part says redirect stderr (2) to stdout (1) which is the output_file.log
# >& represents writing output from one file to the input of another file
Add path to crontab
put this at the top of your crontab:
PATH=/home/steven/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/bin:/home/steven/bin
Note that extending or modifying the path could present a security risk.
Run commands that pertain to the display
# Add this line to the top of your crontab
DISPLAY=:1
# Check your env's DISPLAY var with env | grep DISPLAY
Run sound commands
# Or allow sounds to be played during other sounds
# Add this to top of crontab
XDG_RUNTIME_DIR="/run/user/1000"
# I think this is specific to whichever user the crontab is being run from
# In this case user '1000'
Ensure Command is Run (even if computer is off) Anacron
# I found that the best way to use anacron is by creating a user specific one.
# Otherwise you can run into issues where the root user doesn't have packages used by scripts, etc...
# Source: https://opensource.com/article/21/2/linux-automation ; Find explanations here if desired
# 1. Create relevant directories
mkdir -p ~/.local/etc/cron.daily ~/.var/spool/anacron
# 2. Create personal anacrontab ( or copy /etc/anacrontab as a template; make sure to delete cron.daily/weekly/monthly lines )
File
# Download in ~/.local/etc/
# 3. Add necessary directories to the PATH variable
/home/username/.local/bin perhaps?
# 4. Now you can test by adding a custom script line to anacrontab
1 0 test /home/username/.local/etc/cron.daily/test-script >> ~/test_results.log 2>&1
# 5. Then test run the file
anacron -fn -t /home/username/.local/etc/anacrontab -S /home/username/.var/spool/anacron
# Check to confirm your output is as expected in ~/test_results.log
# 6. Add testing function to ~/.profile and then reboot to make sure it works on reboot
anacron -fn -t /home/username/.local/etc/anacrontab -S /home/username/.var/spool/anacron
# 7. If it worked, then remove the force flags -fn from the ~/.profile command
anacron -t /home/username/.local/etc/anacrontab -S /home/username/.var/spool/anacron
Success! You can now either add executable files to your ~/.local/etc/cron.daily directory or
add more lines to your anacrontab.
Optional: Add a cron.weekly/monthly directory and write that into the anacrontab
Add lines to existing user anacrontab
Format:
<time period in days> <how long to wait on startup> <named identifier> <command to execute>
2 1 test /home/username/.local/etc/cron.daily/test-script >> ~/test_results.log 2>&1
# Every 2 days execute test-script command and log the stdout to ~/test_results.log
Install new command at the end of your crontab
#!/bin/bash
line="* * * * * /path/to/command"
(crontab -u $(whoami) -l; echo "$line" ) | crontab -u $(whoami) -
Run cronjob with certain environment enabled
# Note, add-to-env is installed from pip3 install slg_dev_ops
* * * * * env - $(add-to-env /path/to/env/file) your_command