Home > Back-end >  Program has no output only when run by a shell script enforcing a timeout
Program has no output only when run by a shell script enforcing a timeout

Time:01-05

I will first write a sequence of commands which I want to run

echo <some_input> | <some_command> > temp.dat & pid=$!
sleep 5
kill -INT "$pid"

The above is working perfectly fine when i run it one by one from bash shell, and the contents in the temp.dat file is exactly what I want. But, when I create a bash script containing the same set of commands, I am getting nothing in the temp.dat file.

Now, I'll mention why I'm writing those commands in such a way:

  1. <some_command> asks for an input, that's why I'm piping <some_input>
  2. I want the output of that command in a separate file, that's why I've redirected the output.
  3. I want to kill the command by sending SIGINT signal after some time.

I've tried running an interactive shell by writing #!/bin/bash -i in the first line of the shell script, but it's not working.

Any alternate method to achieve the same results will be appreciated.

  • Update: <some_command> is also invoking a python script, but I don't think that this will cause it to behave differently.
  • Update2: python script was the only cause of that different behavior.

CodePudding user response:

One likely cause here is that your Python process may not be flushing stdout within the allowed five seconds of runtime.

export PYTHONUNBUFFERED=1

...will cause content to be promptly written, rather than waiting for process exit / file close / amount of buffered content to reach a level sufficient to justify the overhead of a flush operation.

CodePudding user response:

will this work for you?

read -p "Input Data : " inputdata ; echo $inputdata > temp.data ; sleep 5; exit

obvs

#!/usr/bin/env bash 

read -p "Input Data : " inputdata
echo $inputdata > temp.data
sleep 5

should work as a script

to suit :D

#!/usr/bin/env bash 

read -p "Input Data : " inputdata
<code you write eg echo $inputdata> > temp.data
sleep 5
  • Related