There is a question happened this week on my crontab job.
It's be set as below and works normal every two weeks until now.
10 06 * * 1 test $(($(date +\%W)\%2)) -eq 0 && echo 'test' > /tmp/test.log
The problem is
$(($(date +\%W)\%2)) would be 08, over 7 in February.
And it will show error message if you run in bash: value too great for base (error token is "08").
There is also not working when I try to revise it for forcing decimal base purpose :
10 06 * * 1 test $((10#$(date +\%W)\%2)) -eq 0 && echo 'test' > /tmp/test.log
Does someone know how to solve this issue? Many thanks.
Every two week at midnight
0 0 */15 * * echo 'test' > tmp.txt
Your cron job will be run at: (5 times displayed)
2016-03-15 00:00:00 UTC
2016-03-30 00:00:00 UTC
2016-04-15 00:00:00 UTC
2016-04-30 00:00:00 UTC
2016-05-15 00:00:00 UTC
Make condition in your script
The use of % sign is discouraged in crontab!
To ensure having jod started on monday, every two weeks you've better to create a small script.
#!/bin/bash
if [ "$1" == "-f" ] ;then # use "-f" switch to force execution on odd weeks
shift
else
printf -v d '%(%W)T' -1
((10#$d%2)) || exit 0 # stop here if odd week
fi
printf "Real job begin at %(%c)T, there...\n" -1
This could be shortened to one single line:
#!/bin/bash
[ "$1" == "-f" ]&&shift||{ printf -v d '%(%W)T' -1;((10#$d%2))||exit;}
So you could add standard lines in your crontab:
10 06 * * 1 path/to/myScript > /tmp/test.log
Sample: year 2016
date -d 2016-1-4\ 10:06 +%s
1451898360
for i in {0..52};do
printf -v d '%(%W)T' $((c=i*7*86400+1451898360))
((10#$d%2)) && printf "%(%a %d %b, week: %W)T\n" $c
done | cat -n
1 Mon 04 Jan, week: 01
2 Mon 18 Jan, week: 03
3 Mon 01 Feb, week: 05
4 Mon 15 Feb, week: 07
5 Mon 29 Feb, week: 09
6 Mon 14 Mar, week: 11
7 Mon 28 Mar, week: 13
8 Mon 11 Apr, week: 15
9 Mon 25 Apr, week: 17
10 Mon 09 May, week: 19
11 Mon 23 May, week: 21
12 Mon 06 Jun, week: 23
13 Mon 20 Jun, week: 25
14 Mon 04 Jul, week: 27
15 Mon 18 Jul, week: 29
16 Mon 01 Aug, week: 31
17 Mon 15 Aug, week: 33
18 Mon 29 Aug, week: 35
19 Mon 12 Sep, week: 37
20 Mon 26 Sep, week: 39
21 Mon 10 Oct, week: 41
22 Mon 24 Oct, week: 43
23 Mon 07 Nov, week: 45
24 Mon 21 Nov, week: 47
25 Mon 05 Dec, week: 49
26 Mon 19 Dec, week: 51
27 Mon 02 Jan, week: 01
For comparission with idea of using 15th and 30th each months:
for i in {1..12}/{15,30};do
date -d '2016/'$i +'%a %d %b, week: %W' 2>/dev/null
done | cat -n
1 Fri 15 Jan, week: 02
2 Sat 30 Jan, week: 04
3 Mon 15 Feb, week: 07
4 Tue 15 Mar, week: 11
5 Wed 30 Mar, week: 13
6 Fri 15 Apr, week: 15
7 Sat 30 Apr, week: 17
8 Sun 15 May, week: 19
9 Mon 30 May, week: 22
10 Wed 15 Jun, week: 24
11 Thu 30 Jun, week: 26
12 Fri 15 Jul, week: 28
13 Sat 30 Jul, week: 30
14 Mon 15 Aug, week: 33
15 Tue 30 Aug, week: 35
16 Thu 15 Sep, week: 37
17 Fri 30 Sep, week: 39
18 Sat 15 Oct, week: 41
19 Sun 30 Oct, week: 43
20 Tue 15 Nov, week: 46
21 Wed 30 Nov, week: 48
22 Thu 15 Dec, week: 50
23 Fri 30 Dec, week: 52
As you could see, there miss one operation on weeks: 6, 9, 10, 21, 32 and 45. At least there is 3 operation less in one year.
Related
*****Shell Script*******
Given a month and the day of the week that's the first of that month, print a calendar for the month. (Remember, number of days in months is different and use \n to go to a new line.)
Unix has a cal command especially for this purpose.
By default, cal shows the current month's calendar.
mayankp#mayank:~/$ cal
November 2018
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30
If you want a calendar for a specific month of a specific year, do this:
mayankp#mayank:~/$ cal 1 2018
January 2018
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
This displays the calendar for January 2018.
So, your shell script would be:(ex: calendar.sh)
#!/usr/bin/env bash
month=$1
year=$2
cal $1 $2
Run the script like this:
mayankp#mayank:~/$ sh calendar.sh 3 2018
March 2018
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Let me know if this helps.
This is my table wys_attendence:
id studid adate amonth ayear acls_id attendence
1 28 02 07 2015 10 1
2 31 02 07 2015 10 0
4 32 02 07 2015 10 1
5 28 13 07 2015 10 0
6 31 13 07 2015 10 1
7 32 13 07 2015 10 1
9 28 14 07 2015 10 1
10 31 14 07 2015 10 1
11 32 14 07 2015 10 1
13 28 15 07 2015 10 1
14 31 15 07 2015 10 0
15 32 15 07 2015 10 1
17 28 16 07 2015 10 0
18 31 16 07 2015 10 1
19 32 16 07 2015 10 1
21 28 17 07 2015 10 1
22 31 17 07 2015 10 1
23 32 17 07 2015 10 0
24 28 20 08 2015 10 1
25 31 20 08 2015 10 1
26 32 20 08 2015 10 0
I want to check if every day of a specific year and month is in the table, and to display all the dates of a selected month and year.
if I am select july 2015.
The output I get is incorrect. This is what I get:
studid 02 02 02 13 13 13 14 14 14 15 15 15 16 16 16 17 17 17
But I want it like this:
studid 2 13 14 15 16 17
my controller code is here
`$amonth = Input::get('amonth');
$ayear = Input::get('ayear');
$teacher_attend=WysTeacherattendance::all();
$attendance = DB::table('wys_teacherattendances')
->where('t_amonth', $amonth)
->where('t_ayear',$ayear)
->get();`
my view.blade.php code is here
<th>studid</th>
#foreach($attendance as $attendances)
<th>{{$attendances->t_adate}}</th>
#endforeach
How can I modify my query to achieve the above result as well as check if days of my selected month and year are in the database or not if in database then day display only once.?
If you want to group records by date then use groupBy() as explained on http://laravel.com/docs/4.2/queries#selects
$attendance = DB::table('wys_teacherattendances')
->where('t_amonth', $amonth)
->where('t_ayear',$ayear)
->groupBy('t_adate')
->get();
How to extract data date wise and do average calculation per date from the below shown output. last column is average.
Sun Jul 5 00:00:02 IST 2015, 97
Sun Jul 5 00:02:01 IST 2015, 97
Sun Jul 5 00:04:02 IST 2015, 97
Mon Jul 6 00:00:01 IST 2015, 73
Mon Jul 6 00:02:02 IST 2015, 93
Mon Jul 6 00:04:02 IST 2015, 97
Tue Jul 7 00:00:02 IST 2015, 97
Tue Jul 7 00:02:02 IST 2015, 97
Tue Jul 7 00:04:01 IST 2015, 97
Wed Jul 8 00:00:01 IST 2015, 98
Wed Jul 8 00:02:02 IST 2015, 98
Wed Jul 8 00:04:01 IST 2015, 98
Thu Jul 9 00:00:02 IST 2015, 100
Thu Jul 9 00:02:01 IST 2015, 100
Thu Jul 9 00:04:01 IST 2015, 100
Fri Jul 10 00:00:01 IST 2015, 100
Fri Jul 10 00:02:02 IST 2015, 100
Fri Jul 10 00:04:02 IST 2015, 100
Sat Jul 11 00:00:01 IST 2015, 73
Sat Jul 11 00:02:01 IST 2015, 73
Sat Jul 11 00:04:02 IST 2015, 73
want output as
Jun 6 - 97
Jun 7 - 86.66
...
You can use this awk:
awk -F ', ' '{
split($1, a, " ");
k=a[2] OFS a[3];
if(!(k in c))
b[++n]=k;
c[k]++;
sum[k]+=$2
}
END{
for(i=1; i<=n; i++)
printf "%s - %.2f\n", b[i], (sum[b[i]]/c[b[i]])
}' file
Jul 5 - 97.00
Jul 6 - 87.67
Jul 7 - 97.00
Jul 8 - 98.00
Jul 9 - 100.00
Jul 10 - 100.00
Jul 11 - 73.00
This should be obvious to all but me,
I have a file full of epoch timestamps and I'm trying to get the records from the last 7 days.
So I tried this in my mac:
PAST=$(gdate -d "7 days ago" +%s)
if [ $timeval -gt $PAST ]; then
do stuff
fi
Example erroneous output:
TS from data, Human fr data, TS from $PAST, Human from $PAST
1419981977690,Tue Jun 9 06 14 50 PST 46967,1421129827,Mon Jan 12 22 17 07 PST 2015
1419400311440,Thu Jan 2 00 17 20 PST 46949,1421129827,Mon Jan 12 22 17 07 PST 2015
1420341489480,Fri Oct 30 06 38 00 PST 46978,1421129827,Mon Jan 12 22 17 07 PST 2015
1421232275040,Thu Jan 22 07 04 00 PST 47007,1421129827,Mon Jan 12 22 17 07 PST 2015
The timestamps in your file are in milliseconds, not seconds. Divide them by 1000.
I have an array of dates e.g.
Fri Jan 28 10:13:19 UTC 2011
Thu Jan 27 16:57:59 UTC 2011
Thu Jan 27 16:41:21 UTC 2011
Wed Jan 26 09:20:48 UTC 2011
Mon Jan 24 16:19:48 UTC 2011
Fri Jan 21 11:45:34 UTC 2011
Fri Jan 21 11:42:19 UTC 2011
How can I group them so the output is as hash with the count of items each day:
Friday 28 => 1
Thursday 27 => 2
Wednesday 26 => 1
Monday 24 => 1
Friday 21 => 2
Or, to put kurumi's solution more verbosely and using Jimmy's strftime:
histogram = dates.inject(Hash.new(0)) do |hist, date|
hist[date.strftime('%A %d')] += 1
hist
end.sort_by{|date, count| date.split(' ').last}.reverse
give us:
Friday 28: 1
Thursday 27: 2
Wednesday 26: 1
Monday 24: 1
Friday 21: 2
OK?
#things.group_by {|thing| thing.strftime "%A %d" }.each do |key, group|
puts "#{key} => #{group.size}"
end
%A is the full weekday name and %d is the day of the month
I can't test this currently but I think it will work.
s=a.inject(Hash.new(0)) do |h,y|
z=y.split
h[ z[0]+z[2] ]+=1
h
end