I am running a $command, in a instance of a screen, as a different user.
It's run by the equivalent of this command (more detailed at bottom):


and that works great. The problem is I want to redirect the output of $COMMAND to some logfile (e.g $LOGFILE).

Just adding > $LOGFILE to the end seems to read from screen instead of $COMMAND, so I am stumped as to what I should do. I've tried grouping it with parenthesis and braces, but that caused errors so I probably wasn't doing it correctly.

So how can I redirect the output of $COMMAND to $LOGFILE?

The code I am running is "equivalent" to this:

execute() {
  su $USERNAME -c "$*"
execute screen -dmS $SCREENNAME $COMMAND

The problem is that not all shell parsing features get invoked if you pass the command directly to screen. So, the solution is to directly pass the command to a shell, which parses it as expected.

For example, assuming you use bash:
su $USERNAME -c "screen -dmS $SCREENNAME bash -c \"$COMMAND > $LOGFILE\""

(The escaped parentheses are so that the command is passed to bash as a whole, otherwise the problem with redirecting screen's output and not the $COMMAND's output would arise again)

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.