Run Linux Command in Background and Keep Runing After Closing Ssh

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..

How to prevent a background process from being stopped after closing SSH client in Linux

Check out the "nohup" program.

Getting ssh to execute a command in the background on target machine

I had this problem in a program I wrote a year ago -- turns out the answer is rather complicated. You'll need to use nohup as well as output redirection, as explained in the wikipedia artcle on nohup, copied here for your convenience.

Nohuping backgrounded jobs is for
example useful when logged in via SSH,
since backgrounded jobs can cause the
shell to hang on logout due to a race
condition [2]. This problem can also
be overcome by redirecting all three
I/O streams:

nohup myprogram > foo.out 2> foo.err < /dev/null &

How to move a running process to background (UNIX)

Press control + Z, which will pause it and send it to the background. Then enter bg to continue it's running in the background.

Alternatively, if you put a & at the end of the command to run it in the background from the start.

This will just make it run in the background and once you log out it will still be killed. In order to keep it running after logout you will need to "disown" the process with disown -h, so that the shell doesn't count it among your processes needing to be killed on logout. See this post for more details.

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

Executing bash script via java in background after ssh connection is closed

You should add & after your command, i.e.

sshconnection.execute("nohup su - oracle -c './shortoracle.bash' &",2000);

Since nohup itself will otherwise get disconnected when you close the SSH
connection. With & you run 'nohup' itself in the background and therefore allow it to continue running after you close the SSH connection.

Hope this helps you out!



Related Topics



Leave a reply



Submit