Вы находитесь на странице: 1из 9

USINGCRONANDWGETTOHITAPHPSCRIPT

Version1.0PatrickBrunswyck
amanualby

MovingArtStudioa.s.b.l.
Copyright2009MovingArtStudio
GNUFreeDocumentationLicence
(http://www.gnu.org/copyleft/fdl.html)

TableofContents
Usingcronandwgettohitaphpscript................................................................................................3
Whatiscron.....................................................................................................................................3
Cronsyntax......................................................................................................................................3
Thewgetcommand..........................................................................................................................3
UsingcronwithVirtualmin..................................................................................................................4
Createalittletestscript...................................................................................................................4
Creatingascheduledcronjob..........................................................................................................5
RunningPHPcronjobs........................................................................................................................8
Versions................................................................................................................................................9

Usingcronandwgettohitaphpscript
Whatiscron
CronisatimebasedjobschedulerinUnixlikecomputeroperatingsystems.Cronisshortfor
Chronograph.
Cronenablesuserstoschedulejobs(commandsorscripts)torunautomaticallyatacertaintimeor
date.

Cronsyntax
.minute(059)
|.hour(023)
||.dayofmonth(131)
|||.month(112)ORjan,feb,mar,apr...
||||.dayofweek(06)(Sunday=0or7)ORsun,mon,tue,wed,thu,fri,sat
|||||
*****commandtobeexecuted
Moreonthesyntax:http://en.wikipedia.org/wiki/Cron#crontab_syntax

Thewgetcommand
HittingaPHPscriptcanbeachievedbyusingcronandwgetwithnooutput;justhititanddie.To
achievethisputthefollowingin/etc/crontab:
*****wgetqhttp://mysite.be/index.php>/dev/null2>&1
Wget'sqorquietoptionturnsoffwgetsoutputwhichisexactlywhatwewantsincewedonot
intendtogeneratecontentbutonlyhitthePHPscript.
Acommonpracticewhenaddingentriestocrontabistoendtheentrylikethis:
>/dev/null2>&1
Thepurposeofthisistosuppressanyoutputfromthecommanditself,becausewerenotinterested.
Thefirstpart>/dev/null:
MeansredirectSTDOUT(thestandardoutputstream)to/dev/null(whichisbasicallyablackhole
forbits).
Thesecondpart2>&1:
MeansredirectSTDERR(standarderrorstream)tothesameplaceasSTDOUT(whichwasjust
specified).STDOUThastheassignednumber1andSTDERRhastheassignednumber2.
ThiswaybothSTDOUT(1)andSTDERR(2)aredirectedto/dev/nullandalloutputofthe
cronnedcommandissuppressed.

UsingcronwithVirtualmin
Createalittletestscript
IhavecreatedalittlePHPscriptInamedphpcron.php.Thisscriptwillgenerateaverifycron.html
filetoverifythatcronexecutedthescheduledjobasconfigured:
<?php
ob_start();
$datum=date("dmYH:i");
echo"thedateis:$datum";
$page=ob_get_contents();
ob_end_flush();
$fp=fopen("verifycron.html","w");
fwrite($fp,$page);
fclose($fp);
?>
Thisscriptwillcapturealloutputandstoreitintoanewfilecalledverifycron.html.Byusingthe
datefunctionwecanverifytheexacttimetheverifycron.htmlpagewascreated.(youcanofcourse
justcreateasimplefileandcheckthetimestamptoseewhenitwascreated)Thiswaywecanbe
surethecronscheduledjobworks.
Justconfirmthatyourscriptcanwritetothedirectoryyouaregoingtosaveyour(verifycron.html)
filetoandisexecutable(aswithanyscript).
Goal:
Tohavecronhitthispageandmakeitcreateaverifycron.htmlfile(Inthisexampleinthe
directory:/var/www/htdocs/patrick/public).
Usingthisexampleasatestcaseformoreusefulpurposes.
Sointhisexamplethecommandtobeconfiguredincronis:
wgetqhttp://patrick.all2all.org/phpcron.php2>&1
Thiswilhitthephpcron.phppage,executethescriptinitandcreateaverifycron.htmlpagethat
willshowtheexactdateandtimeofcreationandthusconfirmthatcronexecutedmyscriptas
configured.

Creatingascheduledcronjob
GotoVirtualminclickonWebminModulesandthenonScheduledCronJobs

NowclickonCreateanewscheduledcronjob

Nowenterthecommand,scheduleitandthenclickthecreatebutton.

Youcannowseethescheduledjobandothersifcreated.ChecktheactiveboxandclickonEnable
SelectedJobs,itnowwritesthejobtocron.
Inthisexamplethewgetqhttp://patrick.all2all.org/phpcron.php2>&1commandwillberun:
OnTuesdayOctoberthe27that13hoursand47minutes
Crontabsyntax:471327102wgetqhttp://patrick.all2all.org/phpcron.php2>&1
Ishouldnowseeanewlycreatedverifycron.htmlfileinmypublicdirectoryat13h47thankstothe
scheduledjob!
Indeedwhenvisitinghttp:/patrick.all2all.org/verifycron.html,thebyPHPcreatedpageshowsthe
correctdateandtimeforthecronjobwescheduled:

TheVirtualminfilemanagerconfirmsthecorrecttimeanddatetoo:

Notethatyoucantestyourcommandsbeforeconfiguringthemasacronjob.Todosoclickon
VirtualminthenonWebminModulesandfinallyonRunningProcesses:
E.g.IwillenterandrunacommandtocallhelpforthePHPbinary:

Thiscommandresultsin:

RunningPHPcronjobs
Scheduledtasksareacommonfeatureinmodernwebapplications.Fromcleaningoutcachesevery
24hourstocheckingsubscriptionperiodsandevengeneratingreports,morewebapplicationslive
bytheclockthaneverbefore.
YoucancallyourPHPscriptsviacronusingthePHPbinary.Sayyourscriptsareinthe
/var/www/htdocs/mysite/scriptsdirectory.YourPHPbinaryisin/usr/bin/php,Yourcommand
torunyourscriptshouldbethis:
/usr/bin/php/var/www/htdocs/mysite/scripts/runmyscript.php
Iwillusethephpcron.phpfilewiththedatefunctionagaintodemonstratehowtorunaPHPcron
job:
TestyourcommandinVirtualmin=>WebminModules=>RunningProcesses:
/usr/bin/php/var/www/htdocs/patrick/phpcron.php

ItshowsthedatelikeitshouldsoIcan
safelyconfigureitasacronjob,knowing
thePHPsyntaxiscorrect.

CreatetheCronJobforyourcommandasshownhere.

Versions
Versionnumber

Modifications

Author

1.0EN

Originalversion

PatrickBrunswyck

Вам также может понравиться