Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

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/

share|improve this answer
    
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 '13 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 '13 at 14:49
    
Unfortunately not, but I've worked around it (see my answer). – jgillich Aug 6 '13 at 9:45
up vote 1 down vote accepted

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.

share|improve this answer

You must log in to answer this question.

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