BASH CRON

From Indie IT Wiki
Jump to: navigation, search

HOWTO: CRON

Fix Character Set Problems In Emails

Edit your crontab and add the following lines...

crontab -e
LANG=en_GB.UTF-8
LANGUAGE=en_GB.UTF-8
LC_CTYPE="en_GB.UTF-8"
LC_NUMERIC="en_GB.UTF-8"
LC_TIME="en_GB.UTF-8"
LC_COLLATE="en_GB.UTF-8"
LC_MONETARY="en_GB.UTF-8"
LC_MESSAGES="en_GB.UTF-8"
LC_PAPER="en_GB.UTF-8"
LC_NAME="en_GB.UTF-8"
LC_ADDRESS="en_GB.UTF-8"
LC_TELEPHONE="en_GB.UTF-8"
LC_MEASUREMENT="en_GB.UTF-8"
LC_IDENTIFICATION="en_GB.UTF-8"
LC_ALL=en_GB.UTF-8
CONTENT_TYPE="text/plain; charset=utf-8"

Edit the system cron settings file...

sudo nano /etc/default/cron

...and add the following line...

READ_ENV="yes"

Edit the system cron init file...

sudo nano /etc/init/cron.conf

and add the following line just before exec cron...

expect fork
respawn
env LC_ALL=en_GB.UTF-8
exec cron

Then restart the cron daemon...

sudo service cron restart

Disable Email Notifictions

Cron by default sends an email to the user account executing cronjob. To disable it add your cron job adding the >/dev/null 2>&1 at the end of the file which redirects the output of the cron to /dev/null.

crontab -e
* * * * * >/dev/null 2>&1

Edit Cron Jobs Of Current User

crontab -e

List Jobs For Current User

crontab -l

List Jobs For Specified User

crontab -u fred -l

Remove Current User's Crontab With Prompt

crontab -i -r # This will invoke the following prompt

crontab: really delete fred's crontab? (y/n)

Remove Current User's Crontab Without Prompt

crontab -r

Fields

{minute} {hour} {day-of-month} {month} {day-of-week} {path-to-shell-script}

Execute Task Job On Reboot

@reboot /usr/local/bin/flexget daemon start -d

16.04 LTS > Use journalctl Command To Display Log

sudo journalctl -u cron
sudo journalctl -u cron -b | more
sudo journalctl -u cron -b | grep something
sudo journalctl -u cron -b | grep -i error

INFO:

Times

Predefined scheduling definitions...

Entry                      Description                                    Equivalent To
@yearly (or @annually)     Run once a year, midnight, Jan. 1st            0 0 1 1 *
@monthly                   Run once a month, midnight, first of month     0 0 1 * *
@weekly                    Run once a week, midnight on Sunday            0 0 * * 0
@daily                     Run once a day, midnight                       0 0 * * *
@hourly                    Run once an hour, beginning of hour            0 * * * *
@reboot                    Run at startup

System Cron Schedules

Predefined cron directories.

  • /etc/cron.d
  • /etc/cron.daily
  • /etc/cron.hourly
  • /etc/cron.monthly
  • /etc/cron.weekly

Thanks to Techmint for much of the above.