简体   繁体   中英

Best practices in scheduling PHP scripts

For a project I need to schedule several PHP scripts. Some of the scripts are a bit complicated (ie. send mail, do database stuff, use several external webservices, use external files and classes etc). The problem is that these scripts also need to be called from the browser, and they do use some of the variables in the $_SERVER array (including HTTP_HOST ).
Is there a way to make these kind of scripts work from the commandline, or do I have to rewrite them to be 'commandline-compatible'? Or is it better to use parameters from the commandline that specify the variables that are not available?

We have a server running Windows Server 2008.

You should abstract the part of script that is common to browser and command line environment. Then you can include it into two scripts, one of which will use $_SERVER, another one command line arguments variables to run the common part.

Best practices in scheduling PHP scripts?

1) don't. It's a slippery slope - you end up spending far too much time trying to organise the scripts into available time slots, you end up with a system which changes state depending on which jobs have been run and haven't. Really you want all processing triggered directly in response to user requests. (that doesn't mean that they have to work synchronously and in the same execution thread as the user request though).

2) don't do it on MSWindows - Microsoft operating systems are OK for providing GUIs but do not have the range of tools available for job management and automation available on other Operating Systems

I've seen a lot of cases where people shove stuff into scheduled batches simply because the performance of their code sucks. In a lot of cases a better solution would be to fix the performance problem.

Having said that, since you're already in a hole, yes, you could duplicate all your code into command line and web versions - or you could download wget or cURL and schedule these programs to invoke your URLs passing the required values via GET or POST

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM