On Linux can I keep a program running in background even after I disconnect?
Solution 1:
This is because your program need terminal to run, you can use GNU Screen to create a virtual terminal so that the program will continue to run even after you disconnect.
just run screen your_program_name
, you can close screen with crtl-a-d
then reopen it later with screen -rx
Solution 2:
Less flexible than screen are nohup
and disown
.
nohup
is a program, and you have to start the long running jub with it like this:
$ nohup longjob --options-for-longjob
it will cause your long job to run in the background and not attached to any terminal (which means that you won't get any output from it on the screen and won't be able to direct input to it from the keyboard)
disown
is a bash builtin that can disconnect a long running job after you've started it. My bash man page says
disown [-ar] [-h] [jobspec ...]
Without options, each jobspec is removed from the table of active jobs. If the -h option is given, each jobspec is not removed from the table, but is marked so that SIGHUP is not sent to the job if the shell receives a SIGHUP. If no jobspec is present, and neither the -a nor the -r option is supplied, the current job is used. If no jobspec is supplied, the -a option means to remove or mark all jobs; the -r option without a job- spec argument restricts operation to running jobs. The return value is 0 unless a jobspec does not specify a valid job.