LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-28-2017, 03:28 AM   #1
boby.kumar
Member
 
Registered: Mar 2013
Posts: 94

Rep: Reputation: Disabled
Calculate time difference without 'date -d` in Solaris


Hi,

I am writing a script to calculate the time difference of a log file from the current time and want to check "No traffic since x min".

I am working on Solaris.

localhost@server: uname -a
SunOS server 5.10 Generic_147147-26 sun4v sparc SUNW,Netra-T2000

date -d "2017-07-27 08:17:40" '+%s'
date: illegal option -- d
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]


Quote:
#!/usr/bin/bash
#
ABC () {
#now=`date +"%Y-%m-%d %H:%M:%S"`
now=`date +"%Y%m%d%H%M%S"`
time=10
sec=60
file=`ls -lshartE $1 | awk '{print $7 $8}' | cut -c1-18 | tr -d '-' | tr -d ':'`
#file=`ls -lshartE $1 | awk '{print $7" "$8}' |cut -c1-19`


DIFF=$((now - file))
HR=`expr $DIFF / $sec`


echo $now
echo $file
echo $DIFF
echo $HR
}
ABC $1

#OutPut
20170728082308
20170728070945
11363
189
OutPut is not correct

Can anyone tell me what is missing in the above code
 
Old 07-28-2017, 06:53 AM   #2
aragorn2101
Member
 
Registered: Dec 2012
Location: Mauritius
Distribution: Slackware
Posts: 567

Rep: Reputation: 301Reputation: 301Reputation: 301Reputation: 301
I suggest you use the %s switch. It gives you the time in seconds since a fixed reference in the past.

e.g.
Code:
> date --date="2017-07-27 10:25:43" +"%s"
1501136743

> date --date="2017-07-28 16:21:42" +"%s"
1501244502
So, in the above, if you subtract these two numbers you'll get the number of seconds having elapsed between 2017-07-27 10:25:43 and 2017-07-28 16:21:42. Then you go ahead calculate further.
 
Old 07-28-2017, 09:12 AM   #3
boby.kumar
Member
 
Registered: Mar 2013
Posts: 94

Original Poster
Rep: Reputation: Disabled
this command is not working..

Quote:
date: illegal option -- date=2017-07-27 10:25:43
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
 
Old 07-28-2017, 03:04 PM   #4
JeremyBoden
Senior Member
 
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,950

Rep: Reputation: 513Reputation: 513Reputation: 513Reputation: 513Reputation: 513Reputation: 513
You are using date as a variable, but it is also a command.
This is confusing!
Code:
DATE="2017-07-27 10:25:43"
should work.
You can check with
Code:
echo $DATE
You need the quotes because of the special characters, especially embedded spaces.
 
Old 07-28-2017, 04:03 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143
edit: missed part of the original question.

The fundamental problem is that your system doesn't use the standard "date" program, it uses some other stripped down version that doesn't support the -d flag. Can you download the source code for date and compile it on your machine?

Last edited by suicidaleggroll; 07-28-2017 at 04:05 PM.
 
Old 07-28-2017, 04:04 PM   #6
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: Rocky 9.5
Posts: 5,832

Rep: Reputation: 2262Reputation: 2262Reputation: 2262Reputation: 2262Reputation: 2262Reputation: 2262Reputation: 2262Reputation: 2262Reputation: 2262Reputation: 2262Reputation: 2262
Quote:
Originally Posted by boby.kumar View Post
this command is not working..
Code:
date: illegal option -- date=2017-07-27 10:25:43
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff] 
Your answer is in the error message. The bolded text is a complete summary of Solaris' date command.
Don't use -d or --date, just
Code:
date +"%Y-%m-%d %H:%M:%S"
(I can't test that, tho. -- not running Solaris)

Last edited by scasey; 07-28-2017 at 04:06 PM.
 
Old 07-30-2017, 06:17 PM   #7
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: Rocky 9.5
Posts: 5,832

Rep: Reputation: 2262Reputation: 2262Reputation: 2262Reputation: 2262Reputation: 2262Reputation: 2262Reputation: 2262Reputation: 2262Reputation: 2262Reputation: 2262Reputation: 2262
Quote:
Originally Posted by boby.kumar View Post
Hi,

I am writing a script to calculate the time difference of a log file from the current time and want to check "No traffic since x min".

I am working on Solaris.


Can anyone tell me what is missing in the above code
If you can parse the log file for the last date, you can do something like this:
Code:
#!/bin/bash

# check smtpd log for timestamp
# email if the last entry is old

# save off the previous time file
mv /root/bin/lastmsg.txt  /root/bin/prevlastmsg.txt

# get the current last message time from the logs
tail -1 /var/log/qmail/smtpd/current | /usr/local/bin/tai64nlocal | cut -b1-19 > /root/bin/lastmsg.txt

# Run the test
TIMETEST=`diff /root/bin/lastmsg.txt /root/bin/prevlastmsg.txt`

# get the values
LASTMSG=`cat /root/bin/prevlastmsg.txt`
NOW=`cat /root/bin/lastmsg.txt`

## if the diff results in an empty file, then there is a problem.  Send an email
if [[ -z $TIMETEST ]]
        then
        mailx -s "Mail problem? Last message: $LASTMSG Now: $NOW" support@mydomain.com < /root/bin/checkmail.msg
fi
This script will send an email if there have been no log entries from the smptd server in the last 10 minutes -- it's run by cron every 10 minutes.
Note that it doesn't check for the time "now" -- it just gets the date of the last log entry and compares that to the last date it saw, so it doesn't use the date command at all.

You'd need to change the log file name to the log you want to check, and modify the cut command to snip out the date on the last line.

The body of the email looks like this:
Code:
 more /root/bin/checkmail.msg
A check of the smtpd/current log shows no message received since the last check.
Since that was ten minutes ago, perhaps there is a qmail problem???

Last edited by scasey; 07-31-2017 at 03:34 PM.
 
Old 08-02-2017, 04:25 AM   #8
aragorn2101
Member
 
Registered: Dec 2012
Location: Mauritius
Distribution: Slackware
Posts: 567

Rep: Reputation: 301Reputation: 301Reputation: 301Reputation: 301
Quote:
Originally Posted by scasey View Post
Don't use -d or --date, just
Code:
date +"%Y-%m-%d %H:%M:%S"
(I can't test that, tho. -- not running Solaris)
OP already has the date in this format and he's trying to calculate time differences. So a direct numeric format with "%s" would be better.

Quote:
Originally Posted by boby.kumar View Post
Code:
date: illegal option -- date=2017-07-27 10:25:43
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
The date fed to option --date is supposed to be a single string, i.e. it should have been quoted: " ".

Anyway boby.kumar, returning to your first post, modify your script to use the date +%s command directly. We don't know what your command line arguments are for the script, but if you use date +%s everywhere you can do easy math with all the time values you get.
 
Old 08-02-2017, 05:45 AM   #9
blastwave
LQ Newbie
 
Registered: Jan 2017
Location: Canada
Distribution: Debian, RHEL, Solaris, various others and LFS
Posts: 19

Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
edit: missed part of the original question.

The fundamental problem is that your system doesn't use the standard "date" program, it uses some other stripped down version ...
Actually the issue is the opposite. He is running Solaris which is POSIX.1-2001 strict compliant. The "date" function in Linux is not standard. So the version in Solaris is correct whereas the version in Linux is all of that plus a pile of non-standard extensions. This is a common misconception however and you would need to check the specifications to see this :

see http://pubs.opengroup.org/onlinepubs...ties/date.html

The Open Group Base Specifications Issue 7
IEEE Std 1003.1-2008, 2016 Edition
Copyright © 2001-2016 The IEEE and The Open Group


date - write the date and time
 
1 members found this post helpful.
Old 08-02-2017, 06:01 AM   #10
blastwave
LQ Newbie
 
Registered: Jan 2017
Location: Canada
Distribution: Debian, RHEL, Solaris, various others and LFS
Posts: 19

Rep: Reputation: Disabled
Not sure of this helps but it may be easiest to diff the unix timestamp between the log and timenow. You can get unixtime from awk like so :

$ /usr/xpg4/bin/awk 'BEGIN{srand(); print srand()}'
1501667971

This is because POSIX awk will see srand() with the current timestamp.

You can get a version and highly precise time of a file from ls with :

$ ls -lapbE /var/log/syslog
-rw-r--r-- 1 root sys 0 2017-07-10 03:10:02.612417954 +0000 /var/log/syslog

Now the hard part is to convert that time to unixtime and then just diff the two numbers.
I have not figured out the last part yet.
 
Old 08-02-2017, 06:00 PM   #11
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: Rocky 9.5
Posts: 5,832

Rep: Reputation: 2262Reputation: 2262Reputation: 2262Reputation: 2262Reputation: 2262Reputation: 2262Reputation: 2262Reputation: 2262Reputation: 2262Reputation: 2262Reputation: 2262
The last time (a couple of years ago) I worked on/with Solaris, there was a directory in the path that had gnu versions of utilities, including date, where it was called gdate. I don't know if that was standard with Solaris installs or specific to the shop; that is, added by the admins.
Was that /usr/xpg4/bin ? That looks familiar.

Am I correct in remembering that the "%s" is not an option for the Solaris date command?

I see that -E is not valid for gnu. Is that the option that give the detailed date?

I wonder if the OP will ever come back...there are lot's of possible solutions here.
 
Old 08-03-2017, 01:38 AM   #12
blastwave
LQ Newbie
 
Registered: Jan 2017
Location: Canada
Distribution: Debian, RHEL, Solaris, various others and LFS
Posts: 19

Rep: Reputation: Disabled
Quote:
Originally Posted by scasey View Post
The last time (a couple of years ago) I worked on/with Solaris, there was a directory ...
It is a flaw and a mistake to depend on anything that is not in the strict specifications.
Thus the "GNU extensions" are an error.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] File time stamp and time as printed by date showing difference MahendraL Linux - Newbie 9 04-19-2015 06:02 AM
[SOLVED] How to Calculate Time Difference? santosh0782 Linux - Newbie 9 02-22-2014 09:57 PM
How to get Kernel space time and compare to userspace to calculate difference? sindhu4sind Programming 1 06-05-2012 01:09 PM
how to calculate time difference in milliseconds in C/C++ waqasdaar Programming 19 03-17-2009 05:02 PM
calculate time difference between 2 formatted timestamps nickleus Linux - General 3 05-12-2006 11:08 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration