I want to use GNU screen to run a single command in detached mode over serial console and save it's output to the log file on the host where screen is running. Basically, non-interactive equivalent of the following actions:

screen /dev/ttyUSB0 115200 # connect to serial console "/dev/ttyUSB0" using speed "115200"
ctrl+a H # start logging
some command
ctrl+a k y # quit

I know how to run a single command in detached mode in the current shell:

screen -d -m -L some command
# output is saved to screenlog.0

But I don't understand how to do the same thing while also attaching to a serial console.


I can't test this properly on a serial port, but I think this might work:

$ screen -S serial -L -d -m /dev/ttyUSB0
$ screen -S serial -X stuff 'some command\n'

My version of screen accepts an argument to -L, the name of the log file. Apparently not all versions do. Another way to set the log file name would be with screen -S serial -X logfile filename (if that works). screen buffers the output to the log for a while before writing. By default the timeout is 10 seconds, but see logfile flush NN to change that.

-X sends a command to a running screen, and stuff, well, "stuffs" the given string as input in the current window.

  • @ilkkachu It doesn't work. screen seem to interpret everything after "-L" as a command to run: Cannot exec 'logfile': No such file or directory. If I replace "logfile" with "echo", log file "screenlog.0" is created with output "-d -m /dev/ttyUSB0". – Thunderbeef Jan 11 '18 at 14:05
  • @Thunderbeef, mhmh, it seems to work for me, with Screen version 4.05.00 (GNU) 10-Dec-16 on Debian. But I suppose you could just leave the log file name out and use the default, or use screen -X logfile ... to set the file name. – ilkkachu Jan 11 '18 at 16:03
  • @ilkkachu My version is 4.03.01. Without "logfile" it works properly (log saved to default file "screenlog.0"). Thanks! For the reference, you could also add that you need to quit session afterwards: screen -S serial -X quit (for me, the log is not updated until I quit session). – Thunderbeef Jan 11 '18 at 17:29
  • @ilkkachu Also, my man screen doesn't show that -L accepts an argument: -L tells screen to turn on automatic output logging for the windows.. If you'll update your answer, I'll accept it. – Thunderbeef Jan 11 '18 at 17:36
  • @Thunderbeef, It's not listed in the HTML manual either (then again, that says "last updated February 21, 2009"...). But the man page on my Debian system does list it, so I'll just assume it's some unofficial addition. Dunno. – ilkkachu Jan 11 '18 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.