Hi,
this patch adds a new option to uptime(1) that allows to see the length of
time the system has been up in seconds.
Patch includes documentation (man-page) update.
Example:
$ uptime
1:26PM up 1:28, 1 user, load averages: 0.00, 0.00, 0.00
$ uptime -S
1:26PM 5308 secs, 1 user, load averages: 0.00, 0.00, 0.00
It's tested in current on amd64 and aarch64.
Why I need it? Well, it makes it easy to check the time the system has been
up, for example, via scripts. It doesn't need to parse the output and
recalculate it.
Cheers,
Alexander
Index: uptime.1
===================================================================
RCS file: /cvs/src/usr.bin/w/uptime.1,v
retrieving revision 1.15
diff -u -p -u -p -r1.15 uptime.1
--- uptime.1 14 Dec 2017 18:03:03 -0000 1.15
+++ uptime.1 20 Mar 2020 12:05:40 -0000
@@ -37,6 +37,7 @@
.Nd show how long system has been running
.Sh SYNOPSIS
.Nm uptime
+.Op Fl S
.Sh DESCRIPTION
The
.Nm uptime
@@ -46,6 +47,11 @@ the number of users, and the load averag
1, 5, and 15 minutes.
This is the first line from
.Xr w 1 .
+.Pp
+The options are as follows:
+.Bl -tag -width Ds
+.It Fl S
+the length of time the system has been up in seconds.
.Sh SEE ALSO
.Xr w 1
.Sh HISTORY
Index: w.c
===================================================================
RCS file: /cvs/src/usr.bin/w/w.c,v
retrieving revision 1.66
diff -u -p -u -p -r1.66 w.c
--- w.c 28 Jun 2019 13:35:05 -0000 1.66
+++ w.c 20 Mar 2020 12:05:40 -0000
@@ -95,7 +95,7 @@ struct entry {
static void fmt_putc(int, int *);
static void fmt_puts(const char *, int *);
static void pr_args(struct kinfo_proc *);
-static void pr_header(time_t *, int);
+static void pr_header(time_t *, int, int);
static struct stat
*ttystat(char *);
static void usage(int);
@@ -110,6 +110,7 @@ main(int argc, char *argv[])
FILE *ut;
struct in_addr addr;
int ch, i, nentries, nusers, wcmd;
+ int sec_only = 0;
char *memf, *nlistf, *p, *x;
char buf[HOST_NAME_MAX+1], errbuf[_POSIX2_LINE_MAX];
@@ -122,7 +123,7 @@ main(int argc, char *argv[])
p = "hiflM:N:asuw";
} else if (!strcmp(p, "uptime")) {
wcmd = 0;
- p = "";
+ p = "S";
} else
errx(1,
"this program should be invoked only as \"w\" or \"uptime\"");
@@ -143,6 +144,9 @@ main(int argc, char *argv[])
case 'N':
nlistf = optarg;
break;
+ case 'S':
+ sec_only = 1;
+ break;
case 'a':
nflag = 0;
break;
@@ -216,7 +220,7 @@ main(int argc, char *argv[])
(void)fclose(ut);
if (header || wcmd == 0) {
- pr_header(&now, nusers);
+ pr_header(&now, nusers, sec_only);
if (wcmd == 0)
exit (0);
}
@@ -422,7 +426,7 @@ nothing:
}
static void
-pr_header(time_t *nowp, int nusers)
+pr_header(time_t *nowp, int nusers, int sec_only)
{
double avenrun[3];
struct timespec boottime;
@@ -442,7 +446,9 @@ pr_header(time_t *nowp, int nusers)
*/
if (clock_gettime(CLOCK_BOOTTIME, &boottime) != -1) {
uptime = boottime.tv_sec;
- if (uptime > 59) {
+ if (uptime <= 59 || sec_only)
+ printf(" %d secs,", (int)uptime);
+ else {
uptime += 30;
days = uptime / SECSPERDAY;
uptime %= SECSPERDAY;
@@ -463,8 +469,7 @@ pr_header(time_t *nowp, int nusers)
(void)printf(" %d min%s,",
mins, mins != 1 ? "s" : "");
}
- } else
- printf(" %d secs,", (int)uptime);
+ }
}
/* Print number of users logged in to system */
@@ -508,6 +513,6 @@ usage(int wcmd)
"usage: w [-ahi] [-M core] [-N system] [user]\n");
else
(void)fprintf(stderr,
- "usage: uptime\n");
+ "usage: uptime [-S]\n");
exit (1);
}