Run bash script between time variables - bash

I need to run some task at a dynamic time presented in the variable (which value is in HH.mm.ss format) + 2 minutes from its value and less than 5 minutes. Then I could add this job to crontab to schedule it for every minute and I hope that the script will run when the time variable syncs the current time + 2 (or a bit more) minutes (but no more than 5 minutes).
Thank you.
Update:
Thanks to l0b0, all that is left is to find a way to substract 2 minutes from HH.mm.ss variable to get for example 05:28:00 after substraction from var 05:30:00. I think it must be somehow simple. Thanks for help.

at should do the trick. Based on man at and an offset variable $offset you should be able to use this (untested):
echo 'some_command with arguments' | at "now + ${offset}"

Related

What is "INTERVAL=0" means in Oracle Schedular?

My Oracle DBA have setup a task with following repeat_interval:
Start Date :"30/JAN/20 08:00AM"
Repeat_interval: "FREQ=DAILY; INTERVAL=0; BYMINUTE=15"
Can I ask what is "Interval=0" means?
Does it means this task will run daily from 8AM, and will repeat every 15 mins until success?
I tried to get the answer from Google, but what I find is what is Interval=1, but nothing for 0.
So would be great if anyone can share me some light here.
Thanks in advance!
INTERVAL is the number of increments of the FREQ value between executions. I believe in this case that a value of 0 or 1 would be the same. The schedule as shown would execute once per day (FREQ=DAILY), at approximately 15 minutes past a random hour (BYMINUTE=15, but BYHOUR and BYSECOND are not set).
Schedule has nothing to do with whether or not the previous execution succeeded or not. Start Date is only the date at which the job was enabled, not when it actually starts processing.
If you want it to run every 15 minutes from the moment you enable it, you should set as follows:
FREQ=MINUTELY; INTERVAL=15
If you want it to run exactly on the quarter hour, then this:
FREQ=MINUTELY; BYMINUTE=0,15,30,45; BYSECOND=0
If you want it to run every day at 8am, then this:
FREQ=DAILY; BYHOUR=8; BYMINUTE=0; BYSECOND=0

How do i execute part of a shell script every 5 minutes and anothe rpart every 1 hour?

I have a shell script that collects some data and send it to destination. Part of the data should be copied every 5 minutes and other every 20 minutes. How can this be achieved in a single script? As of now i'm collecting the data every 5 minutes by scheduling with cron.
Best practice would be to use to separate files with two different cron entries. If you need to reutilize part of your code consider using functions.
If you still want to do it in only one file, you should run it every 5 minutes and on each run check whether or not you should execute the other part (every 20 min) or not.
modulo=$((`date +%_M)` % 20))
//do whatever has to be done every 5min
[...]
//check for modulo of current minute / 20
if [ $modulo -eq 0 ]; then
echo Current minute is `date +%_M)`, must execute part 2
//whatever has to be done every 20min
else
//do nothing
fi;
The reason why the variable modulo is defined in the first line is because what has to be done every 5min can potentially take longer than 1min to execute so by the time it is done minute is no longer 20 but 21. Defining the variable before is a safeguard to overcome this.

Oracle-DBMS jobs scheduler change the start time

I have a DBMS_jobs which is scheduled to run a procedure FINDING_PROCEDURE at 6 am evey day. Can anyone tell me how can i change the start time so that it is scheduled to run at 9 am from tomorrow. Thanks in advance.
As I already mentioned in my comment - your job doesn't run at 6 am every day, it runs every 21 hours.
As a second remark, you should seriously consider switching to DBMS_SCHEDULER - it's so much nicer than DBMS_JOB.
Anyway, to let this job run at 9am every day, this should do the trick:
DBMS_JOB.CHANGE (
job => your_job_id,
interval => 'trunc(sysdate) + 1 + 9/24');
you can use DBMS_JOB.CHANGE() to Alter your job schedule.
Click on this link for complete reference from
Oracle Documentation:DBMS_JOB
and find DBMS_JOB.CHANGE()

CRON: Run job on particular hours

I have a spring batch application and i am using CRON to set how often this application runs. But the problem i am running into is that i want to run the job on specific hours
3 am
7 am
11 am
3 pm
7 pm
11 pm
As you can see it is every 4 hours but starts at 3 am so i cannot use */4 in the hours section of the timing format as this would start the job at 4am
I have also tried '3,7,11,15,19,23' in the hours section but this does not work either (guessing it only works in the minutes section). Does someone know how i can do this?
Use
#Scedule(cron="0 0 3/4 * * ?")
The Pattern x/y means: where <timepart> mod y = x
or
#Scedule(cron="0 0 3,7,11,15,19,21 * * ?")
According to the Quartz Cron Trigger Tutorial:
The '/' character can be used to specify increments to values. For
example, if you put '0/15' in the Minutes field, it means 'every 15th
minute of the hour, starting at minute zero'. If you used '3/20' in
the Minutes field, it would mean 'every 20th minute of the hour,
starting at minute three' - or in other words it is the same as
specifying '3,23,43' in the Minutes field. Note the subtlety that
"/35" does *not mean "every 35 minutes" - it mean "every 35th minute
of the hour, starting at minute zero" - or in other words the same as
specifying '0,35'.
0 0 3,7,11,15,19,23 * * ?
Fires for 0 minute starting at 3am and ending at 23:00 pm every day.
judging by the two answers above the error i was making was i was keeping the apostrophe at the start and end of my hours... very silly
i managed to solve this by using 3-23/4 for the hour as this starts from 3am and then every other fourth hour (just a different way of doing it to the other answers)

bash : Make job every x days

I have a bash script runned every day via cron.
In this bash I want to run some command but only every x day (x is fixed and will be 3, 4 or 5 I don't know at the moment)
I can test if the remainder of the division day of month / x equal 0
day_of_month % x = 0
I will work on a single month but not always between to month, for example months april - may with x = 3
1 2 3 4 5 6 7 8 9 ... 27 28 29 30 31 1 2 3 4 5 6 7 8 9 ... 27 28 29 30
x x x ... x x x x x ... x x
It not a big deal, but is there any other way to do this ?
PS : I don't want to explode my script and make 2 cron jobs (one every day : *, the other evevery x day : */x)
One easy solution would be to use "day of year" instead of day of month. Then there'd be no problem with different month lengths. You'd still get problems at the end of year, of course.
The "day of year" is available via date +%j.
If that is still not acceptable, you can use the seconds since 1970 (date +%s), and divide by days*24*60*60, then use the remainder of that. You'd still get problems with leap seconds, but otherwise it should be correct.
Using cron sounds the like the proper idea actually, because that is what cron is made for.
One idea though to solve your 'across months' problems: don't think in months, instead check whether the time-in-seconds-since-epoch % x*seconds-per-day is 0 ...
I would export a system variable with last date of execution and calculate the interval from it. It wouldn't work after a restart but I assume in this case the interval should be restored.
Have a look at the at command
Using that you can tell a script to call itself at a later date.
From manpage:
For example, to run a job at 4pm three
days from now, you would do at
4pm + 3 days, to run a job at 10:00am on July 31, you would do at
10am
Jul 31 and to run a job at 1am tomorrow, you would do at 1am
tomorrow.
If I had this problem, I would tell cron to run my script once every day, and inside the script I would calculate whether or not work needs to be done. That way, you have full control over it.
For example,
dayno=`date +%d`
let divided=daynum%3
if [ $divided == 0 ] ; then
exit 0
fi
# real work begins here
This script would work on any system that uses bash.
Does your job need to run every day? That is, does it do something on those days when your calculation does not result in a 0. If not, you could set up a job in cron thus:
0 12 3,6,9,12,15,18,21,24,27,30 * * <your job>
and it would run every third day (at 12 noon in my example).
I'm pretty sure that "Feb 30" would be safely ignored.

Resources