2

How to get the uptime from. The terminal. And in a bash file but only the uptime no other infomAtion

I would like it to be on one line

6 Answers 6

2

When in a terminal just run the following command.

uptime

This will display your current uptime plus a little ekstra information.

Mine says.

08:44:21 up 1:05, 2 users, load average: 0,00, 0,01, 0,05

So my uptime is 1 hour and 5 minutes. In a bash script to only get the uptime I'd use

uptime | awk '{print $3}'

This will show only the 3rd section of the uptime command.

2
  • 1
    Be careful what the third section is. Since I have an uptime of '3 days, 1:41', I get just the number 3.
    – muru
    Dec 17, 2014 at 11:46
  • Ah okay. Thought it was in the same column for displaying that. Dec 18, 2014 at 11:52
1

Use echo $SECONDS , gives you the uptime of a terminal in seconds.

0

You can also use the top command to know the uptime.

top

In the top of the terminal, you can view the uptime status.

2
  • While this is true, it is a continuously ongoing command, so it's would not fit well with a bash script. Dec 17, 2014 at 8:02
  • @DanJohansen top has a batch mode, which I'd say is meant for scripts.
    – muru
    Dec 17, 2014 at 11:47
0
cat /proc/uptime | awk '{print $1}'

will give you the uptime expressed in seconds.

0

This will show you when your system has booted and print uptime in brackets also:

last -x | grep boot | head -n 1 
0

You can get the up time using different methods:

  • using w command

    $ w
    
    11:12:21 up 22:08,  3 users,  load average: 0.28, 0.32, 0.40
    

    If you just want the time nothing else:

    $ w | grep up | awk '{print $3}'
    
    22:08,
    
  • using uptime as described in other answer

  • using top as described in other answer

  • using utmp, you can see since when you are on:

    $ last utmp
    
    wtmp begins Mon Jun  1 08:29:04 2015
    

utmp maintains a full accounting of the current status of the system, system boot time (used by uptime), recording user logins at which terminals, logouts, system events.

3
  • what is exactly utmp? Just curious because it shows me June 11th, it's like my computer hasn't been turned off since then?
    – JoKeR
    Jun 26, 2015 at 8:29
  • 1
    Check the definition in the answer
    – Maythux
    Jun 26, 2015 at 8:31
  • 1
    Indeed you use utmp in your answer :) last command read from utmp wtmp and btmp
    – Maythux
    Jun 26, 2015 at 8:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .