Logging GNU screen command output
Previously I described how to use GNU screen to run bash commands in background. In addition to that, if sometimes it happens that a detached command terminates before completion, we would like to have logs for that command, e.g., in order to determine why and when an error occurred. This post explains how to log the progress of a command detached in GNU screen to a text file.
As it turns out, logging command output in GNU screen is pretty straightforward. Let’s consider the following command:
screen -S test_logging bash -c "timeout 20s ping 0.0.0.0 > /tmp/log.txt"
Then you can detach the process by pressing Ctrl+a (release) and then d. The output of the command will still be writing to the log. Success!
Alternatively, you can detach the command right away (when actually running it). You can achieve that by using `dm` parameters:
screen -dmS test_logging bash -c "timeout 20s ping 0.0.0.0 > /tmp/log.txt"
Source of the graphic in the cover image: Encyclopedia Britannica