I am installing new crontab job which will execute a shell script and then send the printed errors on the console to email. I know all of the required steps to do, which will be as following:

 0 10,22 * * * . /X.sh 2>&1 >/dev/null | mail -s "subject" "email"

Mu question is: when will it send the email?

  1. it will send an email each 12 hours even if shell script finished before the next round (I mean the next new cron job).
  2. it will send an email after finish executing from shell script even the duration of the current cron is still active.

The email will be sent every time X.sh finishes running, whenever that is.

cron itself will send a job's output by email by default, to the relevant crontab owner or whatever MAILTO is set to; you could use that instead of manually calling mail.

  • Thanks, yes I received an email earlier without waiting to finish the period of 12 hours – H.H Jun 2 '16 at 20:17

It's the second. cron will send an e-mail when the job completes regardless of when it is. You can check this yourself by setting a crontab like this:

*/5 * * * * sleep 600; date

You'll get an e-mail 10 minutes later every 5 minutes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.