How to get start value with Year in Unix/Linux command or any other way python also fine? - linux

i need output with Year in start field, below command i am using in Linux to get the License details but i am getting date like(start Tue 1/7 9:54) , so want to know the year for this:
lmutil lmstat -a -c user#server_name -f abcd -t
Output : start Tue 1/7 9:54
Looking for start Tue 1/7(with year) 9:54
Please help.
Thanks

Related

Convert Zulu timestamp to seconds since epoch and compare it with current time in bash script Mac

I have below timestamp, which I need to change to seconds since epoch in a bash script, Mac OS and compare with current system time.
2021-09-21T06:27:15Z
Current System Time is in IST format. i.e Tue Sep 21 12:20:42 IST 2021
Please suggest a better way to achieve it.
This should give you what you want:
#!/bin/bash
date="2021-09-21T06:27:15Z"
epoch=$(date -d "${date}" +%s)

How to get the time when I last used the computer

In GNU/Linux on xorg session, what I want to do is to get how many seconds have passed since I stopped working with the computer (i.e. no keys pressed and/or cursor moved).
Running in the background, the script below will display secs in the status bar.
But the question is what THE_COMMAND will be.
While true; do
last_touched="$(THE_COMMAND)"
now="$(date +%s)"
secs=$((now - last_touched))
echo "${secs} seconds ago"
sleep 3
done
I remember asking the same question a while back.
Here is what I found,
last -aiF -n 1 userName
command can give you the current session.
When combined with awk you can get the result as follows
$ last -aiF -n2 username
username :1 Wed Apr 21 13:09:00 2021 still logged in 0.0.0.0
username :1 Wed Apr 21 07:28:47 2021 - down (05:39) 0.0.0.0
$ last -aiF -n 2 ogulcan | awk '{print $10}'
in
(05:39)
the lines here are the session times.
These times are counted as now - first boot login
But I believe these does not work best for you.
So here is the 8 year old question that may be helpful to you.
User Idle time in Linux
Using python you can calculate the idle time passed. Maybe this way, you can simply get what you want with python.

Manipulate the system time for a given command

I want to run command with specific date in history.
Let say today is 12.Nov.2019 and I need to run one command with date of 10th. Is there any possibility of setting date on fly ?
date -d '1 day ago' /path/to/command
doesn't work.
Use can you faketime to run a command with given time:
faketime - manipulate the system time for a given command
Because you didn't tell us what command and time you want to run,
I suppose that you want to run /path/to/command on Oct 10th, 2019.
Run /path/to/command in 2 days ago:
faketime -f -2d /path/to/command
Run /path/to/command on Oct 10th, 2019:
faketime -f '2019-11-10' /path/to/command

How to use --since option with docker logs command

I want to look at last 1 hour of docker container log using docker logs --since option. Which value I should provide for --since parameter?
as the help says
--since string Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes
I would do
docker logs mycontainer_or_id --since 60m
This syntax is correct according to my active container
Please refer to the Docker docs.
docker logs --since 1h
The --since option shows only the container logs generated after a given date. You can specify the date as an RFC 3339 date, a UNIX timestamp, or a Go duration string (e.g. 1m30s, 3h). Besides RFC3339 date format you may also use RFC3339Nano, 2006-01-02T15:04:05, 2006-01-02T15:04:05.999999999, 2006-01-02Z07:00, and 2006-01-02.
You may want logs from a specific date, but docker might not like your date's format.
In such cases, check whether the UNIX date command parse it:
$ date -d "your date here"
Wed Oct 5 12:46:17 GMT 2022
If date's output looks right, then you can use date -I to produce a format that docker understands.
$ docker logs my_container --since "$(date -I -d "your date here")" | less -RX

Get Yesterday's date in solaris

I am running SunOS.
bash-3.00$ uname -a
SunOS lvsaishdc3in0001 5.10 Generic_142901-02 i86pc i386 i86pc
I need to find Yesterday's date in linux with the proper formatting passed from command prompt. When I tried like this on my shell prompt-
bash-3.00$ date --date='yesterday' '+%Y%m%d'
date: illegal option -- date=yesterday
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
I always get date illegal option, why is it so?
Is there anything wrong I am doing?
Update:-
bash-3.00$ date --version
date: illegal option -- version
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
Try this below thing. It should work
YESTERDAY=`TZ=GMT+24 date +%Y%m%d`; echo $YESTERDAY
Try this one out:
DATE_STAMP=`TZ=GMT+24 date +%Y%m%d`
where GMT is the time zone and you might need to alter the 24 according to the hours difference you have from GMT. Either that or you can change GMT to a time zone more comfortable to you e.g. CST
As larsks suggested, you can use perl:
perl -e 'use POSIX qw(strftime); print strftime "%a %b %e %H:%M:%S %Y",localtime(time()- 3600*24);'
Slightly modified from
http://blog.rootshell.be/2006/05/04/solaris-yesterday-date/
To get YYYYMMDD format use this
perl -e 'use POSIX qw(strftime); print strftime "%Y%m%d",localtime(time()- 3600*24);'
This link explains how to format date and time with strftime
http://perltraining.com.au/tips/2009-02-26.html
A pure bash solution
#!/bin/bash
# get and split date
today=`date +%Y%m%d`
year=${today:0:4}
month=${today:4:2}
day=${today:6:2}
# avoid octal mismatch
if (( ${day:0:1} == 0 )); then day=${day:1:1}; fi
if (( ${month:0:1} == 0 )); then month=${month:1:1}; fi
# calc
day=$((day-1))
if ((day==0)); then
month=$((month-1))
if ((month==0)); then
year=$((year-1))
month=12
fi
last_day_of_month=$((((62648012>>month*2&3)+28)+(month==2 && y%4==0)))
day=$last_day_of_month
fi
# format result
if ((day<10)); then day="0"$day; fi
if ((month<10)); then month="0"$month; fi
yesterday="$year$month$day"
echo $yesterday
TZ=$TZ+24 date +'%Y/%m/%d' in SunOS.
Playing on Solaris10 with non-GMT environment, I'm getting this:
# date
Fri Jul 26 13:09:38 CEST 2013 (OK)
# (TZ=CEST+24 date)
Thu Jul 25 11:09:38 CEST 2013 (ERR)
# (TZ=GMT+24 date)
Thu Jul 25 11:09:38 GMT 2013 (OK)
# (TZ=CEST+$((24-$((`date "+%H"`-`date -u "+%H"`)))) date)
Thu Jul 25 13:09:38 CEST 2013 (OK)
As You colud see, I have and I want to get CEST , but TZ=CEST+24 giving me wrong CEST data; GMT+24 giving me correct data, but unusable.
To get the proper result, I has to use GMT+22 (wrong command, correct result) or CEST+22 (wrong value, but finnaly correct result for correct TZ)
A pure bash solution given by #olivecoder is very reliable compared to any other solution but there is a mistake to be corrected. when the day fall on 1st of the month the script is failing with date saying "last_day_of_month" in day value. #olivecoder has missed $ in
day=last_day_of_month, that it should be
day=$last_day_of_month;
After this correction it works very good.
Using Timezone -24 is having some issue based on time when use it. in some cases it goes to day before yesterday. So I think its not reliable.

Resources