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

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

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.

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.

In php cron syntax url i have two parameters passed but it gets one parameter

this my url cron syntax
1.* 30 * * wget http://myappone.com/universitysaver/scripts /signUpForm3.php?userId=49crbrGzdPk3&value=1.99
its gets only one argument that is first argument
the second argument its not displayed
iam using $_REQUEST['userId'];
and $_REQUEST['value'];
and another thing is iam changing the values first argument is value second argument is userId its displayes value but it not display userId
30 * * wget http://myappone.com/universitysaver/scripts /signUpForm3.php?userId=49crbrGzdPk3&value=1.99
its get only first argument the second argument its not get
i want to get two arguments how can i get
please help me
thanks for advance
In bash & is special character and tells the shell to put the process in the background, so nothing after the & is read. you need to escape your string
30 * * * * wget http://myappone.com/universitysaver/scripts /signUpForm3.php?userId=49crbrGzdPk3\&value=1.99
or
30 * * * * wget "http://myappone.com/universitysaver/scripts /signUpForm3.php?userId=49crbrGzdPk3&value=1.99"

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

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