Calendar months variables in BASH script - linux

I am writing a BASH script that calls an API that presents metrics for specific time-frames. I plan to run the script on a cron job on the 1st of each month, the API call needs to contain the start and end time and be in a epoch format with milliseconds. Milliseconds should be ok to set to 000 as it doesnt need to be that specific but the API requires it.
How can I code the script to look at the current human readable time then look at the exact same time exactly 1 calendar month before, then convert both outputs to epoch, and then enter the epoch times in the curl command as a variable?
Example:
Script runs at 1am on July 1st, script then understands a full calendar month before was June 1st, converts both to epoch, places them into the curl command using variables.
I understand how to get a human readable date for 1 month ago, however I am unsure how best to convert these dates to epoch.
date --date="1 month ago" +"%d%m%Y"

To create a variable using date in epoch times with milliseconds:
ENDDATE=`date +%s%N | cut -b1-13`
STARTDATE=`date --date="1 month ago" +%s%N | cut -b1-13`
Use these variables in the cURL command.

Related

Given YYYY-MM-DD, how to find start date of week & end date of week? [duplicate]

This question already has answers here:
Get Monday and Sunday etc.. for a week for any date as parameter in Unix
(2 answers)
Closed 2 years ago.
Is there a way to use the date function in linux to get the exact date of the start of the week & end of the week for a given YYYY-MM-DD?
For example, I could enter 2020-07-24 and it would return 2020-07-20 (Monday) & 2020-07-26 (Sunday) as start & end date for this particular week respectively.
This shell script should work on most Linux as mostly they use GNU date
It converts the input to epoch seconds and then goes back a day until Monday is
found
#!/bin/bash
# take the parameter from command line
d="$1"
# find the current time as seconds since 1st Jan 1970 (epoch time)
start=$(date -d "$d" '+%s')
consider="$start"
# day of the week for the time we are considering
dow=$(date -d "#$consider" '+%A')
# is the day of the week monday? if not, carry on
while [[ "$dow" != "Monday" ]]; do
# adjust the time to be a day further in the past, 24*60*60 seconds is 1 day
let "consider=$consider - 86400"
dow=$(date -d "#$consider" '+%A')
done
# output the found date
date -d "#$consider"

Jenkinz Job time adjustment

I am working on the Jenkins jobs. There is already a job created with this command. I need someone's help to guide me about the time settings in command line. There are two variables for start and end time. Kindly , can you explain that what will be the start and ending time in simple English
export start_date=$(date -d "yesterday 00:00:00 " +%s)
export end_date=$(date +%s)
From man date:
%s is the number of seconds since 1970-01-01 00:00:00 UTC
So for the start_date, the command is saying grab the date from yesterday, with time 00:00:00. And then once you have that date, then get the number of seconds elapsed from the Unix epoch (01/01/1970) until that date. So for example, today is 4/14/2016, so start_date is equal to the number of seconds from 1/1/1970 00:00:00 to 4/13/2016 00:00:00.
The end_date is the number of seconds elapsed since the Unix epoch (01/01/1970), from right now (the current time).

Date not showing in shell program

I am using this shell program where i need to display date with time for some particular task. For some reason the date is not working and its only showing time.
date +'%H:%M:%S Calling' >> /home/hmm/AppFlatForRent/log.txt #calling
Can anyone tell me whats wrong with it
You're only adding formatting to the date command for the time in hours minutes and seconds (i.e. '%H:%M:%S'). Try:
date +'%Y-%b-%d %H:%M:%S Calling' >> /home/hmm/AppFlatForRent/log.txt #calling

How to run cronjob on every month first friday?

I have cronjob to be run on every month first friday evening
i used the below mentioned entry
00 20 1-7 * Fri [ "$(date '+\%a')" = "Fri" ] && $HOME/path/to/my/script.sh > /dev/null 2>&1
This entry should run my script if Friday falls withing 1-7 day of the month, but my script is getting executed even after 7th (i.e on all Fridays of the month).
Please suggest how to fix it.
This is because when you specify a day of month and day of week, cron will execute the job when EITHER of those constraints are true. From the man page for crontab (5):
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 cur‐
rent 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.
There isn't a direct way in cron to do what you want, but cron : how to schedule to run first Sunday of every month describes a workaround by using cron to run your script e.g. every Friday and then calculating in the script if the day of month is in the range 1-7, and only continuing when that is the case.
In response to the comment about using 5 rather than Fri to specify day of week: using Fri is OK, as the man page says:
Months or days of the week can be specified by name.

how to get day of the year in shell?

How can I get the day of the year in shell?
date '+%V' will give me the week of the year, which is 15 for today; but I need to find the day of the year!
From the coreutils date manual:
%j day of year (001..366)
Use the date command and the %j option...
doy=$(date +%j)
POSIX mandates the format string %j for getting the day of the year, so if you want it for today's date, you are done, and have a portable solution.
date +%j
For getting the day number of an arbitrary date, the situation is somewhat more complex.
On Linux, you will usually have GNU date, which lets you query for an arbitrary date with the -d option.
date -d '1970-04-01' +%j
The argument to -d can be a fairly free-form expression, including relative times like "3 weeks ago".
date -d "3 weeks ago" +%j
On BSD-like platforms, including MacOS, the mechanism for specifying a date to format is different. You can ask it to format a date with -j and specify the date as an argument (not an option), and optionally specify how the string argument should be parsed with -f.
date -j 04010000 +%j
displays the day number for April 1st 00:00.
The string argument to specify which date to examine is rather weird, and requires the minutes to be specified, and then optionally allows you to prefix with ((month,) day, and) hour, and optionally allows year as a suffix (sic).
date -j -f "%Y-%m-%d" 1970-04-01 +%j
uses -f format date to pass in a date in a more standard format, and prints the day number of that.
There's also the -v option which allows you to specify relative times.
date -j -v -3w +%j
displays the day number of the date three weeks ago.
If you are looking for a proper POSIX-portable solution for getting the day number of arbitrary dates, the least unattractive solution might be to create your own program. If you can rely on Python or Perl (or GNU Awk) to be installed, those make it relatively easy, though it's still a bit of a chore using only their default libraries.

Resources