Reputation: 11736
nohup someprogram &> $LOG &
echo "what happen: $?"
And when I have no access to $LOG or someprogram exits with a status other than 0, I'd like to be able to detect that.
Currently, when there is a permission error, $? returns 0. Is it possible to get bash to tell me that there was a problem executing the previous statement?
Upvotes: 1
Views: 153
Reputation: 923
If you background a process, you cannot immediately reliably test whether it failed -- because it could still be running before it fails whenever you check it. For a reliable solution you have to wait
.
However, if you don't care about full reliablitiy and you just want to check whether the programm you call survived some fraction of a second, you could do
cpid=$!
sleep 0.1
jobs -l | grep $cpid | grep running && working=1
The snippet could be enhanced, but it should give you the basic idea.
Note that some sleep implementations only support integers. GNU's sleep, which you will find on most Linux distributions, also supports floats.
Upvotes: 0
Reputation: 16379
Try wait to return the status of your child process.
nohup someprogram &> $LOG &
wait $!
echo $?
Upvotes: 1