The way to continue to run the program in the background process of the remote server
If you run a program like Python via SSH connection from the terminal of the host PC, the remote server kills it when connection is lost.
The way to run a program
Run in the background
terminal
$ python sample.py &
If you want to run a program in the background, add '&' to the end of the command.
This command allows you to run other commands in the foreground, but you must keep the SSH connection alive.
Run with nohup command
terminal
$ nohup python sample.py
If run the command with nohup
, ignore HUP Signal (SIGHUP) and continue to run a program via SSH connection.
Trivial things: it seems that HUP comes from hangup.
So you can keep it running in the background process with nohup
when you log off from the remote server.
terminal
$ nohup python sample.py &
The log is output to nohup.out file when run it with nohup
.
Note that alias is not working by this command.
Move the already running process to the background
Assume that you have already run a program with the following:
terminal
$ python sample.py
Pause the job with the shortcut Ctrl + Z because you cannot input a command.
terminal
$ bg
The bg
command restarts the job, that was paused, in the background process.
If you are using bash, separete this job from the current shell with the following:
terminal
$ disown -h
In zsh, -h
option doesn't work, but you can handle the same process without the option.
disown: job not found: -h
If you run the exit
command when the process that is not using nohup is running, the warning message is displayed:
zsh: warning: 1 jobs SIGHUPed
Check the running programs
Show the list of the processes
terminal
$ ps -aux | grep python
user 5643 99.6 0.2 55877080 568420 ? Rl 10:08 0:41 python sample.py
user 5825 0.0 0.0 15204 976 pts/9 R+ 10:08 0:00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox python
The ps
command shows the list of running processes.
View the logs
The cat
and vim
commands can display the logs, but it is recommended to use the tail
command to view the logs stored in the nohup.out file.
terminal
$ tail ./nohub.out -n 5 -f
The -f
option allows the tail
command to keep it running. The -n
option is the number of rows that displayed. (default 10 rows)
Stop this command, use the shortcut Ctrl + C.