2
<?php
    ini_set('display_errors','1'); 
    error_reporting(E_ALL);
    include_once 'dbConnect.php';

    $startdate = trim($_POST['startdate']);
    $enddate = trim($_POST['enddate']);


    if (connect()){
        global $conn;       

        $query="SELECT electionNo FROM election ORDER BY electionNo DESC LIMIT 1";
        $details = $conn->query($query);

        while ($rows = $details->fetch_assoc())
            $election = $rows['electionNo'];

        $election=$election+1;

        $liststart= explode("T",$startdate);            
        $listend= explode("T",$enddate);    

        $start=$liststart[0]." ".$liststart[1];
        $end=$listend[0]." ".$listend[1];

        $year = substr($listend[0],0,4);

        $insertquery = "INSERT INTO election(electionNo,year,startTime,endtime) VALUES('$election','$year','$start','$end')";

        $insert = $conn->query($insertquery);
        if ($insert)
            echo 'Registered Successfully';     
        else
            echo 'No good';     
    }               
?>

I want to pass $start to a Cron Job to schedule the job. For example, if $start = '2018-03-20 12:00:00', the Cron Job should be as follows:

00 12 20 03 * php /home/Dropbox/WebServer/paramGen.php

Which means paramGen.php has to be run at 12:00 on 2018-03-20.

Is this possible and if so, how do I pass $start to the cron job?

godot
  • 3,422
  • 6
  • 25
  • 42
Mag
  • 89
  • 1
  • 11
  • http://php.net/manual/en/reserved.variables.argv.php – Twinfriends Mar 21 '18 at 08:03
  • Possible duplicate of [How to create cron job using PHP?](https://stackoverflow.com/questions/18737407/how-to-create-cron-job-using-php) – godot Mar 21 '18 at 08:07
  • @godot I believe my question is different from the one you suggested. – Mag Mar 21 '18 at 08:12
  • 1
    I know this won't help you with your current problem but I'd suggest to have ONE script run every minute that handles your jobs "internally" – brombeer Mar 21 '18 at 08:20

1 Answers1

1

Here is how to construct the cron command :

$start = '2018-03-20 12:00:00';
  $date=strtotime($start);
  $str= date('i',$date)." ".date('H',$date)." ".date('d',$date)." ".date('m',$date)." \* php /home/Dropbox/WebServer/paramGen.php";

then

exec("echo $str >> cronfile")

hope this helps

user10089632
  • 5,216
  • 1
  • 26
  • 34
  • Important, before you test my command make sure to look at the update, it contains `>>` appending symbol, instead of overriding symbol `>` – user10089632 Mar 21 '18 at 08:31
  • 1
    `$str` gives the correct echo. However, what is inserted in the `cronfile` is completely different: `00 12 20 03 BlankVote.class BlankVote.java..` That is, it is listing all the files in the `WebServer` directory – Mag Mar 21 '18 at 08:54
  • 1
    @Mag, I think you need to escape the asterisks `*` as in the updated answer – user10089632 Mar 21 '18 at 09:05
  • I'm sorry, I am new to this. Can you tell me what commands to use to run `cronfile` as a cron job? – Mag Mar 21 '18 at 09:24
  • in fact cronfile here is not to be used literally, you must replace it with the path to your cron file followed by the name of your cronfile , which is usually at `/var/spool/cron/crontabs/crotabfile`, if you haven't created any cronjobs at all look for the documentation of your linux distribution, for the PHP part look at my answer, if you have question about how to create a cron job consider asking a question in askubuntu.com, or unix.stackexchange.com, good luck. – user10089632 Mar 21 '18 at 09:48