cron job scheduling timing for excecuting the script - unix

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

Related

How can I write a sumo logic query that aggregates a field over time

Artifactory cloud comes with some canned queries.
This query aggregates download volume by repo:
_sourceCategory=artifactory*
| where _sourceCategory matches "*artifactory/traffic"
| parse regex "(?<year>\d{4})(?<month>\d{2})(?<day>\d{2})(?<hour>\d{2})(?<minute>\d{2})(?<second>\d{2})\|\d*\|(?<direction>[^|]*)\|\s*(?<ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|[^|]*)\|(?<repo>[^:]*):(?<fullfilepath>[^|]*)\|(?<size>\d*)" nodrop
| where direction matches "DOWNLOAD"
| sum(size) by repo
| _sum / (1024 * 1024 * 1024) as sizeinGB
| fields -_sum
| sort by sizeinGB | limit 10
This query aggregates data transfer over time:
_sourceCategory=artifactory*
| where _sourceCategory matches "*artifactory/traffic"
| parse regex "(?<year>\d{4})(?<month>\d{2})(?<day>\d{2})(?<hour>\d{2})(?<minute>\d{2})(?<second>\d{2})\|\d*\|(?<direction>[^|]*)\|\s*(?<ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|[^|]*)\|(?<repo>[^:]*):(?<fullfilepath>[^|]*)\|(?<size>\d*)" nodrop
| where direction matches "DOWNLOAD"
| timeslice 1h
| sum(size) by _timeslice, direction
| _sum / (1024 * 1024 * 1024) as sizeinGB | sort by _sum
| fields -_sum
| transpose row _timeslice column direction
I'm trying to combine the two to get GB downloaded over time aggregated by repo, but I cannot figure out how to do this. It looks like I have all the info I need in the query but cannot find the right syntax.
I tried to add a timeslice to the query that sums download size by repo but I get
Field timeslice not found, please check the spelling and try again.
_sourceCategory=artifactory*
| where _sourceCategory matches "*artifactory/traffic"
| parse regex "(?<year>\d{4})(?<month>\d{2})(?<day>\d{2})(?<hour>\d{2})(?<minute>\d{2})(?<second>\d{2})\|\d*\|(?<direction>[^|]*)\|\s*(?<ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|[^|]*)\|(?<repo>[^:]*):(?<fullfilepath>[^|]*)\|(?<size>\d*)" nodrop
| timeslice 1h
| sum(size) by repo
| _sum / (1024 * 1024 * 1024) as sizeinGB
| fields -_sum
| sort by sizeinGB | limit 100
| sort by timeslice

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)

When does this cron job get executed?

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.

ASCII Plotting Functions for R [duplicate]

This question already has answers here:
How can I generate ascii "graphical output" from R?
(2 answers)
Closed 10 years ago.
In order to access my server, I am forced to work with an old text terminal application that does not have X windows. The best thing I have going is emacs/ESS.
Often, I wish to make a rudimentary plots such as histograms and scatter plots and not have to go through the trouble of transferring the file to a computer with a graphics display.
Is there a text terminal-based R graphics library?
There are a number of things that can do bits of it. There's stem in default R, there's this scatter plot function, but best of all, there's the package txtplot on CRAN which does scatterplots, boxplots, barplots, density traces, acfs and plots curves (like the curve function... kinda).
I only need it once in a while - but if I am trying to convey a rough idea of a graphic in pure text as I sometimes need to, it's a life-saver.
In the past I wrote a short piece of R code that made tally-style ascii graphic in very quick time (like a sideways barchart or a stem-and-leaf plot with the numbers replaced by symbols, which solved a problem I had) - but I didn't keep it since stem mostly covers that territory.
Of course the 'table' facility produces ascii output and can be manipulated to do some interesting/useful semigraphical things.
There's also the package ascii that can be used to render various R objects into ascii form in similar fashion to Sweave - handy for formatting tables and so on. Just formatting a table into ascii is not really what it's for but you might still be able to get some use out of it with a little work and the right output format.
Sample output from txtplot:
scatter plot:
> with(cars,txtplot(speed,dist))
+----+-----------+------------+-----------+-----------+--+
120 + * +
| |
100 + +
| * * |
80 + * * +
| * * * |
60 + * * +
| * * * * * |
40 + * * * * * +
| * * * * * * * |
20 + * * * * * * * +
| * * * * |
| * * * |
0 +----+-----------+------------+-----------+-----------+--+
5 10 15 20 25
acf plot:
> txtacf(ldeaths)
+-+--------------+--------------+--------------+--------+
1 + * +
| * |
| * * * * * |
0.5 + * * * * * +
| * * * * * * * * |
| * * * * * * * * |
| * * * * * * * * |
0 + * * * * * * * * * * * * * * * * * * * * * +
| * * * * * * * * * * |
| * * * * * * * * * * |
| * * * * * * * * * |
-0.5 + * * * * * * +
| * * * * |
+-+--------------+--------------+--------------+--------+
0 0.5 1 1.5
density trace:
> txtdensity(rnorm(100,m=5,s=.1))
+------+----------+----------+----------+----------+-------+
| ***** |
4 + ** *** +
| * *** |
| ** *** |
3 + ** *** +
| *** ** |
| ***** ** |
2 + *** ** +
| *** ** |
| ** ** |
1 + ** *** +
| *** ****** |
| ******** *** |
+------+----------+----------+----------+----------+-------+
4.8 4.9 5 5.1 5.2
box plot:
> vc <- ToothGrowth[,2]=="VC"
> oj <- ToothGrowth[,2]=="OJ"
> txtboxplot(ToothGrowth[vc,1],ToothGrowth[oj,1])
5 10 15 20 25 30 35
|----+-------+--------+--------+--------+--------+-------+--|
+--------+-----------+
1 -------------| | |------------------
+--------+-----------+
+------------+----+
2 -------------| | |---------
+------------+----+
Legend: 1=ToothGrowth[vc, 1], 2=ToothGrowth[oj, 1]
curve plot:
> txtcurve(sin(pi*x),from=0,to=2)
+--+-----------+------------+------------+-----------+--+
1 + ********* +
| *** ** |
| ** ** |
0.5 + ** ** +
| ** ** |
| * ** |
0 + * ** * +
| * * |
| ** ** |
-0.5 + *** ** +
| ** ** |
| ** *** |
-1 + ********* +
+--+-----------+------------+------------+-----------+--+
0 0.5 1 1.5 2
bar chart:
> txtbarchart(as.factor(res),pch="|")
+--+------------+------------+------------+------------+--+
50 + | +
| | |
40 + | +
| | |
30 + | | +
| | | |
| | | |
20 + | | | +
| | | | |
10 + | | | +
| | | | |
0 + | | | +
+--+------------+------------+------------+------------+--+
1 1.5 2 2.5 3
Legend: 1=A, 2=B, 3=C
Add in the stem function from default R graphics:
> stem(log(islands,10))
The decimal point is at the |
1 | 1111112222233444
1 | 5555556666667899999
2 | 3344
2 | 59
3 |
3 | 5678
4 | 012
and you have quite a lot of coverage.

cron command to run every 12 hours

I need unix cron command to run every 12 hours.
I have 500+ sub blogs in my server.
This is the file i want to run every 12 hours
http://*.mysite.com/somedir/index.php
Where * is my subdomain of my blogs.
I need cron command for all blogs.
Is it possible to run all of them with single command?
OR do i have to create command for each blog?
A crontab file has five fields for specifying day , date and time followed by the command to be run at that interval.
* * * * * command to be executed
- - - - -
| | | | |
| | | | +----- day of week (0 - 6) (Sunday=0)
| | | +------- month (1 - 12)
| | +--------- day of month (1 - 31)
| +----------- hour (0 - 23)
+------------- min (0 - 59)
* in the value field above means all legal values as in braces for that column.
You could use 0 1,13 * * * which means for every 1AM and 1PM.
0 1,13 * * * rm /var/www/*/somedir/index.php > /home/someuser/cronlogs/some.log 2>&1
where * can be replaced by different domain names.
I think the right way is -> 1 */12 * * * (actually, any number in the minute position will do the trick.)
If you set -> * */12 * * * it will be executed every minute at 12h and again at 24h.
Assuming your sites live in /var/www/sitename and you have the php shell installed in /usr/bin/php you can easily create a cron job that runs all those files.
run
crontab -e
and add this line
42 */12 * * * /usr/bin/php /var/www/*/somedir/index.php >> ~/cronjob.log 2>&1
The * here in /var/www/*/somedir is just a wildcart. This means it will catch every directory in your /var/ww folder.
f.ex:
[jens#localhost ~]$ ls -l temp
total 28
-rw-rw-r--. 1 jens jens 1641 Feb 21 16:12 somefile.py
drwxrwxr-x. 2 jens jens 4096 Feb 22 15:10 test
drwxrwxr-x. 2 jens jens 4096 Feb 22 15:10 test2
drwxrwxr-x. 2 jens jens 4096 Feb 22 15:10 test3
drwxr-xr-x. 8 jens jens 4096 Jan 27 10:21 emptydir
-rw-rw-r--. 1 jens jens 548 Jan 27 16:15 Unsaved Document 1
[jens#localhost ~]$ ls temp/*/testfile.php
temp/test2/testfile.php temp/test3/testfile.php temp/test/testfile.php
As you can see, this returns the testfile.php in each subfolder of temp, namely folder test, test2 and test3.
Emptydir is also a folder, but since it has no testfile.php in it, nothing willhappen with it.
If your directory structure is arbitrarily deep you can use **
e.g.
42 */12 * * * /usr/bin/php /var/www/**/index.php >> ~/cronjob.log 2>&1
Use "*/12" to mean "every 12 hours."
You need some kind of master-script (called by cron), which expands the list of sites, and calls "/usr/bin/php /var/www/*/somedir/index.php", whith the '*' replaced by a list entry. This can be done in a shellscript, a perl or python script, or maybe even a php script. For sh this could be: (untested)
#!/bin/sh
cd /home/subdir/for/cron
LIST="a b c d e f g h i j k l m o p q r s t u v w x y z"
for x in $LIST; do
/usr/bin/php /var/www/${x}/somedir/index.php 2>$1 > /tmp/${x}.log
done
If it is inconvenient to have the list hardcoded like this, there are other methods:
backticks, or read < file_with_all_the_names_in_it
0 */12 * * * means "At minute 0 past every 12th hour."
Check out https://crontab.guru for a nice calculator.
Write command in console
crontab -e
edit with editor (I like nano)
add line
0 1,13 * * * php /home/catalog/public_html/crons/index.php
close with
press ctrl + x
press y then press enter
done :)
Check if saved with
crontab -l
command
if you want to test if it will work test just running it manualy with
php /home/catalog/public_html/crons/index.php
command
Use this it will Run after each 12 hour
* */12 * * * php /var/www/"Your domain"/cronfile.php
->cron('0 */12 * * *');
This cron will run the scheduler at every 12 hours.

Resources