Cron Job run between a range of minutes? - cron

Ok so I see all these questions about crons running every 10 minutes, every whatever minutes, but it's SET. However, I want mine to RANDOMLY run every 10-15 minutes. Between those ranges so sometimes it'll pick 13 min, 12 min, etc.
Is there any possible way you can do that on a shared server? Or would I have to program it via PHP and have that script run everytime someone visits the page I want to refresh?
Thanks for your input!

I usually let the cronjob run every 10min and in the script itself that cron executed I place at the header of the script a sleep+random number of the extra random "window" I need - i.e: in your case 5min => 300sec
Something like:
#!/usr/bin/php
<?
sleep( rand(1, 300) );
print "starting job ! \n"
?>

Related

How to run a python script between a particular times every single day (on Linux)?

I am looking for a way to be able to run a python script at a particular times of day and then have it auto terminated at another time of day. Ideally, I would want this to not be done within the script itself.
For example: I would want the script to start at 08:00 and end at 10:00 then start again at 11:30 and then terminate at 15:00 and I would need this to happen every day automatically.
I have browsed through many suggestions online, and many of them suggested to use cron, however, as far as I can see, cron does not natively offer the functionality of automatically terminating an application.
Others have suggested using cron to start the application at a particular time and then use another cron instance to create a "terminate" file that the program will search for at every loop iteration and if the file is present then the python script will terminate via a sys.exit() function or something, however, this seems quite janky and more of a workaround than a real solution.
You may use Jobber. You will be able to start scripts whenever you want and for the time you want.
Warning : Jobber is not free. You can try it for free though.
Here is the link to Jobber's website.
You could write a script that creates a lockfile with cron (https://unix.stackexchange.com/questions/12815/what-are-pid-and-lock-files-for), and use the lockfile to know what the process ID is, then terminate the process with that id using cron as well
After you have determined that the process name is uniquely identifiable you could do something like this (that's indeed also using cron).
0 8 * * * /path/to/unique_name.py& ( echo "pkill unique_name.py" | at 10:00 )
30 11 * * * /path/to/unique_name.py& ( echo "pkill unique_name.py" | at 15:30 )
Edit 1:
And "name safe" versions (using kill).
0 8 * * * /path/to/unique_name.py& echo kill $! | at 10:00
30 11 * * * /path/to/unique_name.py& echo kill $! | at 15:30

How can I to restrict the execution of an imacros script between some hours?

I have an imaros script that runs each 4 minutes (using auto clicker)... but I need to it doesn't run between 23:30 to 00:30... so I need to do something like:
If (date is <23:30 and date is > 00:30) then run!!
Is this possible?
many thanks!
This is possible with JavaScript. You can create current time in JS and compare it with time between the two times you named. It's a little complicated to make. Msg me on private messages and we can work things out.

Compare A Variable Regularaly Using Linux Scripts and Cron

I'm trying to check if a number differs from what it was last time it was checked, in this case checking a number every minute, using Linux scripts and cron.
eg:
newNum = getNum()
if oldNum != newNum: run some code
oldNum = newNum
(repeat every minute using crontab)
But the problem I am having is that the variables aren't accessible between scripts and using source (eg. source script.sh) runs the script again, hence getting the latest version, not the one from a minute ago.
The best I've got is running a first script which gets the current number, then sleeps for a minute, then runs a second script which is essentially the first two lines of the code above.
eg:
oldNum = getNum()
sleep 60
export oldNum
script2.sh
This seems inefficient to me and I'd like to know if there is a better solution if possible.
You could cache the previous number in a file:
number_cache=/path/to/cache_file
# read the previous number
oldNum=$(< "$number_cache" )
# acquire the new number
newNum=$(getNum)
if [[ "$oldNum" -eq "$newNum" ]]; then
do_something
fi
# cache the new number
printf "%d\n" "$newNum" > "$number_cache"

Cron Jobs Linux row deletion

I'm running this script;
$query = "SELECT * FROM XXX WHERE email='$Email'";
if($count==1) // fails
if($count==0) // succeeds
If successful
mysql_query ("INSERT INTO XXX (email) values ('$Email'");
Then proceeds onto the next script.
So, it checks to see if you have already ran this script in the past on that account, if you have your email is stored then you can't run this script ever again on that same email.
However, after this script has been processed I want it to delete the row created for the email after 6 hours.
So that after 6 hours they may run the script again.. I've been enlightened that I need to use Cron jobs for this, But I'm not sure how.. Any help is highly appreciated!
Many regards, and thanks in advance.
0 0,6,12,18 * * * /path/to/mycommand
This means starting from hour 0, 6, 12, and 18 the cron job would run. That would be the cron needed to do what you want.
Depending on which linux version you are running you will need to see how to actually create the cron job.
I would think at now +6 hours is a better choice here.

set cronjob time

i was trying to make a code to automatically send mails from a server. I want it to run a php everytime at every hour and 15 minutes and 30 minutes.
Example at 08:15, 08:30, 09:15, 09:30, etc..
Thank you,
Daniel!
How about this?
15-30/15 * * * * * php foo.php
Obviously, replace php foo.php with the command you'd like to run. The 15-30/15 syntax indicates: minutes 15 through 30, with increments of 15. This will make your job run every hour at xx:15 and xx:30.

Resources