Jump to content

Scheduling via PHP


Jesdisciple

Recommended Posts

Is it possible to set up a cron job via PHP? If so, is it portable across systems or would some kind of platform detection be required for portability? Thanks.

Link to comment
Share on other sites

Cron is the name of a utility on Unix-based systems. It has a command-line syntax for scheduling events, you can check the man pages for cron to see about that. The scheduling utility on the Windows command line is called "at". You can use the PHP function exec to run shell commands.

Link to comment
Share on other sites

So it is limited to command-line stuff. (I know "cron job" is incorrect otherwise, but that's what came to mind.) Can PHP access the command line?And I read that it's at for Win2k, schtasks for other Windows versions. Is that right?

Link to comment
Share on other sites

So how can I portably detect which OS I'm running on? phpinfo() tells me that my $_ENV['OS'] is "Windows_NT" but Wikipedia doesn't list "OS" as a default environment variable on Windows XP (which is what I have). Can I trust "OS" as cross-platform or even cross-Windows?

Link to comment
Share on other sites

In addition, you can also use proc_open() if you want finer control over what gets executed (and if you want to fetch errors and react on them).For OS detection, I think checking up the DIRECTORY_SEPARATOR constant works best. I mean, why would you want to check the OS? Isn't it because of slashes in paths? The DIRECTORY_SEPARATOR constant shows you the kind of slash you need to use, so it's pretty safe to assume that

if (DIRECTORY_SEPARATOR === '\') {echo 'OS is a version of Windows';}else {echo 'OS is NOT a version of Windows';}

Link to comment
Share on other sites

In addition, you can also use proc_open() if you want finer control over what gets executed (and if you want to fetch errors and react on them).
To do error-handling in this section of my project, I think I need a group of people with diverse OSs to test for me...
For OS detection, I think checking up the DIRECTORY_SEPARATOR constant works best. I mean, why would you want to check the OS? Isn't it because of slashes in paths?
Well, not exactly. In Unix, Linux, and Mac (or at least Mac OS X), 'cron' is the command-line tool for scheduling stuff. In Windows, it's either 'at' or 'schtask' - I think 'at' is only for Win2k, not sure yet.
The DIRECTORY_SEPARATOR constant shows you the kind of slash you need to use, so it's pretty safe to assume that
if (DIRECTORY_SEPARATOR === '\') {echo 'OS is a version of Windows';}else {echo 'OS is NOT a version of Windows';}

I think I'm going to need something more precise than that, specifically for Mac OS <10 and Windows 2000.jsg, how do I find out that I'm using Windows XP (since that's all I can test on)? And would a Mac OS X return "darwin"?
Link to comment
Share on other sites

Check the link above to php_uname, there is an example there to detect Windows. I think both Windows 2000 and XP will both return WINNT though for the PHP_OS constant. The full uname string might be more useful (i.e. check for both "win" and "xp" or "win" and "2000"). You'll definately want to do some testing though. I think you can search for "mac" to find MacOS versions. If I run this script here:

<?phpecho php_uname() . "<br>";echo PHP_OS;?>

I get this output:Windows NT WWW 5.2 build 3790WINNTThis is for Server 2003, the hostname is WWW. You might have to test for the version number. 5.2 is 2003, I believe that 5.1 is XP and 5.0 is 2000. Vista is 6.0. You can also have uname leave out the hostname so you can just check for "Windows NT 5.0" at the beginning of the string or something.

Link to comment
Share on other sites

OK, I've been reading up a lot on this, and here's what I've found. Because of all the URLs, I must use bracket citations to tell where I got each piece of info, or else my paragraphs will get all cluttered and illegible. I'll try to give enough info to help someone who searches the archives later.Apparently, all NT systems have the AT command, but Microsoft only provides an online guide to it "in Windows 2000"[1]. All other NT systems (except NT itself and 2000[2]) supposedly have SCHTASKS in addition to AT. But my copy of XP Home Edition must be defective because it only has AT. (And help for the command is only available by "schtasks /?" in my XP Professional.) Additionally, several parameters are unavailable in XP and 2003[3], but Microsoft has a How-To article for Server 2003[4].So since support for SCHTASKS is ambiguous, should I simply test for Windows vs. *NIX then the supported commands inside each of those? (All Mac versions support cron jobs according to a fairly reliable source.) For example (in pseudo-code)...

IF Windows	IF SCHTASKS		//use SCHTASKS	ELSE IF AT		//use ATELSE IF CRONTAB //(or Linux/Mac/UNIX)	//use CRONTABELSE	//system unsupported

How should I test for AT/SCHTASKS and/or CRONTABS? Just call them and see if I get any error output?1 - http://support.microsoft.com/kb/3135652 - http://www.windowsnetworking.com/kbase/Win...acesAT.exe.html3 - http://msdn2.microsoft.com/en-us/library/b...357(VS.85).aspx4 - http://support.microsoft.com/kb/814596unused but useful5 - http://www.opengroup.org/onlinepubs/007908...cu/crontab.html6 - http://www.opengroup.org/onlinepubs/007908775/xcu/at.html7 - http://technet.microsoft.com/en-us/library/bb490996.aspx

Link to comment
Share on other sites

You could use a shell command to try to run them and use a regular expression to test the output for something like "unknown command" or something like that. But if you're saying that all NT systems support AT then I would just use that for Windows, or if it's Linux then use cron.

Link to comment
Share on other sites

I've decided to use exec() so I can react to error output, and now I'm puzzling over how to construct my commands. I'm wanting to schedule for a certain day (e.g. 15 days from now) then have the command repeat every X, where X could be either 7 days, 2 years, 3 months, etc. Or is this not possible?

Link to comment
Share on other sites

Yeah, it should be possible. It looks like you can set something like that up through the GUI for scheduled tasks in Windows, so it has to be possible through the command line. It sounds like 2 tasks though, one to run at a specific time and date and another one to repeat. There should be command line references for these tools online.

Link to comment
Share on other sites

In WinNT, I need to use a one-time AT ... NEXT to call a recurring AT ... EVERY. Both NEXT and EVERY take an argument that

Specifies the date when you want to run the command. You can specify one or more days of the week (that is, type M,T,W,Th,F,S,Su) or one or more days of the month (that is, type 1 through 31). Separate multiple date entries with commas. If you omit Date, at uses the current day of the month.
So I can't specify an amount of units, but only a very precise unit, and AT will make my command coincide with the next occurrence of that unit.In case an NT guru is reading this, is this the right syntax (expressed in XML)? The PHP will locate the "Windows NT" schedule-command, get its parameter names, extract the CDATA, and preg_replace() the %-enclosed names with their corresponding values.
<scheduleCommand systems="Windows%20NT" parameters="hour0 minute0 day0 hour1 minute1 day1 command"><![CDATA[	at %hour0%:%minute0% /next:%day0% at %hours1%:%minutes1% /interactive /every:%day1% %command%]]></scheduleCommand>

References:http://support.microsoft.com/kb/313565http://technet2.microsoft.com/windowsserve...3.mspx?mfr=trueIn Unix-like systems, I need to use a one-time AT to call a recurring CRONTAB. I think I understand AT (pass date and time in one command, and the scheduled command next?), but CRONTAB has me stumped. 'crontab -e' opens the current user's "cron table" in the vi text editor, but how do I submit a cron job by the command line? Here's what I have so far...

<scheduleCommand systems="Darwin SunOS Linux IRIX FreeBSD Mach" parameters="hour minute date"><![CDATA[	at %hour%:%minute% %date%	crontab -e]]></scheduleCommand>

References:http://www.hccfl.edu/pollock/Unix/AtDemo.htmhttp://www.debianhelp.co.uk/schedulejobs.htmhttp://www.ibm.com/developerworks/library/...scheduling.html

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...