The problem with Python 2 processing SIGPIPE non-standard way (i.e., is ignored) is already written out in Leon's answer, and the correction is given in the link: set SIGPIPE by default ( SIG_DFL ), for example,
import signal signal.signal(signal.SIGPIPE,signal.SIG_DFL)
You can try to disable SIGPIPE from your script with, for example,
#!/bin/bash trap SIGPIPE
but unfortunately it doesnβt work according to the Bash reference guide
Signals ignored when entering the shell cannot be captured or reset.
Last comment: you have useless use of cat here; better to write a script like:
However, since you are using Bash, you can use the built-in read construct as follows (this will advantageously replace fold and head ):
#!/bin/bash read -n4 a < <(tr -dc 'a-z1-9' < /dev/urandom) printf '%s\n' "$a"
It turns out that with this version you will have a clear idea of ββwhat is happening (and the script will not hang):
$ python -c "import subprocess; subprocess.call(['./foo'])" hcwh tr: write error: Broken pipe tr: write error $ $
(Of course, this works well without errors with Python3). And telling Python to use the default signal for SIGPIPE also works well:
$ python -c "import signal; import subprocess; signal.signal(signal.SIGPIPE,signal.SIG_DFL); subprocess.call(['./foo'])" jc1p $
(and also works with Python3).
gniourf_gniourf
source share