6

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.

ashutosh
  • 759
  • 5
  • 13
  • 34

5 Answers5

10

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:

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190
2

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
Erik Nedwidek
  • 6,134
  • 1
  • 25
  • 25
1

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.

Luis H Cabrejo
  • 302
  • 1
  • 8
1

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

Rushaker
  • 11
  • 1
0

I never did 2 actuall cronjobs in one cron-tab file, but rather had the one cronjob execute every 15 minutes and query the database or look into a config file what tasks are there to execute, maybe this concept helps you.

Vengarioth
  • 684
  • 5
  • 13
  • basically my two cron jobs works very differnently, one is used for sending mails, and second one is for checking my application subscriptions for every 15 min, there is lot of code, and thats why I dont wnat to mess with it.;) – ashutosh Nov 07 '12 at 20:26
  • I did simmilar things with my crontab but i found out the resources beeing initialized (lots of users) where mostly the same, so i merged this into one process so when both scripts startet the same time i only had to initialize all the resources once, saved a lot of execution time back then :) – Vengarioth Nov 07 '12 at 20:27
  • I did simmilar things with my crontab but i found out the resources beeing initialized (lots of users) where mostly the same, so i merged this into one process so when both scripts startet the same time i only had to initialize all the resources once, saved a lot of execution time back then :) – Vengarioth Nov 07 '12 at 20:27
  • @paulsm4 : but perhaps, I am not able to do that, will u more elaborate? – ashutosh Nov 07 '12 at 20:29
  • 2
    @ashutosh - what you did is perfectly OK. Two lines in your crontab should mean that two jobs get executed. Period. Whatever's going wrong with the second job is "something else". You must debug what's wrong with the job itself. – paulsm4 Nov 07 '12 at 20:34
  • So if you have a weekly job and a daily job you merge them into one process? That doesn't seem right. What if you have a job that you want to run in the morning and another to run at night? Our crontab files often have dozens of different jobs. If you can do everything in one job, your business must have very simple needs. – Barmar Nov 07 '12 at 20:46