Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you set up cron task? [closed]

Tags:

cron

I have never used CRON before but I want to use CRON in order to be able to perform schedule jobs for a php script. The php script is called "inactivesession.php" and in the php script is this code:

<?php

include('connect.php');


$createDate = mktime(0,0,0,10,25,date("Y"));
$selectedDate =  date('d-m-Y', ($createDate));

$sql = "UPDATE Session SET Active = ? WHERE DATE_FORMAT(SessionDate,'%Y-%m-%d' ) <= ?";                                         
$update = $mysqli->prepare($sql);
$update->bind_param("is", 0, $selectedDate);
$update->execute();

?>

Wht I want to do is that when the above date is reached (25th Oct), I want the php script to perform the UPDATE statement above. But my question is that how do I use CRON in order to do this?

The server I am using is the university's server known as helios, does CRON need to be set up in helios, (do I have to call the admin for this) or is it something else which uses CRON.

I have never used CRON before so can you explain to me how CRON can be set up for the example above with the server I am using?

Thanks

like image 850
user1723760 Avatar asked Oct 24 '12 20:10

user1723760


1 Answers

Method 1: Execute the script using php from the crontab

Just like how you call your shell script (As show in our crontab 15 examples article), use the php executable, and call the php script from your crontab as shown below.

To execute myscript.php every 1 hour do the following:

crontab -e

00 * * * * /usr/local/bin/php /home/john/myscript.php

Method 2: Run the php script using URL from the crontab

If your php script can be invoked using an URL, you can lynx, or curl, or wget to setup your crontab as shown below.

The following script executes the php script (every hour) by calling the URL using the lynx text browser. Lynx text browser by default opens a URL in the interactive mode. However, as shown below, the -dump option in lynx command, dumps the output of the URL to the standard output.

00 * * * * lynx -dump http://www.thegeekstuff.com/myscript.php

The following script executes the php script (every 5 minutes) by calling the URL using CURL. Curl by default displays the output in the standard output. Using the “curl -o” option, you can also dump the output of your script to a temporary file as shown below.

*/5 * * * * /usr/bin/curl -o temp.txt http://www.thegeekstuff.com/myscript.php

The following script executes the php script (every 10 minutes) by calling the URL using WGET. The -q option indicates quite mode. The “-O temp.txt” indicates that the output will be send to the temporary file.

*/10 * * * * /usr/bin/wget -q -O temp.txt http://www.thegeekstuff.com/myscript.php
like image 117
Rush Avatar answered Oct 14 '22 16:10

Rush