Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

PHP 

[PHP] Cron Job Dynamic Inputs

Hello everyone , 😉
I need to make a user interface that can directly give input to file name "crontab" (without any extension)
Here is frontend i made :
Time in Hours Minutes Seconds:
Spoiler (Click to View)

Jquery Date Selector
Spoiler (Click to View)

form
Code
<form id="settingsForm" action="schedule_engine.php" method="post">

<div class="form-group">
    <label for="source">Source</label>
    <div class="input-group">
      <span class="input-group-addon"><span class="glyphicon fa  fa-info"></span></span>
      <select name="source" id="source" style="width:130px;"  class="form-control">
      <option value='9gag_new'>9gag_new</option><option value='fresh'>fresh</option><option value='hot'>hot</option><option value='main'>main</option><option value='trending'>trending</option><option value='videos'>videos</option>      </select>
    </div>
    </div>

<div class="form-group">
<label for="next_launch">Starting From</label>
<input style="width:160px;" name="next_launch" class="form-control" id="datepicker"></input>
</div>

<div class="form-group">
    <label for="day_interval">Time Between Launches <i>(Days, Hours, Minutes)</i></label>
    <div class="input-group" id="time_intervals">
      <span class="input-group-addon"><span class="glyphicon fa fa-info"></span></span>
      <input class="form-control" style="width:130px;" name="day_interval" id="day_interval" defaultValue="Days"></input>
      <input class="form-control" style="width:130px; margin-left:5px;" name="hour_interval" id="hour_interval" defaultValue="Hours"></input>
      <input class="form-control" style="width:130px; margin-left:5px;" name="minute_interval" id="minute_interval" defaultValue="Minutes"></input>
    </div>
</div>

<div class="form-group">
<label for="posts">Number of Posts to Scrape at Once</label>
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon fa  fa-info"></span></span>
<input type="text" name="posts" style="width:130px; margin-left:5px;" id="posts" placeholder="Number of Posts to Fetch">
</div>
</div>

<div class="form-group">
    <label for="source">Mode</label>
    <div class="input-group">
      <span class="input-group-addon"><span class="glyphicon fa  fa-info"></span></span>
      <select name="status" id="status" style="width:130px;" class="form-control">
      <option value="0">Manual</option>
      <option value="1">Automatic</option>
      </select>
    </div>

</div>[b]<!--[/b] panel body -->

<div class="panel-footer clearfix">

<button type="submit" id="submitButton" class="btn btn-default btn-success btn-lg pull-right">Update Site Settings</button>

</div><!--panel-footer clearfix-->

</form>
i need to know how to input exact value in crontab to make it working

here is my current code :
Code
$next_launch = '20'.$_POST['next_launch'].':00';
        $next_launch = strtotime($next_launch);
        $time_interval = intval($_POST['day_interval']) * 3600 * 24 + intval($_POST['hour_interval']) * 3600 + intval($_POST['minute_interval']) * 60;
        if($next_launch < time())
            $next_launch = time() + $time_interval;
        $date_str = date('i H d m',$next_launch);
        $file = file_get_contents('../crontab');
        $next_launch += $time_interval;
        $date_str = round(date('s i H d m Y',$next_launch));
        $source = $_POST['source'];
        $mysqli->query("update jobs set next_launch = \"$date_str\", time_interval = $time_interval, posts = $posts where name=\"{$_POST['source']}\"");

        $file .= $date_str;
        $str = "$file * /usr/local/bin/php ".PATH_TO_ENGINE."scraping_engine/{$_POST['source']}.php";
        file_put_contents('../crontab',"MAILTO=\"\"\n$str\n");
        exec('crontab crontab');

i need accurate value like this :

Code
1 1 1 * * execute.php

please help me 🤝
still looking for help regarding this , anyone here ?
Can you just show what the output of the data from the form (print_r($_POST)) ? Then it'll be very easy to format the data to match with the crontab file.
here is crontab file that is dynamically updated with php
Code
00 20 26 11 * /home4/dream45s/public_html/viral1/hot.php
can u help me with this ?
I wasn't talking about this but about the output of your form.
when we do print_r() its something like this
Code
Array ( [source] => 9gag_new [next_launch] => 11/27/2014 00:00 [day_interval] => 1 [hour_interval] => 0 [minute_interval] => 0 [posts] => 20 [status] => 1 )
The "start from" is the main part that fucked your script and make it hard. Do you really need it ?
(Nov 26, 2014 02:15 PM)Sakuto Wrote:  The "start from" is the main part that fucked your script and make it hard. Do you really need it ?

i can re code everything
can you please give me idea ?
The problem is not from the code, the problem is from the CRON, you can enter value to repeat, but you can't specify a date to start the CRON. So you'll need to use an another CRON Task who will add the perpetual CRON Task to the list, and delete the last one.

Do you understand or I'm not clear ?
PS : Don't PM me each time you answer, I come watch every subjet...
(Nov 27, 2014 06:13 AM)Sakuto Wrote:  The problem is not from the code, the problem is from the CRON, you can enter value to repeat, but you can't specify a date to start the CRON. So you'll need to use an another CRON Task who will add the perpetual CRON Task to the list, and delete the last one.

Do you understand or I'm not clear ?
PS : Don't PM me each time you answer, I come watch every subjet...

i doesnt matter if i dont specify date , how do i specify hours minutes , & after how many days
I don't understand where the problem is, you just need 3 inputs (day, hour, minute)

Structure of the cron will be : minutes hours /day * php execute.php
Where's the problem ?
PHP Code

<?php
if(isset($_POST['submit'])) {
    
$cron = "*/{$_POST['Minutes']} */{$_POST['Hours']} */{$_POST['Day']} * * execute.php";
    echo 
$cron;
}
?>
<form action="Cron.php" method="post">
    <input type="text" placeholder="Hours" name="Hours"><br>
    <input type="text" placeholder="Minutes" name="Minutes"><br>
    <input type="text" placeholder="Day" name="Day"><br>
    <input type="submit" name="submit">
</form> 
According to http://cron.schlitt.info/index.php , the output is correct and do perfectly what you want.
(To test your output into the website, just remove the execute.php)
(Dec 2, 2014 09:52 AM)Sakuto Wrote:  
PHP Code

<?php
if(isset($_POST['submit'])) {
    
$cron = "*/{$_POST['Minutes']} */{$_POST['Hours']} */{$_POST['Day']} * * execute.php";
    echo 
$cron;
}
?>
<form action="Cron.php" method="post">
    <input type="text" placeholder="Hours" name="Hours"><br>
    <input type="text" placeholder="Minutes" name="Minutes"><br>
    <input type="text" placeholder="Day" name="Day"><br>
    <input type="submit" name="submit">
</form> 
According to http://cron.schlitt.info/index.php , the output is correct and do perfectly what you want.
(To test your output into the website, just remove the execute.php)
oh thanks a ton
will try it now 🤝
I can't actually test it on a true Server, but if it doesn't work, I know how to make the script to generate the sequence properly.

It will just look like this (for repeating every 30 minutes, 2 hours, 2 days) :

0,30 0,2,4,6,8,10,12,14,16,18,20,22 /2 *

Just test the previous code and if it doesn't work, I will give you the second option code.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[PHP] by Vip3rrNov 15, 2017 11:30 PM 5 231 Nov 15, 2017 11:30 PM
Last Post: V3t3r4n
[PHP] by dark_angel1Sep 13, 2017 06:50 PM 9 397 Sep 13, 2017 06:50 PM
Last Post: #Skyline
[PHP] by MentorPENAug 21, 2017 07:28 AM 6 222 Aug 21, 2017 07:28 AM
Last Post: MentorPEN