annotate python/example/waiter.py @ 918:9d89288e3b4c

add script for k0s.org web install
author Jeff Hammel <k0scist@gmail.com>
date Sun, 02 Jun 2024 14:39:08 -0700
parents 9f85111f3dee
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
873
9f85111f3dee add process waiting example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
9f85111f3dee add process waiting example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2
9f85111f3dee add process waiting example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 """
9f85111f3dee add process waiting example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 wait for a process writing to stdout to keep an
9f85111f3dee add process waiting example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 (e.g.) ssh connection alive
9f85111f3dee add process waiting example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6 """
9f85111f3dee add process waiting example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7
9f85111f3dee add process waiting example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 import subprocess
9f85111f3dee add process waiting example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 import sys
9f85111f3dee add process waiting example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 import time
9f85111f3dee add process waiting example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11
9f85111f3dee add process waiting example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 KEEPALIVE = 300 # s
9f85111f3dee add process waiting example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 SLEEP = 1
9f85111f3dee add process waiting example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14
9f85111f3dee add process waiting example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15
9f85111f3dee add process waiting example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 def main(args=sys.argv[1:]):
9f85111f3dee add process waiting example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 """CLI"""
9f85111f3dee add process waiting example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18
9f85111f3dee add process waiting example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 last = start = time.time()
9f85111f3dee add process waiting example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 proc = subprocess.Popen(args)
9f85111f3dee add process waiting example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21
9f85111f3dee add process waiting example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 while proc.poll() is None:
9f85111f3dee add process waiting example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 if time.time() - last > KEEPALIVE:
9f85111f3dee add process waiting example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 last = time.time()
9f85111f3dee add process waiting example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 print ("[{} s] waiting for process: {}".format(last-start, subprocess.list2cmdline(args)))
9f85111f3dee add process waiting example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 sys.stdout.flush()
9f85111f3dee add process waiting example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 exit(proc.poll())
9f85111f3dee add process waiting example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28
9f85111f3dee add process waiting example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 if __name__ == '__main__':
9f85111f3dee add process waiting example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 main()