2

I've put two files in the /etc/cron.d/ directory:

The first makes a new post everyday at 12:00AM:

0 0 * * * php /var/www/site1/helper post:make

The second updates the latest post every 10 minutes

10 * * * * php /var/www/site1/helper post:update

Do I have to do something else for this job to run based on the time (eg. every 10 minutes) or do I have to do crontab job1 and crontab job2?

EDIT: I also installed cronie.

4 Answers 4

13

Putting files in cron.d is enough. However, your last entry should be:

*/10 * * * * php /var/www/site1/helper post:update

Otherwise it runs once an hour, at the 10th minute.

1
  • And as mentioned below, add a user. I didn't look closely enough and thought php was your user :)
    – Halfgaar
    Jun 15, 2012 at 12:24
5

Also, add

> /dev/null 2>&1

at the end (after the command) so the cron won't spam you with outputs :)

7
  • how will it spam me with outputs? thanks. Jun 14, 2012 at 10:30
  • 3
    The output of cron jobs goes to the mail box of the user it was running as by default. If you haven't configured your mail server or added something to /etc/aliases, these will end up in /var/spool/mail/<username>. Eventually, they will fill your filesystem... which is bad.
    – Ladadadada
    Jun 14, 2012 at 10:34
  • 1
    Exactly :) It is very interesting when you create a 1 or 2 minute crons and you open your mailbox after 2 days :)
    – mangia
    Jun 14, 2012 at 10:36
  • Oh I didn't know that, It currently has 280 lines. Thank you! Jun 14, 2012 at 10:45
  • 2
    Only put that on the end of the line after you have tested it to ensure it's working correctly, otherwise it won't tell you if there is a problem. Jun 14, 2012 at 16:01
4

The crontabs in /etc/cron.d/ require a username in the 6th field, as does /etc/crontab. User-based crontabs in /var/spool/crontabs/ and via crontab -e do not have a username field.

Halfgaar's answer about the timing of the one that runs every 10 minutes is also correct.

Currently, these cron jobs execute /var/www/site1/helper as the php user rather than executing /usr/bin/php and passing it the arguments /var/www/site1/helper post:make. This will work if your have a php user, the file is executable and has an appropriate shebang. (i.e #!/usr/bin/php as the first line.) Otherwise, it will log an error in /var/log/cron/log.

The example below runs them as the httpd user and has an explicit path to the PHP executable. You should choose an appropriate user.

0     0 * * *  httpd /usr/bin/php /var/www/site1/helper post:make
*/10  * * * *  httpd /usr/bin/php /var/www/site1/helper post:update
2
  • can I run it as a root user instead? Oh and Yeah I checked var/log/cron and it has a line about invalid username. Jun 14, 2012 at 10:31
  • 2
    You certainly can. Choosing a different user is part of the principle of least privilege. If it doesn't require root, don't use root.
    – Ladadadada
    Jun 14, 2012 at 10:32
3

This:

10 * * * * php /var/www/site1/helper post:update

Will not run every 10 minutes, it will run at minute 10 of every hour (00:10, 01:10, ...).

This will run every 10 minutes:

*/10 * * * * php /var/www/site1/helper post:update
0

You must log in to answer this question.

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