Calculate time difference without 'date -d` in Solaris
Linux - NewbieThis 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.
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.
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.
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 03:05 PM.
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???
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.
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 :
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.