How to configure cron task?

I have never used CRON before, but I want to use CRON to be able to do schedule tasks for php script. The PHP script is called "inactivesession.php", and in PHP the script is code:

<?php include('connect.php'); $createDate = mktime(0,0,0,10,25,date("Y")); $selectedDate = date('dm-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(); ?> 

What I want to do is that when the above date is reached (October 25th), I want the php script to execute the UPDATE statement above. But my question is: how to use CRON for this?

The server I use is a university server known as helios, do I need to configure CRON in helios (do I need to call an administrator for this) or is it something else using CRON.

I have never used CRON before, so can you explain to me how CRON can be configured for the example above with the server I'm using?

thanks

+7
source share
1 answer

Method 1: Run a script using php from crontab

Just as you call your shell script (as shown in our crontab 15 example article), use the php executable and call the PHP script from your crontab, as shown below.

To run myscript.php every 1 hour, follow these steps:

crontab -e

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

Method 2. Run php script using url from crontab

If your php script can be invoked using a url, you can use lynx or curl or wget to configure your crontab as shown below.

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

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

The following script runs a php script (every 5 minutes), invoking the URL using CURL. Curl displays output in standard output by default. Using the "curl -o" parameter, you can also output 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 runs a php script (every 10 minutes), invoking the URL using WGET. The -q option indicates pretty mode. "-O temp.txt" indicates that the output will be sent to a temporary file.

 */10 * * * * /usr/bin/wget -q -O temp.txt http://www.thegeekstuff.com/myscript.php 
+3
source

All Articles