Results 1 to 5 of 5

Thread: Been a while

  1. #1
    charlesh's Avatar
    charlesh is offline Master Glow Jedi
    Join Date
    Aug 2006
    Location
    Atlanta, GA - better than you imagined it would be.
    Posts
    189

    Default 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

  2. #2
    Dmitriy is offline Nearly a Master Glow Jedi
    Join Date
    Feb 2007
    Location
    Ukraine
    Posts
    124

    Default

    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!

  3. #3
    charlesh's Avatar
    charlesh is offline Master Glow Jedi
    Join Date
    Aug 2006
    Location
    Atlanta, GA - better than you imagined it would be.
    Posts
    189

    Default

    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

  4. #4
    Dmitriy is offline Nearly a Master Glow Jedi
    Join Date
    Feb 2007
    Location
    Ukraine
    Posts
    124

    Default

    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.

  5. #5
    jmarcv's Avatar
    jmarcv is offline Cranky Coder
    Join Date
    Jan 2005
    Posts
    354

    Default

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16