简体   繁体   English

使用 SSH 在远程服务器上启动后台进程,并退出会话

[英]Use SSH to start a background process on a remote server, and exit session

I am using SSH to start a background process on a remote server.我正在使用 SSH 在远程服务器上启动后台进程。 This is what I have at the moment:这就是我目前所拥有的:

ssh remote_user@server.com "nohup process &" ssh remote_user@server.com "nohup 进程 &"

This works, in that the process does start.这是有效的,因为该过程确实开始了。 But the SSH session itself does not end until I hit Ctr-C.但是 SSH 会话本身直到我按下 Ctr-C 才结束。

When I hit Ctr-C, the remote process continues to run in the background.当我按下 Ctr-C 时,远程进程继续在后台运行。

I would like to place the ssh command in a script that I can run locally, so I would like the ssh session to exit automatically once the remote process has started.我想将 ssh 命令放在我可以在本地运行的脚本中,因此我希望 ssh 会话在远程进程启动后自动退出。

Is there a way to make this happen?有没有办法做到这一点?

The "-f" option to ssh tells ssh to run the remote command in the background and to return immediately. ssh 的“-f”选项告诉 ssh 在后台运行远程命令并立即返回。 Eg,例如,

ssh -f user@host "echo foo; sleep 5; echo bar"

If you type the above, you will get your shell prompt back immediately, you will then see "foo" output.如果您键入上述内容,您将立即返回您的 shell 提示符,然后您将看到“foo”输出。 Five seconds later you will then see "bar" output.五秒钟后,您将看到“bar”输出。 In the meantime, you could have been using the shell.同时,您可能一直在使用 shell。

When using nohup , make sure you also redirect stdin, stdout and stderr:使用nohup ,请确保您还重定向 stdin、stdout 和 stderr:

ssh user@server 'DISPLAY=:0 nohup xeyes < /dev/null > std.out 2> std.err &'

In this way you will be completely detached from the remote process.通过这种方式,您将与远程进程完全分离。 Be carefull with using ssh -f user@host... since that will only put the ssh process in the background on the calling side.使用ssh -f user@host...要小心,因为这只会将 ssh 进程置于调用方的后台。 You can verify this by running a ps -aux | grep ssh您可以通过运行ps -aux | grep ssh来验证这ps -aux | grep ssh ps -aux | grep ssh on the calling machine and this will show you that the ssh call is still active, but just put in the background. ps -aux | grep ssh在调用机器上,这将显示 ssh 调用仍然处于活动状态,但只是放在后台。

In my example above I use DISPLAY=:0 since xeyes is an X11 program and I want it started on the remote machine.在我上面的例子中,我使用DISPLAY=:0因为 xeyes 是一个 X11 程序,我希望它在远程机器上启动。

You could use screen to run your process on this screen, detach from screen Ctrl-a :detach and exit your current session without problem.您可以使用screen在此屏幕上运行您的进程,从屏幕上分离Ctrl-a :detach并毫无问题地退出当前会话。 Then you can reconnect to SSH and attach to this screen again to continue with your task or check if is finished.然后您可以重新连接到 SSH 并再次附加到此屏幕以继续您的任务或检查是否已完成。

Or you can send the command to an already running screen.或者您可以将命令发送到已经运行的屏幕。 Your local script should look like this:您的本地脚本应如下所示:

ssh remote_user@server.com
screen -dmS new_screen sh
screen -S new_screen -p 0 -X stuff $'nohup process \n'
exit    

For more info see this tutorial有关更多信息,请参阅本教程

Well this question is almost 10 years old, but I recently had to launch a very long script (taking several hours to complete) on a remote server and I found a way using the crontab.这个问题已经有将近 10 年的历史了,但我最近不得不在远程服务器上启动一个很长的脚本(需要几个小时才能完成),我找到了一种使用 crontab 的方法。

If can edit your user's crontab on the remote server, connect with ssh to the server, edit the crontab and add an entry that will start your script the next minute.如果可以在远程服务器上编辑用户的 crontab,请使用 ssh 连接到服务器,编辑 crontab 并添加将在下一分钟启动脚本的条目。 Let's say it's 15h03.假设现在是 15 点 03 分。 Add this line :添加这一行:

4 15 * * * /path/to/your/script.sh

save your crontab, wait a minute for the script to be launched.保存您的 crontab,等待脚本启动。 Then edit again your crontab to remove this entry.然后再次编辑您的 crontab 以删除此条目。

You can then safely exit ssh, even shut down your computer while the script is running.然后您可以安全地退出 ssh,甚至可以在脚本运行时关闭您的计算机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM