Skip to content
Advertisement

How can I time out an input/output redirection in Linux?

I was wondering whether or not it is possible to time out an input/output redirection in Linux.

Meaning that I would like to redirect certain input/output to a file just for a time span of 2s for instance.

In a practical case, I am looking to cat the tty0 to a file only during 2s

cat /dev/tty0 >> ~/uart.txt

How can I achieve this?

Thanks

Advertisement

Answer

On most linux, you should have access to timeout:

timeout 2 cat /dev/tty >> ~/uart.txt

This will send a SIGTERM after 2 seconds. If you want a different signal, use -s. If you want to follow up with SIGKILL, use -k. man timeout for details.

Advertisement