简体   繁体   English

中止shell脚本会中止正在执行的命令

[英]Will Aborting a shell script abort the command it is executing

I am a noob to shell scripting, I tried searching but could not find a solution. 我是shell脚本的新手,我尝试搜索但找不到解决方案。

I have a bad internet connection and it keeps dropping. 我的互联网连接状况不佳,并且一直在下降。 I have to run certain commands on a remote server. 我必须在远程服务器上运行某些命令。 When I ssh on a terminal and run those commands and the connection drops, I'm not sure if the command continues or it also stops (I assume that it would behave as if a Ctrl^C has been received. Am I correct?) 当我在终端上ssh并运行这些命令并且连接断开时,我不确定该命令是否继续还是也停止了(我假设它的行为就像已收到Ctrl ^ C一样。我对吗?)

Now i've put all the commands inside a shell script. 现在,我已将所有命令放入外壳脚本中。 ftp-ed it on the server and am running it on a terminal via ssh. 在服务器上ftp-ed它,并通过ssh在终端上运行它。 Will this guarantee that my commands would run even if the connection gets dropped. 这样可以保证即使断开连接我的命令也可以运行。 Or should I wrap it inside another shell script? 还是应该将其包装在另一个Shell脚本中?

Thank you. 谢谢。

If you need to preserve the command that is run remotely after the connection drops use nohup . 如果需要保留断开连接后远程运行的命令,请使用nohup For details see man nohup . 有关详细信息,请参见man nohup I guess all you need to do is to run on remote side: 我想您需要做的就是在远程运行:

nohup /path/to/your_script.sh &

More precisely, whenever the connection is dropped, on the server side timeout occurs, the socket is closed and with it all the processes that belong to the process tree of the ssh connection process. 更确切地说,每当连接断开时,在服务器端就会发生超时,套接字将关闭,并且所有属于ssh连接进程的进程树的进程将被关闭。

Do you need to see the output? 您是否需要查看输出?

If so, then use screen , which effectively runs everything on the server and lets you connect to the screen remotely. 如果是这样,请使用screen ,它可以有效地运行服务器上的所有内容,并允许您远程连接到屏幕。

If you don't, then you can use nohup to background the process. 如果不这样做,则可以使用nohup来使该过程成为后台。 When doing so, make sure you redirect the standard streams ie 这样做时,请确保您重定向标准流,即

nohup script_to_run &>/dev/null </dev/null &

screen会话中使用nohup运行脚本或运行脚本

To guarantee a command or script will run even if connection is dropped, use nohup . 要确保即使断开连接也可以运行命令或脚本,请使用nohup

For even more control, use GNU Screen (you will be able to reconnect and get back to the terminal of the running command). 要进行更多控制,请使用GNU屏幕(您将能够重新连接并返回到运行命令的终端)。

man nohup and man screen have more info. man nohupman screen有更多信息。

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

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