Linux uptime in days

Bash format uptime to show days, hours, minutes

I’m using uptime in bash in order to get the current runtime of the machine. I need to grab the time and display a format like 2 days, 12 hours, 23 minutes.

8 Answers 8

My uptime produces output that looks like:

$ uptime 12:49:10 up 25 days, 21:30, 28 users, load average: 0.50, 0.66, 0.52 

To convert that to your format:

$ uptime | awk -F'( |,|:)+' '' 25 days, 21 hours, 34 minutes. 

How it works

  • -F'( |,|:)+’ awk divides its input up into fields. This tells awk to use any combination of one or more of space, comma, or colon as the field separator.
  • print $6,$7″,»,$8,»hours,»,$9,»minutes.» This tells awk to print the sixth field and seventh fields (separated by a space) followed by a comma, the 8th field, the string hours, the ninth field, and, lastly, the string minutes. .

Handling computers with short uptimes using sed

Starting from a reboot, my uptime produces output like:

 03:14:20 up 1 min, 2 users, load average: 2.28, 1.29, 0.50 04:12:29 up 59 min, 5 users, load average: 0.06, 0.08, 0.48 05:14:09 up 2:01, 5 users, load average: 0.13, 0.10, 0.45 03:13:19 up 1 day, 0 min, 8 users, load average: 0.01, 0.04, 0.05 04:13:19 up 1 day, 1:00, 8 users, load average: 0.02, 0.05, 0.21 12:49:10 up 25 days, 21:30, 28 users, load average: 0.50, 0.66, 0.52 

The following sed command handles these formats:

uptime | sed -E 's/^[^,]*up *//; s/, *[[:digit:]]* users.*//; s/min/minutes/; s/([[:digit:]]+):0?([[:digit:]]+)/\1 hours, \2 minutes/' 

With the above times, this produces:

1 minutes 59 minutes 2 hours, 1 minutes 1 day, 0 minutes 1 day, 1 hours, 0 minutes 25 days, 21 hours, 30 minutes 

How it works

  • -E turns on extended regular expression syntax. (On older GNU seds, use -r in place of -E )
  • s/^[^,]*up *// This substitutes command removes all text up to up .
  • s/, *[[:digit:]]* users.*// This substitute command removes the user count and all text which follows it.
  • s/min/minutes/ This replaces min with minutes .
  • s/([[:digit:]]+):0?([[:digit:]]+)/\1 hours, \2 minutes/’ If the line contains a time in the hh:mm format, this separates the hours from the minutes and replaces it with hh hours, mm minutes .

Handling computers with short uptimes using awk

On the same test cases as above, this produces:

0 days, 0 hours, 1 minutes. 0 days, 0 hours, 59 minutes. 0 days, 2 hours, 1 minutes. 1 days, 0 hours, 0 minutes. 1 days, 1 hours, 0 minutes. 25 days, 21 hours, 30 minutes. 

For those who prefer awk code spread out over multiple lines:

Thanks for the awk solution. One small improvement when piping a file with several uptimes into it, I needed to reset the variables: cat filewithuptimes.log | awk -F'( |,|:)+’ ‘d=0; h=0; m=0; else >> ‘

@mrvulcan Good observation! I just updated the answer to initialize those variables so that the case of multiple input lines is handled. Thanks.

Читайте также:  Linux изменить тип файла

Just vor completeness. what’s about:

$ uptime -p up 2 weeks, 3 days, 14 hours, 27 minutes 

Solution: In order to get the linux uptime in seconds, Go to bash and type cat /proc/uptime .Parse the first number and convert it according to your requirement.

This file contains information detailing how long the system has been on since its last restart. The output of /proc/uptime is quite minimal:

The First number is the total number of seconds the system has been up.

The Second number is how much of that time the machine has spent idle, in seconds.

Keep in mind that this is Linux specific— it won’t work on other POSIX operating systems (unless they’re designed to mimic Linux)

I made a universal shell script, for systems which support uptime -p like newer linux and for those that don’t, like Mac OS X.

#!/bin/sh uptime -p >/dev/null 2>&1 if [ "$?" -eq 0 ]; then # Supports most Linux distro # when the machine is up for less than '0' minutes then # 'uptime -p' returns ONLY 'up', so we need to set a default value UP_SET_OR_EMPTY=$(uptime -p | awk -F 'up ' '') UP=$ else # Supports Mac OS X, Debian 7, etc UP=$(uptime | sed -E 's/^[^,]*up *//; s/mins/minutes/; s/hrs?/hours/; s/([[:digit:]]+):0?([[:digit:]]+)/\1 hours, \2 minutes/; s/^1 hours/1 hour/; s/ 1 hours/ 1 hour/; s/min,/minutes,/; s/ 0 minutes,/ less than a minute,/; s/ 1 minutes/ 1 minute/; s/ / /; s/, *[[:digit:]]* users?.*//') fi echo "up $UP" 

Referenced John1024 answer with my own customizations.

Источник

Linux Uptime Command

Any Linux distro comes with the “uptime” command. It’s an important command for system administrators to know. It helps troubleshoot the issues related to power and scheduling. Of course, there are other alternative tools available for this purpose but uptime is relatively simple and easy to use.

The content of this guide is as follows:

Let’s start the uptime command.

How Does the Uptime Command Work?

If you use the system’s uptime command via command prompt, you get many benefits out of it. For example, suppose you are facing a problem in connecting to the server. Then, you can easily run the uptime command on the server to check if there has been a recent reboot on the server. This helps in troubleshooting the situation and provides you with better visibility to apply the required solution.

The output specifies the current time. “Up” specifies that the system is up and running along with the total time that the system is up to the user count and the system load averages.

Running the uptime command of a Linux system via the command line, you get a specified output in the following order:

  • The current time of the system.
  • The total uptime of the system.
  • The active users that are currently running the system.
  • The average of the system loads that is available for the past 1, 5, and 15 minutes.

Uptime command comes with various options. To check the options, we can run the “help” command.

Example 1: Uptime in Human-Readable Format

With the use of the “-p” option, you can get a pretty clear output which displays the uptime in the number of days, hours, minutes, and seconds format:

Читайте также:  Sql plus in linux

Example 2: Check the Start Time of the System

Another option is to check the exact time when the system is first started rather than the time spent since it started. Run the following command on the command-line interface with the “-s” option:

Example 3: Check the Version of the Installed Uptime Command

If you want to check the version of the installed uptime package in the system, run the following command with the “-V” option:

The output shows the current version of the “uptime” command.

Conclusion

Linux is a well-known environment and is highly recommended for various projects due to its stability and various configurations. The “uptime” command checks the system information. We explained the “uptime” command with its various options.

About the author

Syed Minhal Abbas

I hold a master’s degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.

Источник

Convert linux sysuptime to well format date

/proc/uptime give me up time of system in seconds, is there a standard solution that convert seconds to this format?

5 Answers 5

You can do this for example with perl and some simple math:

cat /proc/uptime | perl -ne '/(\d*)/ ; printf "%02d:%02d:%02d:%02d\n",int($1/86400),int(($1%86400)/3600),int(($1%3600)/60),$1%60' 

If you do not need the seconds, you can simply run the uptime command. Its output can then be simply transformed to DD:HH:MM.

For example using (works this way only if uptime > 1h)

 uptime | perl -ne '/(\d*) day[^\d]*(\d*):(\d*)/ ; printf "%02d:%02d:%02d\n", $1, $2, $3' 

No, because it’s not really a hard problem. Divide the number of seconds by 86400 using integer division to get the number of days. Take the remainder and divide that by 3600 to get the number of hours. Divide the remainder of that by 60 to get the number of minutes, and you’re left with the number of seconds. All this is doable from the shell using the expr command if your shell is ancient enough to not support arithmetic natively

Taking the number of seconds since the epoch and turning that into a human readable date is a hard problem, and so there are standard ways to do that, e.g. date -r SECONDS from the shell. But that’s a different problem.

Adapting the relevant part of this answer.

As long as your uptime is less than a year, you can use the following command:

TZ=UTC date -d@$(cut -d\ -f1 /proc/uptime) +'%j %T' | awk '' 

I extract the first field of the uptime file, since at least on my Linux, there is a second field containing the total idle time. I treat that as the number of seconds since the epoch, and convert that to day of the year followed by time in hours, minutes and seconds. Since the day of the year starts at one, I subtract one from that in my final awk invocation.

Источник

How to get real uptime?

Is there any easy way to calculate uptime excluding period system is suspended? I would like to count time I’ve spent in front of computer.

var/log/pm-suspend.log, will contain the last awake time ,using this ,uptime and current time you can manually find real uptime with subtraction.Guess this is not the easy way!!

Читайте также:  Работа со screen linux

@Stormvirux I had the same idea, but wish I had found your suggestion first instead of figuring that out six years later!

2 Answers 2

On one hand this answer is six years late, on the other hand it’s a blink of the eye if the internet is eternal!

You can get the real uptime with this little bash script:

$ suspendtime Apr 07 05:53:34 to Apr 07 17:07:17 suspended 11 hours, 13 minutes, 43 seconds Apr 07 21:56:34 to Apr 08 04:20:57 suspended 6 hours, 24 minutes, 23 seconds (. SNIP . ) May 08 05:55:20 to May 08 16:32:37 suspended 10 hours, 37 minutes, 17 seconds May 08 23:21:00 to May 09 07:02:05 suspended 7 hours, 41 minutes, 5 seconds Linux uptime 2,813,939 seconds (4 weeks, 4 days, 13 hours, 38 minutes, 59 seconds) 64 Suspends 1,715,196 seconds (2 weeks, 5 days, 20 hours, 26 minutes, 36 seconds) Real uptime 1,098,743 seconds (1 week, 5 days, 17 hours, 12 minutes, 23 seconds) 

Linux will report uptime as 8 days and 40 minutes. The real uptime (after subtracting suspend time) is about 2 days and 18 hours.

suspendtime bash script

Here’s the code you can copy to your system:

#!/bin/bash # NAME: suspendtime # PATH: $HOME/askubuntu/ # DESC: For: https://askubuntu.com/questions/321855/how-to-get-real-uptime # DATE: November 6, 2019. # NOTE: Calculate suspend time from systemd's journalctl # UPDT: 2019-11-07 Fine-tune removing 0 Units in DaysMinutesStr # 2020-05-09 Add "weeks" unit measure to DaysMinutes() function # Duplicate DaysMinutes from ~/.bashrc for Ask Ubuntu DaysMinutes () < local w d h m s (( w = $/ 604800 )) (( d = $%604800 / 86400 )) (( h = ($%86400) / 3600 )) (( m = ($%3600) / 60 )) (( s = $%60 )) DaysMinutesStr="$w weeks, $d days, $h hours, $m minutes, $s seconds" # Convert 1's to singular [[ $ = "1 " ]] && \ DaysMinutesStr="$" DaysMinutesStr="$" DaysMinutesStr="$" DaysMinutesStr="$" DaysMinutesStr="$" # Suppress zero strings [[ $ = "0" ]] && DaysMinutesStr="$" DaysMinutesStr="$" DaysMinutesStr="$" DaysMinutesStr="$" DaysMinutesStr="$" > # DaysMinutes # Build array of suspend cycles from Systemd IFS=$'\n' Arr=( $(journalctl -b-0 | \ grep -E 'systemd\[1]: Start.*Suspend' | cut -c1-15) ) [[ $ -gt 0 ]] && upper=$(( $ - 1 )) [[ $upper -gt 0 ]] && for (( i=0; i") - $(date +%s -d "$") )) SuspendTime=$(( SuspendTime + Time )) DaysMinutes "$Time" printf "%s to %s suspended%s\n" "$" "$" \ "$DaysMinutesStr" done echo LinuxTime=$(( $(date +%s -d "Now") - $(date +%s -d "$(uptime -s)") )) DaysMinutes "$LinuxTime" printf "Linux uptime %'d seconds (%s)\n" "$LinuxTime" "$DaysMinutesStr" DaysMinutes "$SuspendTime" printf "%s Suspends %'d seconds (%s)\n" \ "$SuspendCount" "$SuspendTime" "$DaysMinutesStr" RealTime=$(( LinuxTime - SuspendTime )) DaysMinutes "$RealTime" printf "Real uptime %'d seconds (%s)\n" "$RealTime" "$DaysMinutesStr" 

About half the program is converting seconds to human readable format in weeks, days, hours, minutes and seconds. The function DaysMinutes does this and was copied from my ~/.bashrc file where you may want to put that function yourself.

How it works

The key component is getting suspend start and end times from journalctl :

$ journalctl -b-0 | grep -E 'systemd\[1]: Start.*Suspend' Oct 31 05:55:19 alien systemd[1]: Starting Suspend. Oct 31 16:54:26 alien systemd[1]: Started Suspend. (. SNIP . ) Nov 07 21:07:28 alien systemd[1]: Starting Suspend. Nov 08 05:08:52 alien systemd[1]: Started Suspend. 
  • The journalctl -b-0 command reads all the system messages for the current boot. You could enhance the function to look at the previous boot using -b-1 the boot before that with -b-2 , etc.
  • The grep command with regex does the heavy lifting returning 32 system messages pertaining to suspend from the 19,330 system messages recorded (on my system)

Источник

Оцените статью
Adblock
detector