Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

2 Answers 2

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
add comment
up vote 0 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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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