I have a long-running command that I've been running in screen so as to be able to interact with it when it encounters bugs. But I'd also like to be able to start it via another shell script (which achieves something similar to GNU parallel), so I've been using screen -m -D mycommand which works quite well, except that I can't seem to figure out how to get the exit status - which is important for restarting jobs that have failed.

Is there a way to get screen to return the exit status of its child process? Just to have a test case to play with:

screen -mD sh -c "exit 1"
echo $?
# prints 0; would like something that prints 1

1 Answer 1


You could try using the -L flag:

-L: tells screen to turn on automatic output logging for the windows.

For me, this creates a file called screenlog.0 in the working directory which logs all the output; if you then run screen like

screen -mDL sh -c 'your_command || echo $?'

or possibly

screen -mDL sh -c 'your_command; echo $?'

you can then examine the last line of that file for an exit code... Perhaps the full result would be something like

screen -mDL sh -c 'your_command; echo $?' && tail -1 screenlog.0
  • seems like more efficient would be to ditch the -L and just echo $? > screenlog.0 or whatever... I might try that if nothing else better shows up; seems a shame to have to worry about writing it to a file though.
    – arcticmac
    Aug 1, 2015 at 9:47
  • 1
    also, the reason your second-to-last example doesn't work is that you need single quotes; with double quotes, the $? gets evaluated in your parent shell before being passed into the new shell you're starting.
    – arcticmac
    Aug 1, 2015 at 9:52
  • Oh yeah, didn't even think about that. That'd do it. You could probably use mktemp to generate a temp file and then write to that. Aug 1, 2015 at 19:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .