LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Make shell script to monitor a web service. (https://www.linuxquestions.org/questions/linux-server-73/make-shell-script-to-monitor-a-web-service-4175582342/)

kkrrss 06-18-2016 05:45 AM

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

kkrrss 06-18-2016 06:41 AM

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...

Turbocapitalist 06-18-2016 07:33 AM

Quote:

Originally Posted by kkrrss (Post 5562882)
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?

kkrrss 06-18-2016 08:11 AM

Hello Turbocapitalist,

Yeah.. I got it.


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

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


All times are GMT -5. The time now is 10:25 PM.