PHP & Cron - Basic Questions 4 Newbie

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
maple_toast
Forum Newbie
Posts: 2
Joined: Wed Aug 10, 2011 2:21 pm

PHP & Cron - Basic Questions 4 Newbie

Post by maple_toast »

So I've been learning PHP for about a year now. I've built several small and a few bigger applications. Now I'm on an applications which requires cron. Suffice it to say, it's kicking my arse. :evil:

For those of you who are experienced, perhaps you can help answer some of my questions...

1) When I run the script directly by calling it in the browser, there are not any errors and it completes in about 2 min 40 seconds. Yet when I call it in cron, it can take as much as an hour to complete, and sometimes it hangs.

2) I've been using two different commands: /usr/bin/php -q /path/to/script and wget -q http :// direct path to script .php . Is there a better way to do this?

3) I've duplicated the script into different filenames, such as fileA.php, and fileB.php. If I want to do twice the work, would it make sense to cron fileA.php and cron fileB.php?

4) Am I correct in assuming that cron will run my php script the same way that a web browser would?

Thanks for your help. It's much appreciated!

M.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP & Cron - Basic Questions 4 Newbie

Post by Celauran »

1. Without seeing the code, I wouldn't even hazard a guess.
2. Calling PHP directly from the CLI should be more efficient as it removes the additional overhead of having to call Apache.
3. Why? How do you mean by 'doing twice the work'?
4. Yes.
maple_toast
Forum Newbie
Posts: 2
Joined: Wed Aug 10, 2011 2:21 pm

Re: PHP & Cron - Basic Questions 4 Newbie

Post by maple_toast »

The purpose of the cron is to run user-submitted "jobs". Since I have the cron set to run once a minute, I figured that running two processes would do twice the work. That's why I was testing out a fileA.php and fileB.php system.

I'm still confused as to why the cron is so inconsistent on its running time. Running it in the browser still gets around 2 min 40 sec. In cron, it can run 3min to as much as 10 hours now.

Since the process takes 2 min 40 seconds (when calling it in a browser) I set the cron to run every 5 minutes. Does the cron need to be finished with a script before the next cron runs, if it's going to call the same script?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP & Cron - Basic Questions 4 Newbie

Post by Celauran »

Out of curiosity, have you compared execution time for CLI vs. wget?
Post Reply