Skip to content

Instantly share code, notes, and snippets.

@sonalranjit
Last active January 21, 2017 21:34
Show Gist options
  • Save sonalranjit/37cc43607e261671d34f361786ff79c3 to your computer and use it in GitHub Desktop.
Save sonalranjit/37cc43607e261671d34f361786ff79c3 to your computer and use it in GitHub Desktop.
How to do crons

Installing a new cron

  1. type crontab -e to open the crontab file, this is where you add a task for cron to perform.
  2. have to specify the path to all the functions in the terminal, cronjob by default won’t recognize any function that you type, example if you create a cron task that will run a php script like: php phpscript.php it will not recognize the function php unless you specify the path.
  3. to specify all the function paths on the machine to for cron you have to add this line in the top of the crontab -e file. SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  4. first 5 elements in a cron task line is to define the time to run the task.
  5. format: * * * * * sh script.sh
  6. The first five fields are: [minute 0-59] [hour 0-23] [day of the month 1-31] [month of the year 1-12] [day of the week 0-6 with 0 = Sunday, 1= Monday] http://techsk.blogspot.ca/2008/06/how-to-run-cronjob-on-last-friday-of.html

Cronjob to run a shell script that will send email

  • * * * * * * sh /home/sonal/cronjob/cron_email.sh The shell script just contains a sendmail command that will send an email with a text files as the body.

    #!/usr/bin/env bash

    sendmail sonal.ranjit3@gmail.com < "/home/sonal/cronjob/test.txt"

  • The five stars for the time field will run this shell script every minute for every hour of the month for every month of the year and everyday of the week.

Cronjob code to run the parcel address scripts and reminders

  • 00 01 * * 5 [ $(date +"\%m") -ne $(date -d 7days +"\%m") ] && sh /var/lib/postgresql/cronjob/cron_email.sh
  • The first crontab is set to send a reminder email every last friday of the month listing all the files that are present on the folder system and to remind to download the york region data.

Escaping double quotes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment