1

let's say I have a really-long-running.sh ("really long" as in "hours").

I should like to have a remote machine execute it (and then shut down the local machine).

Unfortunately, I don't have much experience with ssh. Does this do what I want it to do or does it just mask the connection?

ssh user@host "bash -s " < really-long-running.sh &

UPDATE

It doesn't.

How then can I do this?

User1291
  • 547
  • 3
  • 7
  • 22
  • Some explanation: ssh user@host "bash -s " < really-long-running.sh & feeds bash running on the remote side with commands from local .sh file through ssh which runs locally in background. I think even if you make remote bash survive your disconnection, it will stop reading the script when ssh closes (when you shut down the local machine). Your script should be there on the remote side so the remote bash can read it when you disconnect. It's not enough to make bash survive your disconnection though – you still have to pick one of the solutions from the answers. – Kamil Maciorowski Oct 12 '16 at 16:51

4 Answers4

2

There is also nohup tool. From man nohup:

nohup - run a command immune to hangups, with output to a non-tty

You can work step by step:

  • ssh to the remote host
  • nohup /path/to/really-long-running.sh > /path/to/where-to-store-the-output &
  • may disconnect now;

or at once from your local machine:

ssh remote-host 'nohup /path/to/really-long-running.sh > /path/to/where-to-store-the-output &'

These examples require the script to be executable and reside on the remote host. In your code the script may not be executable and it is a local file.

1

I recommend you use screen or tmux on the executing side - this allows you to disconnect and reconnect at will, giving you the option to read the output of your script at a later point in time.

Eugen Rieck
  • 20,271
  • screen doesn't work. as soon as I shut down the local machine, the session is terminated and the process consequently stopped. And judging by its description, it's the same with tmux. – User1291 Oct 12 '16 at 15:48
  • @User1291 How exactly do you use screen on the executing side so it is stopped as soon as you shut down the local machine? – Kamil Maciorowski Oct 12 '16 at 15:58
  • @KamilMaciorowski screen, then ssh into the remote, issue the command then Ctrl+A, D and shutdown. – User1291 Oct 12 '16 at 16:04
  • @User1291 Use screen on the executing (remote) side. – Kamil Maciorowski Oct 12 '16 at 16:12
  • @KamilMaciorowski I'm not really fond of the idea of having to install screen on all remotes I may want to access. I may not even be able to, in some cases. – User1291 Oct 12 '16 at 16:16
1

That 'may' do what you want (ssh in again afterwards and check with top?)

However, the ideal ways to do this are to start the command in a screen session (or tmux) which then allows you to disconnect from this running session, while still viewing what it's doing

Or you can ssh in, then 'disconnect' the process from the current terminal by backgrounding it and then disconnecting it using

ctrl + z
bg
disown

Or you can background the process from the command line via adding & to your command. You'll still need to disown it to stop it dying when you disconnect however.

  • I don't want to "background" the session, I want to actually end the session and be able to shut down the local machine. – User1291 Oct 12 '16 at 15:50
  • 1
    The disown will prevent it from dying when you disconnect your ssh session. – MikeA Oct 12 '16 at 16:16
1

You can use at on the remote machine to schedule your job.

$ ssh remote-host
$ at now + 5 min
at> really-long-running.sh
at> <Ctrl-D>
job 1 at 2016-10-12 09:10
$ exit

You job will run per the schedule you define for the at job. Check the manpage for at for more details. Default configuration allows any user to use at.

MikeA
  • 168