How to add sleep setting to cron job - php

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

Related

Set up crontab every 4 hours 15 minutes

Below, I have this cron expression that runs every 4 hours:
* */4 * * *
However, I need it to run every 4 hours 15 minutes.
Your help is appreciated.
I'm having problems with this cron, I do not know if the expression will be incorrect, it will run every 4 hours but it does not stop, it runs every minute of the time it starts running
* */4 * * *
I'm sure it's impossible to implement it with just a single rule.
But you still can achieve the same using multiple rules:
0 0 * * * ...
15 4 * * * ...
30 8 * * * ...
45 12 * * * ...
0 17 * * * ...
15 21 * * * ...
WARNING: It would run with larger/shorter intervals on DST change though.

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.

random cron job

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

Categories