weider weider - 10 months ago 75
Linux Question

Time of script execution [EDIT]

I read a serial port and send data to python, I want this chain to work for 10 seconds. The code below does not work:

sudo cat -v /dev/pts/2 | python3 bash_test.py
sleep 10
kill -9 $pid

e.g bash_test.py

import sys
for line in sys.stdin:

Answer Source

You can use timeout :

sudo bash -c "cat -v /dev/pts/2 | timeout 10 python3 bash_test.py"

Command info:

       timeout - run a command with a time limit

       timeout [OPTION] DURATION COMMAND [ARG]...
       timeout [OPTION]

Simple example for timeout:

timeout 3 sleep 10

The command here is sleep 10 but timeout will kill it after 3 seconds.