exit ssh and still have a command running?
Solution 1
When a session leader with associated controlling terminal (bash
, in your case) exits, the operating system sends SIGHUP to all processes in the foreground process group (foreground job). Additionally, bash
sends by default the SIGHUP to all its jobs, running or stopped (stopped jobs are also sent SIGCONT). Now the default action upon receiving SIGHUP is the process termination.
There are then several approaches to letting the process running after bash
exits. First, you can make your process immune to SIGHUP; this is what the nohup
command does. Second, you can background the corresponding process group and instruct bash
not to send SIGHUP to it; this is what the disown
shell builtin is used for (note also that there is a huponexit
shell option, which causes SIGHUP to be sent to all jobs when an interactive login shell exits).
If you wish to reattach a terminal to the process group after you log in again, you may want to use a wrapper capable of this; terminal multiplexers such as screen
have this functionality.
Solution 2
One solution is to prefix the command with nohup
. This tells the computer to ignore 'hangup' signals (such as exiting an ssh session). This MAY NOT WORK with for
loops.
Another good solution (and my personal favorite way to do this) is you install the 'screen' package. screen
provides a virtual terminal that can be disconnected from, leaving processes running by pressing CTRL + A , D
. The screen can then be reconnected to later with screen -r
.
Related videos on Youtube
sanjihan
Updated on September 18, 2022Comments
-
sanjihan almost 2 years
I am running a command on a server. It comes with this library http://www.graphicsmagick.org/.
Basically I have a for loop that does image procession on a lot of files.
Command runs for hours and will run for hours. I am wondering, can I exit ssh and still have those commands running on remote machine? Does exiting ssh sends some signal to end current command execution?
The other thing that I noticed..Although I am running command remotely, my laptop is overheating. Activity monitor shows bash is using 70% of cpu. I am on macbook pro
Now this is strange. How can remote command cause strain on local machine?
-
EightBitTony about 7 yearsCan you show us what you're running, i.e. show us how you connect and the
for
loop that is being executed. -
Satō Katsura about 7 yearsThere are many ways to do that. The simplest is probably to use
tmux
orscreen
. -
Yaron about 7 yearstake a look here
-
Matej Vrzala M4 about 7 yearsYou can also use nohup to run the command..
-
sanjihan about 7 yearsThe command: for i in *.png; do for pos6 in *.png; do gm composite $i $pos6 overlays/${i/.png/}/${i/.png/}${pos6/.png/}.png ; done; done . I use ssh command to connect to server
-
Gilles 'SO- stop being evil' about 7 yearsPlease ask one question at a time. I have closed your question as a duplicate for the first question in your post, which is what the answers here were about. The second question, “How can remote command cause strain on local machine?”, cannot be answered, it needs more information from you. Copy-paste the commands you run, and look at the output of top or htop to see what is using CPU time. Ask a new question about this, with the information that we requested.
-
-
ivanivan about 7 years+1 for
screen
. So useful. -
sanjihan about 7 yearsThanks. Will try that. What about processor strain on local machine?
-
Caleb Huggins about 7 yearsThe strain will be as much as normally running the command over SSH.