Score:5

Simple Backup bash Script with nohup and rsync

in flag

I have the following simple command to start my backup process in the background with nohup:

nohup rsync -a /src /target &

I am trying to find a proper solution to capture the exit code and send a failed or success message into a log file or mail. To achieve that I tried the following:

nohup rsync -a /src/ /target/ &
oldpid=$!
wait $oldpid

if [[ $? -eq 0 ]]
then
        echo "SUCCESS"
else
        echo "FAILED"
fi

The code seems to work fine but I acctually don't now if this is a reliable approach. I have two worries:

  • Could the variable $? be overwritten when another command is executed in between?
  • Is the wait $oldpid messing with the asynchronous way of nohup?

Or maybe this logic is just dumb and there is simply a better approach?

marcelm avatar
cn flag
Are you using cron to run the rsync command? If so, have you considered using [chronic](https://github.com/docwhat/chronic) (and ensuring cron can send mail)?
Score:4
cn flag

According to the question:

Could the variable $? be overwritten when another command is executed in between?

The answer is YES. The variable $? contains the exit status of the last task. For example:

$ false; echo $?
1
$ false; true; echo $?
0
$ false; true; false; echo $?
1

Like as the variable $! contains the PID of the last background task.

Reference: Devhints.io: Bash scripting cheatsheet


According to the question:

Is the wait $oldpid messing with the asynchronous way of nohup?

It shouldn't - isn't it designed exactly for such tasks? :)


According to the question:

Or maybe this logic is just dumb and there is simply a better approach?

For me it is not clear why you pushing the rsync into the background and then wait for it instead just execute the command and then do the test.

#!/bin/bash
rsync -a source.file destination.file 2>/dev/null

if [[ $? -eq 0 ]]; then echo "SUCCESS"; else echo "FAILED"; fi
  • 2>/dev/null is used here to suppress the error messages of rsync, but you can save them in a temp file and attach it to the email.

But probably it is not the entire script...


In addition, in the first rows of help wait is provided the following information.

wait: wait [-fn] [id ...]

Wait for job completion and return exit status.

So you can use the output of wait directly within the if statement. I've already done successful test for this approach.

#!/bin/bash
nohup rsync -a source.file destination.file 2>/dev/null &
oldpid=$!

if wait $oldpid
then
        echo "SUCCESS"
else
        echo "FAILED"
fi
  • 2>/dev/null is used here to suppress the error messages of nohup, while rsync error messages goes somewhere in the background.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.