Hot answers tagged

72

On my system it gets the uptime from /proc/uptime: $ strace -eopen uptime open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3 open("/lib/libproc-3.2.8.so", O_RDONLY|O_CLOEXEC) = 3 open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3 open("/proc/version", O_RDONLY) = 3 open("/sys/devices/system/cpu/online", O_RDONLY|O_CLOEXEC) = 3 ...


30

uptime If you want it in numerical form, it's the first number in /proc/uptime (in seconds), so the time of the last reboot is date -d "$(</proc/uptime awk '{print $1}') seconds ago" The uptime includes the time spent in a low-power state (standby, suspension or hibernation).


12

If you have a separate server to run your check script on, something like this would do a simple Ping test to see if the server is alive: #!/bin/bash SERVERIP=192.168.2.3 NOTIFYEMAIL=test@example.com ping -c 3 $SERVERIP > /dev/null 2>&1 if [ $? -ne 0 ] then # Use your favorite mailer here: mailx -s "Server $SERVERIP is down" -t ...


11

Load is not equal to CPU usage. It is basically an indicator how many processes are waiting to be executed. Some helpful links: http://superuser.com/questions/23498/what-does-load-average-mean-in-unix-linux http://blog.scoutapp.com/articles/2009/07/31/understanding-load-averages


11

You can use uptime or last To see only the last time last reboot -F | head -1 | awk '{print $5,$6,$7,$8,$9}' more generically last reboot Note and warning The pseudo user reboot logs in each time the system is rebooted. Thus last reboot will show a log of all reboots since the log file was created.


10

As long as I know, uptime uses /proc/uptime to calculate system uptime. You can see it more clearly in the source code uptime.c FILE *fp; fp = fopen ("/proc/uptime", "r"); if (fp != NULL) { char buf[BUFSIZ]; char *b = fgets (buf, BUFSIZ, fp); if (b == buf) { char *end_ptr; double upsecs = c_strtod ...


9

Here's a way without perl: awk '{printf("%d:%02d:%02d:%02d",($1/60/60/24),($1/60/60%24),($1/60%60),($1%60))}' /proc/uptime


8

As log-files are usually deleted after some time, the total up-time is difficult to get. If the hard disk is as old as the PC, the RAW value (last number) of smartctl -a /dev/sda | grep Power_On_Hours could give a rough estimate how many hours the PC was used.


8

In general, on a RPM-based distribution like Fedora, you can find the name of the package which provides a given command with rpm -qf /path/to/command. Like this: $ rpm -qf $( which uptime ) procps-3.2.8-18.20110302git.fc16.x86_64 You can then download the source RPM with yumdownloader --source procps. (yumdownloader comes from the yum-utils package, if ...


8

I usually use who -b, which produces output such as: $ who -b system boot 2014-05-06 22:47 $ It tells me the date and time when the machine was last booted, rather than the time that has elapsed since it was last booted. This command works on many other Unix systems too (Solaris, …).


7

Uptime is part of the 'procps' package, the upstream source is at http://procps.sourceforge.net/ (Not a fedora user, so not sure where to find their .src.rpm). To answer the question you didn't ask, however; take a look in /proc/uptime The first number is seconds since boot. You should be able to turn that into something usable fairly easily :)


7

On a standard UNIX system (based on the original sources *), uptime reads /var/adm/utmpx and checks for the last time of reboot entry. In other words: this is retrieving the date you also get with who -b and then computes the time since then. *) uptime is a link to the w program and was introduced by BSD around 1980.


6

On any POSIX-compliant system, you can use the etime column of ps. LC_ALL=POSIX ps -o etime= -p $PID The output is broken down into days, hours, minutes and seconds with the syntax [[dd-]hh:]mm:ss. You can work it back into a number of seconds with simple arithmetic: t=$(LC_ALL=POSIX ps -o etime= -p $PID) d=0 h=0 case $t in *-*) d=${t%%-*}; t=${t#*-};; ...


6

Idle time is the sum of all your CPU/core idle times, while uptime is the wall-clock time your system has been up. I'm guessing you have four CPUs/cores/threads.


5

If you want to keep track of this -from now on- you can use uptimed It's available as package in Ubuntu and Fedora and probably other distributions as well.


5

uptimed One such tool that I came across many years ago is called uptimed. The project site is here: http://podgorny.cz/moin/Uptimed. This is a pretty straightforward install, given uptimed appears to be in most of the major distros' repositories. Installation $ sudo yum install uptimed Once installed the service needs to be configured so that it will ...


5

Pinging is an option, but on many occasions a machine will be able to send a ping reply, while the actual server that it is all about is down. It is better do an end-to-end test. In the below example a page is requested from the webserver. If it is a webserver, it would look something like this: #!/bin/bash wget -qO /dev/null ...


5

It depends on the system you are talking about. For Linux-based systems, you probably are using uptime from procps, which reads the data from /proc/uptime. To see this, read the source-code in its Git repository, e.g., uptime.c, which uses proc/sysinfo.c. According to CentOS documents 3.2.30. /proc/uptime This file contains information detailing ...


4

Load average is the number of processes who are in waiting queue for CPU time. These three values are the average number of processes in waiting queue for past 1, 5 and 15 minutes. I would suggest you to read the following articles: Linux Troubleshooting, Part I: High Load Examining Load Average I personally believes more on CPU IDLE Time than on load ...


4

The output of uptime depends on the uptime itself, i.e. On one system $ uptime 17:35pm up 5 days 9:24, 9 users, load average: 0.30, 0.28, 0.28 thus, 12 fields. On another system uptime 17:36:15 up 8:44, 2 users, load average: 0.09, 0.30, 0.41 And thus 10 fields. It may defer for your system, of course. I suppose that you switched on your ...


4

You can use the following 2 commands (who & last) to find out the last time the system was rebooted and also messages about previous shutdown or runlevel changes. Last time system booted? For this you can use the who command. Specifically with the -b switch. $ who -b system boot 2013-08-01 17:56 This says the last time the system was ...


3

If you have some server, use commands like: $ uptime $ w $ last HDD SMARTCTL is also good try. Also from installation could be information about date created of some files in filesystem. Think about, which files were created with installing of your system? If you have linux, look at the date of creation of your /root/ directory. If you have windows, ...


3

Here is how I solved the same problem #!/bin/bash NOTIFYEMAIL=<your email> SMSEMAIL=<cell phone number @ sms-gateway> SENDEREMAIL=alert@localhost SERVER=http://127.0.0.1/ PAUSE=60 FAILED=0 DEBUG=0 while true do /usr/bin/curl -sSf $SERVER > /dev/null 2>&1 CS=$? # For debugging purposes if [ $DEBUG -eq 1 ] then echo "STATUS = $CS" ...


3

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


2

Discovered one that works across Linux (probably all versions) and Solaris (at least, 5.9) - no access to other OS's to test it and too lazy to check docs :) boottime=$(who -b | awk '/ /{print $3 " " $4}')") is the time the system was booted in yyyy/mm/dd HH:MM:SS format, so converting to epoch-seconds is: bootepoc=date -d "$boottime" +%s and time its ...


2

This will work: ps -eo etime,cmd | grep process_name


2

echo $(( $(date +'%s') - $(stat -c '%Y' /proc/$PID) )) This should work in any system with a /proc filesystem. Unfortunately I don't have the means to test it.


2

I don't know if there is one command for all systems, but you can use the Unix Rosetta Stone to translate the commands to each system.


2

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



Only top voted, non community-wiki answers of a minimum length are eligible