Input crash after killing less than (1) subprocess

I am writing a program that displays text on a terminal using Unix less (1). Here is the relevant part:

less = subprocess.Popen(['less -F -'], stdin=subprocess.PIPE, 
            stdout=sys.stdout, shell=True)
try:
    less.stdin.write(rfc_text)
    less.stdin.flush()
    less.stdin = sys.stdin
    less.wait()
except IOError:
    less.terminate()
    return errno.EPIPE
except KeyboardInterrupt:
    less.terminate()
    return 0

Waiting for it to end, I listen to the KeyboardInterrupt exception. If I catch one, I kill the SIGTERM signal less and exit my program.

Now that this happens, I will return to the invitation of my shell, but the shell no longer repeats what I am writing, and I need to reset (1) so that it works again.

Any ideas on how to make less die without taking my stdin with him to the grave? The full source is available at https://github.com/jforberg/rfc/blob/master/rfc.py

EDIT:. , (1), man (1) C. . , , , , - , .

+5
3

- less ( q):

#!/usr/bin/env python
from subprocess import PIPE, Popen

p = Popen(['less'], stdin=PIPE)
try:
    p.communicate(''.join("%d\n" % i for i in range(1000)))
except KeyboardInterrupt:
    print("Press `q` to exit.")
    p.wait()
+1

:

  • , C, , . , . , , , . , ( , Unix man).

  • -K, control-C, .

+1

! . . , . "", "tail -f", "less + F -S logfilename". CTRL + C, , Q .

, , 6- ( . . ):

logpath = os.path.join(cwd, "filename.log")
less = Popen(["less","+F","-S", logpath])
retcode = None
while retcode is None:
    try: retcode = less.wait()
    except KeyboardInterrupt: pass

I think this solves the original. If you enter something less, you may need to configure the Popen method: either wait(), or communicate(input).

0
source

All Articles