Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Cron jobs in one crontab file

I wanted to implement two cronjobs with different execution time. One cron job is for sending emails and second cron job for validating my application subscriptions.

I write one crontab file and write to two cronjob as follows:

2 * * * * path to mailCronjob mail.php
20 * * * * path to check my application's subscriptions sub.php

The problem is first cronjob is working fine. Mail will delivers fine, but the second cronjob is not working. I tried to run second job manually, its also working fine.

I am using command to set cronjob as:

crontab crontab_file

when I give command crontab -l it also shows both cronjob in command line.

I wanted to ask, am I missing something here, or what should I do to run those cronjobs.

like image 388
ashutosh Avatar asked Nov 07 '12 20:11

ashutosh


4 Answers

FACT: you can run as many cron jobs from a single crontab file as you wish.

FACT: you can also run different jobs as different users, each with their own crontab file.

SUGGESTION:

1) Just debug what's wrong with your second job.

2) It could be path, it could be permissions; it's more than likely environment (the environment for "cron" can be different from the environment for the same user from a command line).

PS:

Try this, too:

  • How to simulate the environment cron executes a script with?

  • Debugging crontab jobs

like image 52
paulsm4 Avatar answered Sep 25 '22 22:09

paulsm4


Check the owning user's email and see if an error report has been sent to it.

If you need to be a certain user and have that user's environment change your call to

su - -c "/path/to/sub.php" SubScriptUser

If your script only works from a certain directory use

cd /path/to/ && ./sub.php
like image 24
Erik Nedwidek Avatar answered Sep 24 '22 22:09

Erik Nedwidek


I recall the same problem. For some reason, I had to press enter after my second cron job. Even in the first cron job, if it is the only job, needs a CR/LF (enter). Cursor needs to be on second line (if there is one cron job)... cursor needs to be on the third line, if there is two cron jobs. I guess, needs to read the file completely to interpret the cron job command. I just share this, because if you dont do that, only the first job will be executed, and skips the second one totally unless enter is pressed at the end of the second job. Best regards and cheers... Test that and let us know.

like image 21
Luis H Cabrejo Avatar answered Sep 25 '22 22:09

Luis H Cabrejo


You need to add an empty line to the end of the config file

like image 36
Rushaker Avatar answered Sep 24 '22 22:09

Rushaker