I ran this command to try and get my python program to run in the background and let me exit the SSH connection I am using:
nohup python files.py >> files.log &
I then check ps
to check it worked
$ ps a
PID TTY STAT TIME COMMAND
13059 pts/0 Ss 0:00 -bash
13327 pts/0 R+ 0:00 ps a
[1]+ Exit 1 nohup python files.py >> files.log
What does this [1]+ Exit 1
mean?
And how can I run my program in the background letting me exit correctly?
Best Answer
When you use the
&
at the end of a command in bash, it runs the command in the background. The[1]+ Exit 1
means that job 1 exited with status 1. The number between the[]
is the job number, and the number afterExit
is the exit status. If no exit status is shown, it means the command exited with status0
.The reason you don't see this until you run another command is because by default the status of a completed backgrounded command is not shown until the prompt is updated. This means after another command exits, or you simply press
<ENTER>
on an empty prompt.Edited:
You also do not need to use the
nohup
. Bash will only HUP a process when the shell itself receives a HUP. This doesn't happen if you exit cleanly (viaexit
, or CTRL+D).However you can also use the
disown
builtin to make it so specific jobs don't get a HUP.Lastly you can also set a SIGHUP handler to disown all jobs when the shell gets a HUP by doing
function sighup { disown -a; exit 0; }; trap sighup HUP
.