Cron Job daily schedule - server

I need a cron job. It should run be every day on Monday till Thursday, starting at 16:30, executing every 5 minutes till 03:30 AM next day. On Friday it will start at 16:30 executing every 5 minutes till Monday 03:30 AM. Thank you very much!

Based on your comment you need two records like this:
This cover:
Need to run every weekday from Monday till Thursday, starting at
23:05, exporting every 5 minutes till 11:00 AM next day
5,10,15,20,25,30,35,40,45,50,55 23 * * 1-4 command
*/5 0-11 * * 2-5 command
This cover:
On Friday start to run at 23:05 until Monday 11:00 AM
5,10,15,20,25,30,35,40,45,50,55 23 * * 5 command
*/5 * * * 6,7 command
*/5 0-11 * * 1 command
And when we optimize the cron records we will get something like:
*/5 0-11 * * 1-5 command
5,10,15,20,25,30,35,40,45,50,55 23 * * 1-5 command
*/5 * * * 6,7 command
EDIT1:
As per question change you need:
Monday till Thursday, starting at 16:30, executing every 5 minutes
till 03:30 AM next day
30,35,40,45,50,55 16 * * 1,2,3,4 command
*/5 17,18,19,20,21,22,23 * * 1,2,3,4,5 command
*/5 0,1,2 * * 2,3,4,5 command
0,5,10,15,20,25 3 * * 2,3,4,5 command
On Friday it will start at 16:30 executing every 5 minutes till Monday
03:30 AM
30,35,40,45,50,55 16 * * 5 command
*/5 * * * 6,7 command
*/5 0,1,2 * * 1 command
0,5,10,15,20,25 3 * * 1 command
And to "optimize" the cron records
30,35,40,45,50,55 16 * * 1-5 command
*/5 0,1,2 * * 1,2,3,4,5 command
0,5,10,15,20,25 3 * * 1,2,3,4,5 command
*/5 17,18,19,20,21,22,23 * * 1,2,3,4,5 command
*/5 * * * 6,7 command

Related

Why does SELECT '2019-05-03'::timestamp - '2018-05-07'::timestamp < '1 year 1 day'::INTERVAL; return FALSE in postgresql?

I am trying to compare two dates and return TRUE if the first date is less than '1 year 1 day' from the second date.
Using 361 days instead of '1 year 1 day' returns FALSE, but this makes sense based on why justify_interval('360 days'::interval) results '1 year'.
But when I run
SELECT '2019-05-03'::timestamp - '2018-05-07'::timestamp < '1 year 1 day'::INTERVAL;
I get FALSE, and when I run
SELECT '2019-05-03'::timestamp - '1 year 1 day'::INTERVAL < '2018-05-07'::timestamp;
I get TRUE.
Why do these return different things?
I can't find it in the docs, but this is due to the way intervals represented and compared.
Note that:
select timestamp '2019-05-03' - timestamp '2018-05-07' < interval '366 day';
gives you the expected result of TRUE.
To compare two intervals, Postgres first converts the intervals to integers. This is done in a pretty naive way, where years are concerned:
/*
* interval_relop - is interval1 relop interval2
*
* Interval comparison is based on converting interval values to a linear
* representation expressed in the units of the time field (microseconds,
* in the case of integer timestamps) with days assumed to be always 24 hours
* and months assumed to be always 30 days. To avoid overflow, we need a
* wider-than-int64 datatype for the linear representation, so use INT128.
*/
So, your query is asking:
select 361 * 24 * 3600 * 1000000 < (1 * 12 * 30 * 24 * 3600 * 1000000) + (1 * 24 * 3600 * 1000000);
Or,
select 31,190,400,000,000 < 31,190,400,000,000;
which is obviously false. ^^

Hive Calculation of Percentage

I am trying to write a simple code to calculate the percentage of occurrence of distinct instances in a table.
Can I do this in one go???
Below is my code which is giving me error.
select 100 * total_sum/sum(total_sum) from jav_test;
In the past when I have had to do similar things this is the approach I've taken:
SELECT
jav_test.total_sum AS total_sum,
withsum.total_sum AS sum_of_all_total_sum,
100 * (jav_test.total_sum / withsum.total_sum) AS percentage
FROM
jav_test,
(SELECT sum(total_sum) AS total_sum FROM jav_test) withsum -- This computes sum(total_sum) here as a single-row single-column table aliased as "withsum"
;
The presence of the total_sum and sum_of_all_total_sum columns in the output is just to convince myself that the correct math took place - the one you are interested in is percentage, based on the query you posted in the question.
After populating a small dummy table, this was the result:
hive> describe jav_test;
OK
total_sum int
Time taken: 1.777 seconds, Fetched: 1 row(s)
hive> select * from jav_test;
OK
28
28
90113
90113
323694
323694
Time taken: 0.797 seconds, Fetched: 6 row(s)
hive> SELECT
> jav_test.total_sum AS total_sum,
> withsum.total_sum AS sum_of_all_total_sum,
> 100 * (jav_test.total_sum / withsum.total_sum) AS percentage
> FROM jav_test, (SELECT sum(total_sum) AS total_sum FROM jav_test) withsum;
...
... lots of mapreduce-related spam here
...
Total MapReduce CPU Time Spent: 3 seconds 370 msec
OK
28 827670 0.003382990805514275
28 827670 0.003382990805514275
90113 827670 10.887551802046708
90113 827670 10.887551802046708
323694 827670 39.10906520714777
323694 827670 39.10906520714777
Time taken: 41.257 seconds, Fetched: 6 row(s)
hive>

current time minus mod_date_time

I want to be able to subtract the current time from the mod_date_time field.
This is what I have so far:
SELECT * FROM PROD_TRKG_TRAN.MOD_DATE_TIME - CURRENT_TIME WHERE USER_ID = '12345' * 24 * 60 "INTERVAL IN MINUTES"
Any help?
If you are using Oracle, you can use the following query:
SELECT (MOD_DATE_TIME - SYSDATE) * 24 * 60
FROM PROD_TRKG_TRAN
WHERE USER_ID = '12345';

How to make query in postgresql which will find all records which are started more than 6 hours?

How to make query in postgresql which will find all records which are started more than 6 hours ago?
In my table I have a column which stores utc time of last time used in milliseconds (long integer).
SELECT *
FROM exams
WHERE started < HERE_I_NEED_UTC_IN_MILLISECONDS - 6 * 60 * 1000 * 1000;
select *
from exams
where started < extract(epoch from (current_timestamp - interval '6' hour))

Cron trigger from 12:04 to 14:25

Is it possible to write cron expression for trigger that must fire every day and every minute from 12:04 to 14:25?
I think the shortest solution (using cron) are these 3 lines
4-59 12 * * * <command>
0-59 13 * * * <command>
0-25 14 * * * <command>
They define the trigger ranges for each hour.
You'll have to set 3 diff cron jobs:
.---------------- minute (0 - 59)
| .------------- hour (0 - 23)
| | .---------- day of month (1 - 31)
| | | .------- month (1 - 12) OR jan,feb,mar,apr ...
| | | | .---- day of week
| | | | |
4-59 12 * * * <command to be executed>
0-59 13 * * * <command to be executed>
0-25 14 * * * <command to be executed>
You tagged quartz so here is an example taken from the docs at http://quartz.sourceforge.net/javadoc/org/quartz/CronTrigger.html
0 * 12-14 * * ? would fire every minute every day between 12:00 and 14:59.
Judging by the example "0 0/5 14,18 * * ?" Fire every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day in the web page i linked to, you may be able to do something like
0 4-59,0-59,0-25 12,13,14 * * ?
but I'm not sure that would work because it looks a bit dubious, and the docs don't say how the minutes/hours are interpreted if you write it like that. If it doesn't work, you have to define three triggers:
0 4-59 12 * * ?
0 * 13 * * ?
0 0-25 14 * * ?