random cron job - php

I need a script1 that will execute script2 at random times a day.
I'm looking to execute the script2 around 30 times a day within random times.
script1 will be set in the cron job.
Could someone please help how to make it happen?
PS I am not a programmer, so would need something ready to go, please

Seth's solution certainly works, but the number of executions per day will differ. If you want definitely 30 executions, not more and not less, I propose using a cron entry like
0 0 * * * gen-executions.sh
and a script gen-executions.sh:
#!/bin/bash
for number in $(seq 30)
do
hour=$(( ${RANDOM}*24/32768 ))
minute=$(( ${RANDOM}*60/32768 ))
at -f /path/to/script.sh $(printf "%02d" ${hour}):$(printf "%02d" ${minute})
done
This generates exactly 30 executions of /path/to/script.sh at random times of the day using at.

* * * * * script1.sh
#!/bin/bash
if [ $(($RANDOM*100/32768)) -gt 2 ]; then exit; fi
exec php script2.php

Related

How to get out of the loop, if the PHP script takes more than 53 seconds to execute

In Short: How to break the code after the code takes more than 53 seconds to execute, something like this:
while (5 < 200)
{
// do some work here, which will take around 1-6 seconds
if (code this loop is running for more than 53 seconds)
break;
}
If you want to know, why i want to do this:
Ok, this is What I am doing: I am copying data from 7 JSON pages on the web and inserting them into my MySQL database.
The code is something like this:
$start_time = microtime(TRUE); // Helps to count PHP script execution time
connect to database
$number = get inserted number into database
while ($number > ($number+20) )
{
7 Open JSON File link like this - "example.com/$number?api=xxxyyzz"
Use Prepared PDO statements to insert data into MySQL
}
// Code to count PHP script execution time
$end_time = microtime(TRUE);
$time_taken = $end_time - $start_time;
$time_taken = round($time_taken,5);
echo '<p>Page generated in '.$time_taken.' seconds.</p>';
So in my case, It takes around 5.2 seconds to complete one whole loop of adding all data. But some JSON files are empty, so it takes only 1.4 second to complete 1 loop, if they are empty.
So like that, I want to complete millions of loops (add Data from millions of JSON files). So if my code runs for 24/7, it will take me 1 month to complete my task.
But after the code runs for 90 seconds, i get this error:
I am using a CRON job to do the task. And looks like server gives the same error to CRON job.
So, I want to do the CRON job to run every 1 minute, so I do not get timed out error.
But I am afraid of this: What If the script added data in half rows and 1 mintue gets over, and it do not add data into other half rows. Then after the starting on the new minute, the code start from the next $number.
So, If i can break; out of the loop after 53 seconds (If the code starts another loop at 52 seconds, then break at the end of it, that will be around 58-59 seconds).
I mean, i will put the break; code just before the loop end (before }). So i do not exit the loop, while the data got inserted into half of the rows.
I guess that your PHP's max_execution_time is equal to 90 seconds, you can specify max_execution_time by set_time_limit, but I don't think it is a good approach for this.
Have a try pcntl or pthreads, it would save you a lot of time.

run cron from 5am to mightnight, every 30minutes, everyday

I was able to generate this directive with some help
As I'm not a cron expert by any means.
What I expect it to do is:
Run every 30 minutes from
5 AM to Midnight, every day
0,30 0,5-23 * * * /path_to_script
Even if it's correct, is this the right syntax to do it? Or it doesn't matter
because I haven't seen "," before in crons, just "/"
Your syntax is correct, but it will also run at 00:30. If you don't want that, you need two entries.
0,30 5-23 * * * /path_to_script
0 0 * * * /path_to_script

Setting up Cron job using PHP script [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
I am beginner in programming and I was trying to set up Cron Job using PHP script in couple ways, which I found on the internet, but I always run into some errors I can't fix by myself:
First method:
exec('echo -e "`crontab -l`* * {$_POST['birthDay']} {$_POST['birthMonth']} * /home/jharvard/vhosts/pset7/public/{$_POST['lastname']}{$_POST['firstname']}.php" | crontab -");
,but I think error appears because of ` symbol and I am sure a lot of ' symbols os not good either.
Second method:
echo "* * {$_POST['birthDay']} {$_POST['birthMonth']} * /home/jharvard/vhosts/pset7/public/{$_POST['lastname']}{$_POST['firstname']}.php" | tee -a /var/spool/cron/root
Error is: ( ! ) Parse error: syntax error, unexpected 'var' (T_VAR) in /home/jharvard/vhosts/pset7/public/calendar.php on line 24
Third try:
$cron_file = "{$_POST['lastname']}{$_POST['firstname']}";
// Create the file
touch($cron_file);
// Make it writable
chmod($cron_file, 0777);
// Save the cron
file_put_contents($cron_file, "* * {$_POST['birthDay']} {$_POST['birthMonth']} * /home/jharvard/vhosts/pset7/public/{$_POST['lastname']}{$_POST['firstname']}.php");
//Install the cron
exec('crontab cron_file');
I have no idea why this one doesn't work. I execute
crontab -l
, but it doesn't show any new jobs
Your first try got lots of problem:
The string has started with single quotation, ended with double.
variables inside strings that started with double quote will be parsed. so if you start your string with single quote, variable won't replace with their values.
you can not use single quote character within a string started with single quote, unless you escape with backslash. (same apply to double quote as well) example: $var = '\'test\''
So grammatically, your first try should be like this:
exec("echo -e \"`crontab -l`* * {$post['birthDay']} {$post['birthMonth']} * /home/jharvard/vhosts/pset7/public/{$post['lastname']}{$post['firstname']}.php\" | crontab -");
Better Solution
As I understand from your code, you want to do something on user's birthday.
A better solution is create one single cronjob manually, that will run one file every night, and that file will manage what to do (including who's birthday is that, sending them email, etc) instead of having a cronjob for every user.
You can do it like this
$command = " * * * * * php PATH_TO_YOUR/some.php ";
exec('echo -e "`crontab -l`\n'.$command.'" | crontab -', $result);
var_dump($result);
by this you can add to www-data's user crontab new command, which will run some.php every minute
for more information here is reference about crontab - http://www.adminschoice.com/crontab-quick-reference , you need to read it to understand what to do with * * * * * (which are responsible for cronjob running time) and how to build correct $command with usage of php,sh and other scripts

How to setup CRON job to run every 10 seconds in Linux?

I need to run a CRON job every 10 seconds from started time.
In Linux how to run a CRON job on every 10 seconds from the time its started?
I am trying to solve that as following: when I make a request (or start) at 04:28:34 it should start at 04:28:44 not at 4:28:40
This is what I have done
# m h dom mon dow command
*/10 * * * * /usr/bin/wget http://api.us/application/
What did I do wrong? Why does this not trigger wget every 10 seconds?
To elaborate on Sougata Bose's answer, I think the OP wants a command to be run every 10 seconds from a start time; not 10 seconds after the first minute and every subsequent minute.
cron only has a resolution of 1 minute (there are other tools I think that may have finer resolutions but they are not standard on unix).
Therefore, to resolve your issue you need 60 seconds / 10 seconds = 6 cron jobs, each with a sleep.
e.g. run crontab -e and add the following lines to your chosen editor:
* * * * * ( /usr/bin/wget http://api.us/application/ )
* * * * * ( sleep 10 ; /usr/bin/wget http://api.us/application/ )
* * * * * ( sleep 20 ; /usr/bin/wget http://api.us/application/ )
* * * * * ( sleep 30 ; /usr/bin/wget http://api.us/application/ )
* * * * * ( sleep 40 ; /usr/bin/wget http://api.us/application/ )
* * * * * ( sleep 50 ; /usr/bin/wget http://api.us/application/ )
Another option is to edit your crontab with crontab -e and write:
* * * * * for i in {1..6}; do /usr/bin/wget http://api.us/application/ & sleep 10; done
*/10 * * * * will run every 10 min.
*/10 * * * * * will run every 10 sec.
You can checkout the cron editor for more options.
Using commas in seconds field works too:
0,10,20,30,40,50 * * * * *
Use watch instead; for example:
watch -n10 -x your_command
If something should run every 10 seconds it does not have to be cron job. It can be a script with endless loop with sleep inside like:
while true
do
# or whatever command you need to run
rm -rf /var/www/some-directory
sleep 10
done
and run it with
nohup bash my-endless-script.sh
which will run it on background and will continue also if you shut down the terminal.
You can kill the run with kill command.

How to add sleep setting to cron job

Bellow is my cron command.I want to add the sleep command there.
This is what i tried so far.is that CORRECT ?
* * * * * root sleep 5 && /home/samitha/bash.sh >> /home/samitha/log/cron.log 2>&1
That is absolutely correct. You can also use ; instead of &&, but in this case it doesn't matter because sleep returns 0 always.
Update
This format here is for /etc/crontab.
If you use users' crontabs (edit them using crontab -e, as on the screenshot),
you must omit username from the line:
* * * * * sleep 5 && /home/samitha/bash.sh >> /home/samitha/log/cron.log 2>&1
example: To execute command every 1min with random between 1..5 seconds
*/1 * * * * sleep $(((RANDOM \% 5) + 1)); yourCommand

Categories