Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I am currently creating a backup script which uses screen to start a backup job with rsync inside a screen session. The backup jobs are started as follows.

screen -dmS backup /usr/bin/rsync ...

As soon as the rsync job is finished, the screen session is terminated automatically. To make sure, that the backup was successful, I would like to check the exit code of the rsync job but unfortunately I really don't know how to get the exit code after the screen was terminated.

Does someone have a good idea how to automatically check, if the rsync job was successful or not?

Would be great if someone does.

I already thought about using a temp file but like this:

screen -dmS myScreen "rsync -av ... ; echo $? > /tmp/myExitCode" but this unfortunately does not work. Then I thought about using stderr like in the example below:

screen -dmS myScreen "rsync -av ... >2 /tmp/rsync-sterr

None of my ideas worked out so far, since stderr is not written when I use the command above. :-( ?

Would be great if someone has a good idea or even a solution.



share|improve this question
Hey thank you very much so far, that kind of helped but I have still a little problem. screen -dmS name sh -c "{ rsync -av /mySource /myTarget 2>&1 ; } > /tmp/rsync-status" Assuming that /mySource is not existant, rsync exits with exit code 23 and stdout and sterr are written to /tmp/rsync-status. What I would like to achieve is, that there is only stderr without stdout written to the file? I tried it like in the example below but unfortunately it does not work out: screen -dmS name sh -c "{ rsync -av /mySource /myTarget 2> ; } > /tmp/rsync-status" In that case the file is n –  Bettina Jan 31 '11 at 13:34
this isn't a chat room, can you please read our FAQ and look at other questions/answers before posting again. –  Chopper3 Jan 31 '11 at 13:35

1 Answer 1

Pass sh -c "commands" to screen, if you want to use redirection and such:

screen -dmS name sh -c "{ rsync -av ... 2>&1; echo $?; } > /tmp/rsync-status"

Also, it's fd>file, not >fd file. To redirect stderr, you would use 2> /tmp/foo.

share|improve this answer
I think you're missing some stuff. Perhaps screen -dmS name sh -c "{ rsync -av ... 2>&1 ; echo $?; } > /tmp/rsync-status" or any of several other variations. –  Dennis Williamson Jan 30 '11 at 21:30
@Dennis: Right, it seems I posted half an answer :/ Will edit. –  grawity Jan 31 '11 at 9:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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