0

I have a problem with cron jobs, I need to send email notification to users, I use this command

curl "http://example.com/index.php?option=com_community&task=cron">/dev/null 2>&1

When I run this command in shell it works, and it sends emails, but when I'm trying to set cron job it doesn't send emails:

Here is my /etc/cron.d :

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
05,45 * * * * root run-parts /etc/cron.hourly

It is suposed to run /etc/cron.hourly cron job, here is /etc/cron.hourly/0anacron :

#!/bin/bash
# Skip excecution unless the date has changed from the previous run
if test -r /var/spool/anacron/cron.daily; then
    day=`cat /var/spool/anacron/cron.daily`
fi
if [ `date +%Y%m%d` = "$day" ]; then
    exit 0;
fi

# Skip excecution unless AC powered
if test -x /usr/bin/on_ac_power; then
    /usr/bin/on_ac_power &> /dev/null
   if test $? -eq 1; then
    exit 0
     fi
fi
/usr/sbin/anacron -s
curl "http://example.com/index.php?option=com_community&task=cron">/dev/null 2>&1

Here is cron log, it shows that cron job is running

Aug  2 16:45:01 u17669867 CROND[3164]: (root) CMD (run-parts /etc/cron.hourly)
Aug  2 16:45:01 u17669867 run-parts(/etc/cron.hourly)[3164]: starting 0anacron
Aug  2 16:45:01 u17669867 run-parts(/etc/cron.hourly)[3173]: finished 0anacron

Maybe, there is something wrong in /etc/cron.hourly/0anacron ?

Reg Edit
  • 244
  • 2
  • 11

1 Answers1

0

Did you append your code directly into the 0anacron file??

That's what's wrong here; don't do that.

check the man page for anacron:

Anacron can be used to execute commands periodically, with a frequency specified in days. Unlike cron(8), it does not assume that the machine is running continuously.

Clearly, this is not related to what you are having in mind with your emails, and if you check the script, you can see that there's a good chance it script would've exited 0 before coming to your task.

Anyway, try putting your

curl "http://example.com/index.php?option=com_community&task=cron">/dev/null 2>&1

Inside a file named something like /etc/cron.hourly/1send_emails. Chown it to root and make executable.

The file will then be run on an hourly basis and it should be logged in your cron log in a similar fashion to the anacron job.

If you want to specify your own cron expression, or run as different user than root, put a similar file with your line prefixed with cron expression and user to run as before the job to run in /etc/cron.d instead.

Petter H
  • 3,443
  • 1
  • 16
  • 19