How to use ubuntu command line to find a holiday? - unix

How to use ubuntu command to find the date of Mother's Day (Second Sunday of May) in 2017?
Since -v doesn't work on ubuntu, it works on Mac.

Lets say its usually the second sunday of may :
fhenri#machine:~$ date -v1d -v5m -v+1y -v+2w -v-sun
Dim 14 mai 2017 22:56:28 CEST
you should look at man date to get more examples and to find out if you need another particular sunday or fix day
I was running on my macos (freeBSD based) and did not realized date was not uniform across *nix system.
on ubuntu, it revealed a bit more difficult but here how I would go
vagrant#ubuntu:~$ firstofmay=$(date -d '05/01/2017')
vagrant#ubuntu:~$ firstsunday=$(date -d "$firstofmay" '+%Y-%m')-$(( 8 - $(date -d "$firstofmay" '+%u') ))
vagrant#ubuntu:~$ secondsunday=$(date -d "$firstsunday + 1 week" '+%Y-%m-%d')
vagrant#ubuntu:~$ echo $secondsunday
2017-05-14
first I initialize the 1st of may as my base date
then I get a format of the first sunday from this month: day are 7 based and $(date -d "$firstofmonth" '+%u') will be the day number of the first of may (i.e.: monday is 1); this gives a format such as YYYY-MM-dd
adding 1 week to this date I have previously gives the correct date - adjust the formatting if you want it different

Related

Cron job run at the wrong time in AIX 7.1

I had configured my cronjob to run in every first Monday of the month 8:40am as below
40 08 1-7 * 1 /fs/test/testtime.sh
But it not only run on Monday, it also run on today which is Tuesday.
Is there anything i miss out?
From the man page for crontab (my emphasis):
Note: The day of a command's execution can be specified by two fields - day of month, and day of week. If both fields are restricted (i.e., aren't *), the command will be run when either field matches the current time.
For example, 30 4 1,15 * 5 would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday.
So, in your case, the job runs on every one of the first seven days in each month, plus every Monday.
You can do what you wish by adding an AND condition in the command rather than relying on an OR condition in the time specification, something like:
40 08 1-7 * * test $(date +\%u) -eq 1 && /fs/test/testtime.sh
This will run the actual cron job on all those days (first seven days in each month) but the payload (the script) will only run if the day is Monday.

run previous date script from crontab

I made a small script to take take previous date as argument. The script is running fine but when running this script from crontab it takes todays date
Script
Previous_day=`date --date="-1 days" +% Y% m% d`;
./some_script -date $Previous_day ;
Some_script is script which takes date as argument provided using -date
This is solaris box
Dont know why when running from crontab it runs with argument as today's date
% is a special character in Cron, so you should escape the date parameters:
Previous_day=`date --date="-1 days" +\%Y\%m\%d;

ps utility does not show the year a process was started

I am doing some work on a fairly old system and need to know when a couple of processes started. When I use "ps -ef" one says October 18 and the other March 23. We haven't got to October 18 this year so I'm not sure if this is October 18 last year or the previous year. The uptime command is showing 2419 days (6.6 years!) so it's possible the Oct 18 is from earlier than 2012. This is a HP-UX system. I have done a bit of googling and none of the answers I came across worked, eg ps -o, looking in the /proc dir.
cfgmgr 9947 9943 3 Mar 23 ? 6831:32 /home/cfgmgr/bin/snmpagt
root 24338 1 0 Oct 18 ? 2628:13 /usr/sbin/snmpdm -tcplocal
PS can only show the date because as per PS documentation at MAN page
'Only the year will be displayed if the process was not started the same year ps was invoked, or "mmmdd" if it was not started the same day, or "HH:MM" otherwise.'

PowerShell script with specific date/time range

I am trying to run a powershell script for exchange 2010 to pull information from a specific date/time range.
example: show me amount of received emails from monday-friday time range 6am-11pm
get-messagetrackinglog -resultsize unlimited -Recipient TEST#TEST.COM -Server EXCHANGE -Start "3/4/2013 6:00:00 AM" -End "3/6/2013 23:00:00 PM" | select messageid -unique | measure
but I would like to make the date range not so static. so If i run the script at 11pm on Friday night, every week, how can i get it to do this query for the last 5 days.
I was trying adding in (get-date).adddays(-5) but I can't figure out how to add that in.
any help will be greatly appreciated.
Try this
for($i=-4,$i -lt 0,$i++){
$start = (get-date -hour 6).adddays($i);
$end = (get-date -hour 23).adddays($i);
Write-host $start.DayoftheWeek (get-messagetrackinglog -resultsize unlimited -Recipient TEST#TEST.COM -Server EXCHANGE -Start $start -End $end | select messageid -unique | measure).count
}
The get-messagetrackinglog cmdlet only takes a single argument for -start and -end, so you can't specify that in a single command.
You can run 5 separate queries of the messagetracking logs from 6AM-11PM, each on different days and aggregate those results together, or you can do one query for all the logs from 6AM on the first day to 11PM on the last day, then filter out the ones that are timestamped between 11PM and 6AM in the interim days.
Just trying to answer the part about the past date and hour and assuming you are running this at 11pm but want to go back to 6am (17hrs diff)...
Maybe use something like:
$past=(Get-Date).adddays(-5).addhours(-17)
And then try
-start $past -end (get-date)

Schedule Task : 6 months from now

I'm tryin' to find a way (from a batch file) that I can use to create a scheduled task that will execute 6 months from now. I've looked all over the net, and I'll I've come across is AT and SCHTASKS that will schedule the task monthly ..
I'm looking for a solution that will execute this task every 6 months.
I know this will require some time/date manipulation .. finding the month (number of month) and then adding 6 to it; once it passes '12' it goes back to 1 etc .. Then lining those numbers up with the months name, and using SCHTASKS or AT to schedule the task.
I know how to use SCHTASKS to schedule a task from a batch file; I just need the code to find the month, add 6 to it, grab the months name in 6 months time, and put that into a variable so I can parse it into the SCHTASKS.
I hope this all makes sense.
My english is not so great.
TIA,
Greg
Thanks to everyone who replied, and offered suggestions. I slept on it, woke up with the flu, and had a brainwave. Funny how things work out. I know this is overkill, and someone will come up with a better suggestion, but here goes anyway ..
REM Grab month number and put into variable
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
REM Six months from now
set /a addmm=%mm% + 6
if %addmm% gtr 6 (set /a sixmonths=%addmm% - 12)
REM Determine month name
if %sixmonths%==1 (set monthname=JAN)
if %sixmonths%==2 (set monthname=FEB)
if %sixmonths%==3 (set monthname=MAR)
if %sixmonths%==4 (set monthname=APR)
if %sixmonths%==5 (set monthname=MAY)
if %sixmonths%==6 (set monthname=JUN)
if %sixmonths%==7 (set monthname=JUL)
if %sixmonths%==8 (set monthname=AUG)
if %sixmonths%==9 (set monthname=SEP)
if %sixmonths%==10 (set monthname=OCT)
if %sixmonths%==11 (set monthname=NOV)
if %sixmonths%==12 (set monthname=DEC)
REM Schedule Task
schtasks /create /TN TuneUpReminder /RU system /TR TuneUpReminder.bat /SC MONTHLY /M %monthname%
Why not use the Windows Scheduler?
Most of the information you need is included in this article: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/schtasks.mspx?mfr=true
You can use the monthly setting, with a value of 6 surely?
Here's a crazy idea: in case you want this script on a server (which isn't rebooted), you could write a batch file which waits for six months and then executes the given program. To wait for a specific number of seconds you could abuse the 'ping' command, like this:
ping -n %SECS% localhost > NUL
This command will effectively pause for %SECS% seconds and then return. To wait for six months, simply wait for something like (365 / 2) * 24 * 60 * 60 seconds.
Here's a little batch file which implements this idea:
#echo off
set CMD=echo Half a year elapsed
set /a SECS_IN_HALF_A_YEAR=365 / 2 * 24 * 60 * 60
loop_start:
ping -n %SECS_IN_HALF_A_YEAR% localhost > NUL
%CMD%
goto loop_start
I'm not saying it's pretty, but I thought it's a funky idea. Maybe some food for thought. :-)
you can download coreutils for windows . Then use date command like this
C:\test>gnu_date "+%Y%m%d" -d "6 months"
20110404
(it is renamed to gnu_date.exe )
I have not played with schtasks, but depending on what format of date it uses, you can change the parameters to suit schtasks
C:\test>gnu_date "+%Y-%m-%d" -d "6 months"
2011-04-04
If you need time as well
C:\test>gnu_date "+%Y-%m-%d-%H:%M:%S" -d "6 months"
2011-04-04-18:12:35
Use a for loop to save the date to a variable as desired. then pass it to schtasks for scheduling
Since Greg's answer made me cry inside, here is some array like syntax:
for /F "tokens=%sixmonths%" %%A IN ("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC") DO set monthname=%%A
And since there’s always more than one way to skin a bat[ch]?
set /a sixmonths=%sixmonths% * 4
set months=666 JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC
setlocal ENABLEDELAYEDEXPANSION
set monthname=!months:~%sixmonths%,3!
REM Optional: setlocal DISABLEDELAYEDEXPANSION

Resources