hoangweb
4/13/2018 - 12:30 AM

php - cron

<?php
/*
use for cpanel
- create a table: “tasklog” table.
create table tasklog (
  id int not null auto_increment,
  created datetime,
  primary key (id)
)
*/
/** 
 * simple code to add job: public_html/taskrun.php
 */
try {
  $host = "localhost";
  $dbname = "your_database";
  $user = "your_db_user";
  $pass = "your_password";

  # MySQL with PDO_MYSQL
  $db = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
  $db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

  $stmt = $db->prepare("INSERT INTO tasklog(`created`) VALUES(NOW())");
  $stmt->execute();

  $db = null;
}
catch(PDOException $e) {
    echo $e->getMessage();
}

/**
 * Register cron job
*/
// in cpanel create new job with a one minute frequency.
/usr/local/bin/php "$HOME/jobs/taskrun.php" > /dev/null 2>&1