bash 使用 SSH 远程重启码头而不被停止

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19881730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:34:16  来源:igfitidea点击:

Restart jetty remotely using SSH without it being stopped

bashsshamazon-ec2jetty

提问by Kof

Jetty 9 is installed on EC2 linux server, jetty.sh was copied to /etc/init.d and proper links made at /etc/rc1.d, /etc/rc2.d.

Jetty 9 安装在 EC2 linux 服务器上,jetty.sh 被复制到 /etc/init.d 并在 /etc/rc1.d、/etc/rc2.d 建立正确的链接。

I'm connecting to the instance using SSH, and running jetty through sudo service jetty startwhich loads the service correctly, even after logging out of the SSH session.

我正在使用 SSH 连接到实例,并运行 jetty 通过sudo service jetty start它正确加载服务,即使在注销 SSH 会话后也是如此。

But when running a remote SSH command ssh -t -i key.pem [email protected] 'sudo service jetty restart'on my instance, it starts but then stops right after. Here's the log:

但是当ssh -t -i key.pem [email protected] 'sudo service jetty restart'在我的实例上运行远程 SSH 命令时,它会启动但随后立即停止。这是日志:

2013-11-14 18:03:01 main DispatcherServlet [INFO] FrameworkServlet 'restapi': initialization completed in 1376 ms
2013-11-14 18:03:01.824:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@67fc3482{/,file:/usr/share/jetty/jetty-distribution-9.0.6.v20130930/webapps/ROOT/,AVAILABLE}{/ROOT}
2013-11-14 18:03:01.941:INFO:oejs.ServerConnector:main: Started ServerConnector@10614f3d{HTTP/1.1}{0.0.0.0:8080}2013-11-14 18:03:02.226:INFO:oejs.ServerConnector:main: Started ServerConnector@666c1838{SSL-http/1.1}{0.0.0.0:8443}2013-11-14 18:03:05.670:INFO:oejs.ServerConnector:Thread-3: Stopped ServerConnector@10614f3d{HTTP/1.1}{0.0.0.0:8080}
2013-11-14 18:03:05.671:INFO:oejs.ServerConnector:Thread-3: Stopped ServerConnector@666c1838{SSL-http/1.1}{0.0.0.0:8443}

I've tried executing remotely sudo nohup service jetty restart &, same result.

我试过远程执行sudo nohup service jetty restart &,结果一样。

How can I restart remotely (SSH) and keep it running?

如何远程重启 (SSH) 并保持运行?

回答by slayedbylucifer

I replicated your issue on CentOS (RPM based) and it works like a charm. I installed jetty and configured it on port 7070.

我在 CentOS(基于 RPM)上复制了你的问题,它的作用就像一个魅力。我安装了 jetty 并在端口 7070 上进行了配置。

Stopping Jetty:

停靠码头:

# ssh -t -i key.pem [email protected] 'sudo service jetty stop'
Stopping Jetty: OK
Connection to ec2-xx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com closed.

Nothing is listening on port 7070:

7070 端口没有监听:

# ssh -t -i key.pem [email protected] 'sudo netstat -anp | grep 7070'
Connection to ec2-xx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com closed.

Starting jetty:

开始码头:

# ssh -t -i key.pem [email protected] 'sudo service jetty start'
Starting Jetty: . OK Thu Nov 14 08:57:19 EST 2013
Connection to ec2-xx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com closed.

port 7070 is in use now:

端口 7070 正在使用中:

# ssh -t -i key.pem [email protected] 'sudo netstat -anp | grep 7070'
tcp        0      0 :::7070                     :::*                        LISTEN      2431/java           
Connection to ec2-xx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com closed.

Here are the corresponding logs:

以下是对应的日志:

2013-11-14 08:57:15.610:INFO:oejsh.ContextHandler:main: Started o.e.j.s.h.MovedContextHandler@1c9c8aa{/oldContextPath,null,AVAILABLE}
2013-11-14 08:57:16.283:INFO:oejs.ServerConnector:main: Started ServerConnector@176982e{HTTP/1.1}{0.0.0.0:7070}
2013-11-14 08:57:16.392:INFO:oejs.ServerConnector:main: Started ServerConnector@166320b{SSL-http/1.1}{0.0.0.0:8443}

And here is the platform I used:

这是我使用的平台:

# ssh -t -i key.pem [email protected] 'sudo lsb_release -a'
LSB Version:    :core-4.0-ia32:core-4.0-noarch:graphics-4.0-ia32:graphics-4.0-noarch:printing-4.0-ia32:printing-4.0-noarch
Distributor ID: CentOS
Description:    CentOS release 6.3 (Final)
Release:    6.3
Codename:   Final
Connection to ec2-xx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com closed.

And finally, here is hte restartcommand:

最后,这是 hterestart命令:

# ssh -t -i key.pem [email protected] 'sudo service jetty restart'
Stopping Jetty: OK
Starting Jetty: . OK Thu Nov 14 09:13:00 EST 2013
Connection to ec2-xx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com closed.

I ran all above commands via sudoto make sure that sudo is not the issue.

我通过运行以上所有命令sudo以确保 sudo 不是问题。

  • Were there any changes on your instance?
  • Were you able to start jetty via SSH n sudo in the past successfully? if yes, then what has changed since then?
  • If this is new installation, then spin-up a new instance and check whether you still face the same issue.
  • 您的实例有任何变化吗?
  • 您过去是否能够通过 SSH n sudo 成功启动 jetty?如果是,那么从那以后发生了什么变化?
  • 如果这是新安装,则启动一个新实例并检查您是否仍然面临同样的问题。

BTW, I am not a java guy and followed theseinstruction to install jetty 9.

顺便说一句,我不是 Java 人,而是按照这些说明安装了 jetty 9。

回答by user2284570

I think that the init script teminate before Jetty is fully launched so it still need the shell during some seconds after the script termination.
Just try:

我认为 init 脚本在 Jetty 完全启动之前终止,因此在脚本终止后的几秒钟内它仍然需要 shell。
你试一试:

ssh -t -i key.pem [email protected] 'sh -c "sudo service jetty restart;sleep 8"'

Again, you may try to redirect input from your script

同样,您可以尝试从脚本重定向输入

ssh -t -i key.pem [email protected] <<'FIN'
#commands to run on remote host
/bin/bash --login -i -s << FAIM
su -l -s /bin/bash << FEIN
your_password
nohup service jetty restart &
bg
disown -a
sleep 20
FEIN
sleep 5
FAIM
FIN

An another alternative is to ask to the red hat support, since you're paying for it with your Amazon cloud service.

另一种选择是请求红帽支持,因为您是通过 Amazon 云服务付费的。

回答by ArturFH

Try to redirect I/O using following command:

尝试使用以下命令重定向 I/O:

ssh -t -i key.pem [email protected] 'sudo service jetty restart </dev/null >/dev/null 2>/dev/null'

If it helps, you can try with only >/dev/null.

如果有帮助,您可以仅尝试使用 >/dev/null。

回答by user2284570

Working Solution:

工作解决方案:

  1. On EC2 instance, edit the sudoers file with what you want.
  2. Then, restart Jetty remotely with this command: ssh -i key.pem [email protected] 'sudo service jetty restart', notice that -t was removed.
  3. If command prompt doesn't come back after restart finishes, add a >/dev/null at the end.
  1. 在 EC2 实例上,根据需要编辑 sudoers 文件。
  2. 然后,使用以下命令远程重启 Jetty:ssh -i key.pem [email protected] 'sudo service jetty restart',注意 -t 已被删除。
  3. 如果重启完成后命令提示符没有返回,请在最后添加 >/dev/null。

回答by michael501

try this , use -f to force ssh into background :

试试这个,使用 -f 强制 ssh 进入后台:

 ssh -f -t -i key.pem [email protected] 'sudo service jetty restart' 

回答by pgl

Does this work, from my comment above:

这是否有效,从我上面的评论来看:

ssh -t -i key.pem [email protected] "sudo su - -c 'service jetty start'"

?

?