Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

This is still blocking:

ssh host nohup cmd

This still leaves the connection open:

ssh host nohup cmd &
share|improve this question

migrated from stackoverflow.com Mar 22 '10 at 23:39

This question came from our site for professional and enthusiast programmers.

    
Is there really no way to properly do this? I've been struggling all day, and none of the options seem to leave the remote process running AND terminate the local SSH process. The best I've got so far is to run the SSH in the background, wait 15 seconds, then kill the PID. But that's ugly. –  Hammer Bro. May 23 '11 at 22:25
add comment

3 Answers 3

Try this:

ssh host batch cmd
share|improve this answer
    
This is a nice approach, but note that batch depends on having a working cron/at system on the remote system. –  coneslayer Mar 26 '10 at 17:46
add comment

The & in your example will detach the ssh command, but not your remote program.

This seems to work:

ssh remote-host 'tail -f /var/log/syslog &' &

ssh remote-host 'tail -f /var/log/syslog > /dev/null &' &

The first & will detach the command for the remote host, and the second & will detach the ssh command itself

In my example, the tail command is still running after I've closed the connection.


edit this does not seems to work as tail exits shortly after the connection is actually closed.

This may be related to the fact that it's writing to STDOUT which will probably raise a broken pipe after the connection is closed

edit 2 works fine when redirecting the tail command to /dev/null ^_^

Just be careful and don't write to stderr / stdout, or redirect the output to a local destination

share|improve this answer
add comment

The following will work:

ssh myhost " nohup ./r.sh & " & sleep 2 ; kill -9 $! && echo

(at least if you are not prompted for a password). In case you are I don't see a easy way to do it.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.