When does this cron job get executed? - unix

I see this cron setting in a crontab and I am curious as to when the script actually gets executed.
8 10 * * 6 expr `date +\%W` \% 2 == 1 >/dev/null || /path/to/script/scriptToRun.sh

Such a picky syntax...
8 10 * * 6 expr `date +\%W` \% 2 == 1 >/dev/null || /path/to/script/scriptToRun.sh
First, the cronjob:
+---------------- minute (0 - 59)
| +------------- hour (0 - 23)
| | +---------- day of month (1 - 31)
| | | +------- month (1 - 12)
| | | | +---- day of week (0 - 6) (Sunday=0 or 7)
| | | | |
* * * * * command to be executed
8 10 * * 6
So in this case it means that the cronjob gets executed every Saturday at 10.08.
Then, man date says:
%W
week number of year, with Monday as first day of week (00..53)
$(date +\%W) \% 2 == 1 >/dev/null means: if the week number is not multiple of 2, then send the output to dev/null. Otherwise, proceed normally.
So the script gets executed every other Saturday at 10.08.

Related

Calculating Survival rate from month to month without losing starting values

I have a set of code that divides the number of alive specimens from the initial count.
I am trying to determine the survival rate for the entire 5 month experiment but, there seems to be an issue with the computation each month. For the initial month, the code computes the correct survival rate (ie: 48/50 - 96%). But, the issue comes in when computing for the next month where the code will compute the survival rate from 48 instead of 50 (ie: 46/48 survived, instead of 46/50 which is what I need). It continues this way for the remainder of the experiment (30/46 month 3, then 20/30 for month 4).
Additionally, each of the "dead" specimens are then added to an NA group automatically (There should be no NA groups). I think if the first issue is taken care of then the NA issue wont happen. Is there a way to fix this with the code I have or do I need to rearrange the data in excel?
I have 2 species in 4 habitats that need this code for analysis.
Thanks!
Month 1
| Species | Cage || nStart | nAlive || PropAlive
| -------- | -------------- || | |
| X | 1 || 10 | 9 | .9
| Y | 2 || 10 | 8 | .8
| -------- | -------------- || | |
Month 2
| Species | Cage || nStart | nAlive || PropAlive (nAlive/nStart)
| -------- | -------------- || | |
| X | 1 || 9 | 8 | .89
| Y | 2 || 8 | 7 | .875
| -------- | -------------- || | |
month 2 should be 8/10 and 7/10 for Prop Alive not 8/9 and 7/8.
library(readxl)
library(tidyverse)
library(lme4)
library(car)
library(emmeans)
JulyData<- read_excel("~/R/Cage Data Final 2016 EMV 1.20.xlsx", sheet="7.1.2016")
str(JulyData)
summary(JulyData$Lice)
AllCages<- distinct(JulyData, Cage, Species)
AllCages$nStart<- rep(10,nrow(AllCages))
Alive<- JulyData%>%
filter(!is.na(Lice))%>%
group_by(Cage, Species)%>%
summarise(nAlive=n())
CleanData<-merge(AllCages,Alive, all=TRUE)
CleanData$nAlive[is.na(CleanData$nAlive)]<-0
CleanData$nAlive[CleanData$nAlive>10]<-10
CleanData<-CleanData %>%
separate(Cage,c("Habitat", "Rep"),1,remove=FALSE) %>%
mutate(nDead=nStart-nAlive)
CleanData
CleanData%>%
group_by(Species,Habitat)%>%
summarize(nStart=sum(nStart),
nAlive=sum(nAlive),
PropAlive = nAlive/nStart)
So, the issue was related to formatting within the data. The code is right - a simple labeling error was the issue.

how to combine R data frames with non-exact criteria (greater/less than condition)

I have to combine two R data frames which have trade and quote information. Like a join, but based on a timestamp in seconds. I need to match each trade with the most recent quote. There are many more quotes than trades.
I have this table with stock quotes. The Timestamp is in seconds:
+--------+-----------+-------+-------+
| Symbol | Timestamp | bid | ask |
+--------+-----------+-------+-------+
| IBM | 10 | 132 | 133 |
| IBM | 20 | 132.5 | 133.3 |
| IBM | 30 | 132.6 | 132.7 |
+--------+-----------+-------+-------+
And these are trades:
+--------+-----------+----------+-------+
| Symbol | Timestamp | quantity | price |
+--------+-----------+----------+-------+
| IBM | 25 | 100 | 132.5 |
| IBM | 31 | 80 | 132.7 |
+--------+-----------+----------+-------+
I think a native R function or dplyr could do it - I've used both for basic purposes but not sure how to proceed here. Any ideas?
So the trade at 25 seconds should match with the quote at 20 seconds, and the trade #31 matches the quote #30, like this:
+--------+-----------+----------+-------+-------+-------+
| Symbol | Timestamp | quantity | price | bid | ask |
+--------+-----------+----------+-------+-------+-------+
| IBM | 25 | 100 | 132.5 | 132.5 | 133.3 |
| IBM | 31 | 80 | 132.7 | 132.6 | 132.7 |
+--------+-----------+----------+-------+-------+-------+
Consider merging on a calculated field by increments of 10. Specifically, calculate a column for multiples of 10 in both datasets, and merge on that field with Symbol.
Below transform and within are used to assign and de-assign the helper field, mult10. In this use case, both base functions are interchangeable:
final_df <- transform(merge(within(quotes, mult10 = floor(Timestamp / 10) * 10),
within(trades, mult10 = floor(Timestamp / 10) * 10),
by=c("Symbol", "mult10"),
multi10 = NULL)
Now if the 10 multiple does not suffice for your needs, adjust to level you require such as 15, 5, 2, etc.
within(quotes, mult10 <- floor(Timestamp / 15) * 15)
within(quotes, mult10 <- floor(Timestamp / 5) * 5)
within(quotes, mult10 <- floor(Timestamp / 2) * 2)
Even more, you may need to use the reverse, floor or ceiling for both data sets respectively to calculate highest multiple of quote's Timestamp and lowest multiple of trade's Timestamp:
within(quotes, mult10 <- ceiling(Timestamp / 15) * 15)
within(trades, mult10 <- floor(Timestamp / 5) * 5)

MS Access date diff from one row and previous row of different fields

I need to find the difference in weeks from marked Previous End Date and next Start Date.
Visit Type | Start Date | End Date | Weeks since previous visit
------------+---------------+---------------+------------------------------
Check-Up | 19-Jan-15 | 19-Feb-15 |
Check-Up | 27-Jan-15 | 27-Jan-15 | xxx
Check-Up | 22-Jan-15 | 22-Feb-15 |
Check-Up | 21-Jan-15 | 21-Jan-1 |
I need to find the diff bw 19 feb of End date and 27 jan of Start date . A simple datediff is not working. can someone help now ?

cron job scheduling timing for excecuting the script

I need to execute a script through cron expression that is 5 days per week (Mon-Fri) between 6pm and 7am GMT.
please advise what will be the cron expression for this..
I have tried this as ...
0 00 23 ? * MON-FRI
as the format is as follows:
+---------------- minute (0 - 59)
| +------------- hour (0 - 23)
| | +---------- day of month (1 - 31)
| | | +------- month (1 - 12)
| | | | +---- day of week (0 - 6) (Sunday=0 or 7)
| | | | |
* * * * * command to be executed
you may want it to be:
* 6 * * 1-5 command
This will work every minute from 6.00 to 6.59. If you need it to execute also at 7.00:
* 6 * * 1-5 command
0 7 * * 1-5 command
and if you want from 6.00 to 7.59, every minute:
* 6-7 * * 1-5 command

Time/Date range grammars

I need to parse strings containing time spans such as:
Thursday 6:30-7:30 AM
December 30, 2009 - January 1, 2010
1/15/09, 7:30 to 8:30 PM
Thursday, from 6:30 to 7:30 AM
and others...
added
6:30 to 7:30
and date/times such as most any cases that Word's insert->date can generate
As I'd be extremely surprised if anything out there covers all the cases I need to cover, I'm looking for grammars to start from.
Ok, the following grammar parses anything in your example:
DTExp = Day, ['-', Day]
Day = DayExp, [[','], ['from'], TimeRange]
DayExp = WeekDay
| [Weekday], Month, DayNumber, [[','], YearNumber]
| [Weekday], MonthNumber, '/', DayNumber, ['/', YearNumber]
TimeRange = Time, [['-'|'to'] Time]
Time = HourNumber, ':', MinuteNumber, ['AM'|'PM']
WeekDay = 'monday' | 'tuesday' | ...
Month = MonthNumber | MonthName
MonthName = 'january' | 'february' | ...
DayNumber = Number
MonthNumber = Number
YearNumber = Number, ['AD'|'BC']
HourNumber = Number
MinuteNumber = Number
There is a slight problem in the grammar. If a DayExp is read, followed by a Time, and a '-', then you could expect another DayExp or another time. But this is solved by a lookahead, because if it is a time, a number is followed by a ':'.
Lets try to construct a parse tree:
Thursday 6 : 30 - 7 : 30 AM
| | | | | |
WeekDay Number : Number - Number : Number |
| -----|---- -----|-----------
| Time - Time
| ---------|---------
DayExp TimeRange
----------|-----------
Day
|
DTExp

Resources