0

I use cron to execute some php scripts (owncloud background tasks etc). My server runs nginx and PHP via php-fpm, which by default runs PHP processes under the apache user, the only user with write access to /var/www aside from root. Thus it should be fine to add these background jobs to apache`s crontab:

crontab -u apache -e

The contents:

MAILTO=cron@localhost
*  *  *  *  * echo "..." | mail -s "crontest" cron@localhost

This should send me a mail every minute, however it does not. When I run the command as the apache user (sudo -s -u apache), it works fine, but the cronjob never executes.

I found some mention of "locked accounts" here, but I'm not sure if the suggested solution is a good idea.

2 Answers 2

1

I don't believe that the number 60 is valid in the minutes column (someone correct me if I'm wrong). The number should be between 0 and 59.

What you're actually telling the machine to do is "run echo "..." | mail -s "crontest" cron@localhost every 60 minutes". If you want the job to run every minute, the line would be:

*/1 * * * * echo "..." | mail -s "crontest" cron@localhost

http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/

3
  • You are right, the syntax is incorrect. But I put that in there just for testing, now with a valid syntax it doesn't work either.
    – jgillich
    Aug 5, 2013 at 14:17
  • Maybe it's a $PATH issue? Try specifying the full path to mail and echo in the crontab entry.
    – arochette
    Aug 5, 2013 at 14:49
  • Unfortunately not, but I've worked around it (see my answer).
    – jgillich
    Aug 6, 2013 at 9:45
1

Ok, /var/log/cron gave me a clue.

CROND[7709]: (apache) CMD (echo "..." | mail -s "crontest" cron@localhost)
CROND[7709]: (CRON) ERROR chdir failed (/usr/share/httpd): No such file or directory

The issue is that cron tries to switch to the user's home directory before executing anything. However, as I didn't have apache installed, /usr/share/httpd didn't exist. I guess creating that directory would work, but using sudo as root should be the better solution here.

* * * * * sudo -u apache env TERM=xterm php -f /var/www/owncloud/cron.php

env TERM=xterm avoids a warning from cron.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .