1

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.

Cheers,

Bettina

Bettina
  • 11
  • 1
  • 2
  • 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 Answers1

2

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

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

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

user1686
  • 10,162
  • 1
  • 26
  • 42