-
Been a while
Hi guys,
It's been a while. I hope everyone had a great new year. I have been doing some research already, but thought I'd put this out there:
I have a task that I will need to do via a php script every day at midnight (server time)... So, I need to set up a cron job, right? ...To execute the script at that time.
Is there any consideration to doing this that has to be given, in writing code for command line versus the web in php?
I guess I should say, has anyone been down this road and has a few suggestions such as error handling, etc...
Thanks,
CharlesH
-
Hey
Well, the're no big differance if you don't go deep into predefined global arrays like $_SERVER, $_REQUEST etc. May be I forgot something, but it is really almost the same for CLI as for normal Web.
As for the error handling - there is a tricky thing that result of script execution can be send to your e-mail and you will be notified on any errors, just turn on error output. You can also use this for status notify. Anything that normally script outputs to the browser will be sent to you (look at cPanel cron task options), so you can echo/print notification messages and review them next morning.
Good luck!
-
Dmitriy,
That's what I was thinking - no, I won't have to access any superglobals - what I need to do is to go in, look at some dates in a database, determine which users are 5 days away from expiring and then email those users.
Shouldn't be that difficult. I really like your idea about the error logging and the email. I think I have an error class somewhere.
Some of the newer concepts of error logging, such as using try and catch and throwing an exception are kinda new to me, but I've been starting to use that syntax. This will be a good place to do so.
Thx,
CharlesH
-
You're welcome, Charles
Then you will not have any trobules in writing the script. As for the try/catch. Don't use this to often or your code will become x-times bigger (and sometimes slower). Try to use it only when:
- a special action is required for this specific exception, eg. closing connection to the database.
- the original exception should be replaced / wrapped in another, maybe friendlier, exception.
In any other case, no such block is necessary. The generic handler will take care of it. Or your custom handler that will display friendly message and e-mail you on the errors, so you can take care of it.
At least we always deploy our custom error handling, so user will not see filenames and line numbers. However, you don't need it for CLI.
-
As D say, the REQUEST and SERVER globals mostly refer to apache, so they are basically blank. You can use the $_SERVER['argv'] to get parameters passed to the script FROM the command line, say you wanted that 5 days to be different on different crons, or different months....
The other thing you MAY need to be aware of is the set_time_limit defaults to infinite, despite what is in the php.ini so you need to explicitly set it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules