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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
07-28-2017, 03:28 AM
|
#1
|
Member
Registered: Mar 2013
Posts: 94
Rep:
|
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
|
|
|
07-28-2017, 06:53 AM
|
#2
|
Member
Registered: Dec 2012
Location: Mauritius
Distribution: Slackware
Posts: 567
|
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.
|
|
|
07-28-2017, 09:12 AM
|
#3
|
Member
Registered: Mar 2013
Posts: 94
Original Poster
Rep:
|
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]
|
|
|
|
07-28-2017, 03:04 PM
|
#4
|
Senior Member
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,950
|
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 You need the quotes because of the special characters, especially embedded spaces.
|
|
|
07-28-2017, 04:03 PM
|
#5
|
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573
|
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.
|
|
|
07-28-2017, 04:04 PM
|
#6
|
LQ Veteran
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: Rocky 9.5
Posts: 5,832
|
Quote:
Originally Posted by boby.kumar
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.
|
|
|
07-30-2017, 06:17 PM
|
#7
|
LQ Veteran
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: Rocky 9.5
Posts: 5,832
|
Quote:
Originally Posted by boby.kumar
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.
|
|
|
08-02-2017, 04:25 AM
|
#8
|
Member
Registered: Dec 2012
Location: Mauritius
Distribution: Slackware
Posts: 567
|
Quote:
Originally Posted by scasey
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
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.
|
|
|
08-02-2017, 05:45 AM
|
#9
|
LQ Newbie
Registered: Jan 2017
Location: Canada
Distribution: Debian, RHEL, Solaris, various others and LFS
Posts: 19
Rep:
|
Quote:
Originally Posted by suicidaleggroll
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.
|
08-02-2017, 06:01 AM
|
#10
|
LQ Newbie
Registered: Jan 2017
Location: Canada
Distribution: Debian, RHEL, Solaris, various others and LFS
Posts: 19
Rep:
|
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.
|
|
|
08-02-2017, 06:00 PM
|
#11
|
LQ Veteran
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: Rocky 9.5
Posts: 5,832
|
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.
|
|
|
08-03-2017, 01:38 AM
|
#12
|
LQ Newbie
Registered: Jan 2017
Location: Canada
Distribution: Debian, RHEL, Solaris, various others and LFS
Posts: 19
Rep:
|
Quote:
Originally Posted by scasey
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.
|
|
|
All times are GMT -5. The time now is 04:53 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|