Goal: To run a command independent of the terminal and return immediately. And if the command fails run an alternative commands independent of the terminal and return immediately.
I did the following. It works if the command works, but returns the error code as 0 even if it fails. The code can be rectified by removing the first disown but then the command doesn't returns immediately.
ErrorCode=$(
nohup "${Command[@]}" >/dev/null 2>&1 &
Pid=$!
disown "${Pid}"
wait "${Pid}"
echo "$?"
)
if ((ErrorCode != 0)); then
nohup "${CommandAlternative[@]}" >/dev/null 2>&1 &
else
echo "#Debug: ErrorCode: ${ErrorCode}"
fi
So basically I am not able to capture the error code of the previous command with disown. Command is mpv "${MusicFile}"
if
?if [ "$ErrorCode" -ne 0 ]
....