Fix wrong detection when run in background in shell script