Mercurial > repo
annotate bin/uptime @ 12286:8e9312c07f58 draft
<b_jonas> `` f=/hackenv/bin/botloop; >$f echo $\'#!/bin/sh\\necho "^botloop"\'; chmod -c a+x "$f"
author | HackEso <hackeso@esolangs.org> |
---|---|
date | Wed, 01 Jan 2020 09:36:54 +0000 |
parents | f35125e00403 |
children |
rev | line source |
---|---|
12142
ec79f376cd1a
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
12141
diff
changeset
|
1 #!/usr/bin/python3 |
12143
f35125e00403
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
12142
diff
changeset
|
2 # (/usr/bin/uptime -p) is broken, so here's a reimplementation |
12137
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
3 boottime = 1245511822 |
12141
bbcac610574d
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
12140
diff
changeset
|
4 import sys, os, time |
12137
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
5 helpmsg = "\nUsage:\n uptime [options]\n\nOptions:\n -p, --pretty show uptime in pretty format\n -h, --help display this help and exit\n -s, --since system up since\n -V, --version output version information and exit\n\nFor more details see uptime(1)." |
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
6 opt_p, opt_s = 0, 0 |
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
7 for a in sys.argv[1:]: |
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
8 if "--" == a: |
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
9 break |
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
10 if "-" == a[0]: |
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
11 for c in a[1:]: |
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
12 if "p" == c: |
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
13 opt_p = 1 |
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
14 elif "s" == c: |
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
15 opt_s = 1 |
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
16 elif "V" == c: |
12141
bbcac610574d
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
12140
diff
changeset
|
17 os.execl("/usr/bin/uptime", "/usr/bin/uptime", "-V") |
bbcac610574d
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
12140
diff
changeset
|
18 print("uptime from procps-ng 2.95.1") |
12137
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
19 sys.exit(0) |
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
20 elif "h" == c: |
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
21 print(helpmsg, file = sys.stdout) |
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
22 sys.exit(0) |
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
23 else: |
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
24 print("uptime: invalid option -- '" + c + "'\n" + helpmsg, file = sys.stderr) |
12140
2826c0c87d92
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
12139
diff
changeset
|
25 sys.exit(1) |
12137
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
26 if opt_s: |
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
27 print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(boottime))) |
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
28 else: |
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
29 now = time.time() |
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
30 uptime_s = now - boottime |
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
31 uptime_d, uptime_md = divmod(uptime_s, 60*60*24) |
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
32 uptime_h, uptime_mh = divmod(uptime_md, 60*60) |
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
33 uptime_m = uptime_mh // 60 |
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
34 if opt_p: |
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
35 print("up %d day, %d hours, %d minutes" % |
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
36 (uptime_d, uptime_h, uptime_m)) |
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
37 else: |
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
38 nowfmt = time.strftime("%H:%M:%S", time.localtime(now)) |
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
39 loadavg = ", ".join(open("/proc/loadavg").read().split()[0:3]) |
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
40 print(" %s up %d day, %2d:%02d, 0 users, load average: %s" % |
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
41 (nowfmt, uptime_d, uptime_h, uptime_m, loadavg)) |
db60ce0f0bb3
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff
changeset
|
42 |