Using command & + disown instead of nohup
disown
is the better practice (being built into the shell rather than depending on an external tool), but it requires more work: You need to redirect stdin, stdout, and stderr yourself (whereas nohup
will do the redirection with a nohup.out
name hardcoded if you haven't done it yourself).
Thus:
rsync "${args[@]}" </dev/null >logfile 2>&1 & disown -h "$!"
As a stylistic note, if the only use you make of the PID is passing it to disown
, I do suggest putting the disown
on the same line as the invocation, as done above: This ensures that the $!
reference is to the background process forked immediately prior, even if future changes add more code, potentially forking other background processes, after the rsync
is started. (On the other hand, if you want to refer to the PID later, you might put a variable assignment on the same line: rsync ... & rsync_pid=$!
, then disown -h "$rsync_pid"
on a separate line).