LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 06-18-2016, 05:45 AM   #16
kkrrss
Member
 
Registered: Jun 2016
Posts: 56

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled

Hi Again,

Please help to get below out put.

DOWNTIME=`date +%s`
UPTIME =`date +%s`

I want to take output of ($UPTIME - $DOWNTIME) in minutes and seconds..

Eg : Total downtime was 20 minutes and 10 seconds
 
Old 06-18-2016, 06:41 AM   #17
kkrrss
Member
 
Registered: Jun 2016
Posts: 56

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Lets say I have below two variables.

Code:
!#/bin/bash
MEMFILE=lock-file

date +%s > $MEMFILE

sleep 130

UPTIME= `date +%s`
I want to take the output of ( $UPTIME - $MEMFILE) in minutes and seconds.

Eg:

"Total downtime was 2 minutes and 5 seconds"

Please help me...
 
Old 06-18-2016, 07:33 AM   #18
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,309
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by kkrrss View Post
I want to take the output of ( $UPTIME - $MEMFILE) in minutes and seconds.
It's a little easier to work with command substitution is you use $( ... ) instead of backticks. You can do arithmetic in the shell using $(( ... )) and can use addition, subtraction, multiplication, division, exponents, and modulus.

Here is a variant of your script set to do the first part of the calculation to get seconds, in preparation for then calculating the minutes.

Code:
#!/bin/sh

memfile=lock-file

date +%s > $memfile

sleep 130

then=$(head -n 1 $memfile)
now=$(date +%s)

downtime=$(( $now - $then ))

seconds=$(( $downtime % 60 ));

echo Downtime was X minutes and $seconds seconds
Can you see how to modify it to get minutes?
 
2 members found this post helpful.
Old 06-18-2016, 08:11 AM   #19
kkrrss
Member
 
Registered: Jun 2016
Posts: 56

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Thumbs up

Hello Turbocapitalist,

Yeah.. I got it.


Minutes=$(( $downtime / 60 ));

Thanks a lot for your support. and for everyone who have supported me out...
 
  


Reply



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
How to make this script to a service. deepcore Linux - Software 1 05-18-2014 10:14 AM
Web service test using Perl/Shell jack.sully Programming 9 03-13-2014 07:59 AM
Script for qmail service monitor. s3cr3t Linux - Newbie 3 06-05-2009 10:16 PM
Simple script to monitor POP3, SMTP service s3cr3t Solaris / OpenSolaris 1 05-29-2009 12:33 PM
Configuring Shell Script as service/daemon nkanthikiran Red Hat 2 11-07-2006 05:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 11:33 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