How to make a program continue to run after log out from ssh?
Assuming that you have a program running in the foreground, press ctrl-Z, then:
[1]+ Stopped myprogram
$ disown -h %1
$ bg 1
[1]+ myprogram &
$ logout
If there is only one job, then you don't need to specify the job number. Just use disown -h
and bg
.
Explanation of the above steps:
You press ctrl-Z. The system suspends the running program, displays a job number and a "Stopped" message and returns you to a bash prompt.
You type the disown -h %1
command (here, I've used a 1
, but you'd use the job number that was displayed in the Stopped
message) which marks the job so it ignores the SIGHUP
signal (it will not be stopped by logging out).
Next, type the bg
command using the same job number; this resumes the running of the program in the background and a message is displayed confirming that.
You can now log out and it will continue running..
Continue executing a program, after end of ssh
I assume when you tried, you put it in the background with myscript &
, and then logged out. Some GNU/Linux distros default to sending a SIGHUP
to all processes that have the session tty as their tty (or some other way of finding processes from a login sessions, like a tree of parent PIDs, I forget.)
So, a solution is:
nohup myscript &
It's really that easy. When run under nohup
, you process will ignore the hang-up signal. nohup
redirects stdin from /dev/null if it was from the terminal, and stdout/stderr to nohup.out
. Then you read nohup.out
, which will be created in the directory from which you called your script.
tmux is really nice, and I use it all the time, but it's not actually needed for this. (I actually use screen, but that's only because tmux didn't exist when I started using screen, and switching would incur some re-learning overhead.)
Keep Go script running on Linux server after ssh connection is closed
There are mainly three options here, one is to use the nohup command, the other is to use the screen command, and the last is the upgraded version of byobu of screen. After reading these three commands, I actually prefer to use the byobu command, because the byobu command is more powerful, an upgraded version of screen, and the interface is more friendly.
Windows ssh - How to keep proccess running after Disconnect
If you have GNU Coreutils/cygwin installed then you can run start nohup your-command > logfile.txt
and it should remain running when you exit the ssh session. This works with the OpenSSH server that comes with Windows 10.
How to keep the task running after closing the ssh connection?
You can use screen to detach a session. You connect to your ssh server, launch screen and then your computation...
At your next connection, screen -a
to attach previous sessions
see : http://www.bangmoney.org/presentations/screen.html
Keep process running on remote machine after exiting ssh session inside bash script
You need to redirect stdin,
stdout
and stderr
to somewhere else as opposed to your terminal like so:
ssh root@10.101.10.35 'iperf -s -B 192.168.99.1 < /dev/null > /tmp/iperf_combined.log 2>&1 &'
stdin
is taken from /dev/null
(nothing is entered)
stdout
and stderr
goes to /tmp/iperf_combined.log
The process will run on the remote machine until you will manually kill it or until the script/command will exit on its own.
Edit (as a reply to the poster's comment):
If you want to run multiple commands in the same ssh
session, you may use:
ssh -T root@10.101.10.35 << EOF
iperf -s -B 192.168.99.1 < /dev/null > /tmp/iperf_combined_1.log 2>&1 &
iperf -s -B random_ip2 < /dev/null > /tmp/iperf_combined_2.log 2>&1 &
EOF
As per ssh
man page:
-T Disable pseudo-tty allocation.
Detailed explanation on psqudo-tty
here
Related Topics
How to Tar a Directory Without Retaining the Directory Structure
How to Store a Command in a Variable in a Shell Script
How to Install Gcc 4.9.2 on Rhel 7.4
How to Get the Total Cpu Usage of an Application from /Proc/Pid/Stat
Why Do You Need to Put #!/Bin/Bash At the Beginning of a Script File
What Are the Return Values of System Calls in Assembly
Is There Really No Asynchronous Block I/O on Linux
Recursively Counting Files in a Linux Directory
Change the X-Frame-Options to Allow All Domains
What Registers Are Preserved Through a Linux X86-64 Function Call
Get Exit Code of a Background Process
How to Recursively Find All Files in Current and Subfolders Based on Wildcard Matching
How Many Socket Connections Possible
What Is an Uninterruptible Process