uptime: add since option
The --since or -s option will show the time since the host was online. Reference(s): http://www.freelists.org/post/procps/Patch-Added-since-option-to-uptime Signed-off-by: Craig Small <csmall@enc.com.au>
This commit is contained in:
parent
6c67c8d27a
commit
a705fdcb15
5
uptime.1
5
uptime.1
@ -1,6 +1,6 @@
|
|||||||
.\" -*-Nroff-*-
|
.\" -*-Nroff-*-
|
||||||
.\"
|
.\"
|
||||||
.TH UPTIME "1" "June 2011" "procps-ng" "User Commands"
|
.TH UPTIME "1" "December 2012" "procps-ng" "User Commands"
|
||||||
.SH NAME
|
.SH NAME
|
||||||
uptime \- Tell how long the system has been running.
|
uptime \- Tell how long the system has been running.
|
||||||
.SH SYNOPSIS
|
.SH SYNOPSIS
|
||||||
@ -28,6 +28,9 @@ the time.
|
|||||||
\fB\-h\fR, \fB\-\-help\fR
|
\fB\-h\fR, \fB\-\-help\fR
|
||||||
display this help text
|
display this help text
|
||||||
.TP
|
.TP
|
||||||
|
\fB\-s\fR, \fB\-\-since\fR
|
||||||
|
system up since, in yyyy-mm-dd MM:HH:SS format
|
||||||
|
.TP
|
||||||
\fB\-V\fR, \fB\-\-version\fR
|
\fB\-V\fR, \fB\-\-version\fR
|
||||||
display version information and exit
|
display version information and exit
|
||||||
.SH FILES
|
.SH FILES
|
||||||
|
32
uptime.c
32
uptime.c
@ -21,19 +21,45 @@
|
|||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
|
||||||
#include "c.h"
|
#include "c.h"
|
||||||
#include "fileutils.h"
|
#include "fileutils.h"
|
||||||
#include "nls.h"
|
#include "nls.h"
|
||||||
|
#include "proc/sysinfo.h"
|
||||||
#include "proc/whattime.h"
|
#include "proc/whattime.h"
|
||||||
#include "proc/version.h"
|
#include "proc/version.h"
|
||||||
|
|
||||||
|
static void print_uptime_since()
|
||||||
|
{
|
||||||
|
double now, uptime_secs, idle_secs;
|
||||||
|
time_t up_since_secs;
|
||||||
|
struct tm *up_since;
|
||||||
|
struct timeval tim;
|
||||||
|
|
||||||
|
/* Get the current time and convert it to a double */
|
||||||
|
gettimeofday(&tim, NULL);
|
||||||
|
now = tim.tv_sec + (tim.tv_usec / 1000000.0);
|
||||||
|
|
||||||
|
/* Get the uptime and calculate when that was */
|
||||||
|
uptime(&uptime_secs, &idle_secs);
|
||||||
|
up_since_secs = (time_t) ((now - uptime_secs) + 0.5);
|
||||||
|
|
||||||
|
/* Show this */
|
||||||
|
up_since = localtime(&up_since_secs);
|
||||||
|
printf("%04d-%02d-%02d %02d:%02d:%02d\n",
|
||||||
|
up_since->tm_year + 1900, up_since->tm_mon + 1, up_since->tm_mday,
|
||||||
|
up_since->tm_hour, up_since->tm_min, up_since->tm_sec);
|
||||||
|
}
|
||||||
|
|
||||||
static void __attribute__ ((__noreturn__)) usage(FILE * out)
|
static void __attribute__ ((__noreturn__)) usage(FILE * out)
|
||||||
{
|
{
|
||||||
fputs(USAGE_HEADER, out);
|
fputs(USAGE_HEADER, out);
|
||||||
fprintf(out, _(" %s [options]\n"), program_invocation_short_name);
|
fprintf(out, _(" %s [options]\n"), program_invocation_short_name);
|
||||||
fputs(USAGE_OPTIONS, out);
|
fputs(USAGE_OPTIONS, out);
|
||||||
fputs(USAGE_HELP, out);
|
fputs(USAGE_HELP, out);
|
||||||
|
fputs(_(" -s, --since system up since\n"), out);
|
||||||
fputs(USAGE_VERSION, out);
|
fputs(USAGE_VERSION, out);
|
||||||
fprintf(out, USAGE_MAN_TAIL("uptime(1)"));
|
fprintf(out, USAGE_MAN_TAIL("uptime(1)"));
|
||||||
|
|
||||||
@ -46,6 +72,7 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
static const struct option longopts[] = {
|
static const struct option longopts[] = {
|
||||||
{"help", no_argument, NULL, 'h'},
|
{"help", no_argument, NULL, 'h'},
|
||||||
|
{"since", no_argument, NULL, 's'},
|
||||||
{"version", no_argument, NULL, 'V'},
|
{"version", no_argument, NULL, 'V'},
|
||||||
{NULL, 0, NULL, 0}
|
{NULL, 0, NULL, 0}
|
||||||
};
|
};
|
||||||
@ -56,10 +83,13 @@ int main(int argc, char **argv)
|
|||||||
textdomain(PACKAGE);
|
textdomain(PACKAGE);
|
||||||
atexit(close_stdout);
|
atexit(close_stdout);
|
||||||
|
|
||||||
while ((c = getopt_long(argc, argv, "hV", longopts, NULL)) != -1)
|
while ((c = getopt_long(argc, argv, "hsV", longopts, NULL)) != -1)
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 'h':
|
case 'h':
|
||||||
usage(stdout);
|
usage(stdout);
|
||||||
|
case 's':
|
||||||
|
print_uptime_since();
|
||||||
|
return EXIT_SUCCESS;
|
||||||
case 'V':
|
case 'V':
|
||||||
printf(PROCPS_NG_VERSION);
|
printf(PROCPS_NG_VERSION);
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
Loading…
Reference in New Issue
Block a user