0% found this document useful (0 votes)
66 views9 pages

Using Cron and Wget To Hit A PHP Script: Version 1.0 Patrick Brunswyck A Manual by

Manual Cron

Uploaded by

shashi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views9 pages

Using Cron and Wget To Hit A PHP Script: Version 1.0 Patrick Brunswyck A Manual by

Manual Cron

Uploaded by

shashi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 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

You might also like