在shell中运行一个命令,并在关闭会话时继续运行该命令

时间:2021-10-21 23:01:57

I am using Putty to connect to a remote server. What I want to know is if there is any way to write my commands and allow them to keep running after I close the session with Putty. The reason for this is that I do not want to keep the computer ON all the time. Is there any way to do this?.

我正在使用Putty连接远程服务器。我想知道的是,在我用Putty关闭会话之后,是否有办法编写命令并允许它们继续运行。原因是我不想让电脑一直开着。有什么办法吗?

Update with the solution

更新的解决方案

For my question as it is presented the best solution is use one of the commands provided such as nohup, because you do not have to install any additional software. But if you are in the same problem use screen, install it and use it. It is amazing.

对于我提出的问题,最好的解决方案是使用nohup之类的命令,因为您不必安装任何其他软件。但是如果您遇到同样的问题,请使用屏幕,安装并使用它。它是令人惊异的。

I have selected the answer of Norman Ramsey as favourite because propose several solutions using commands and screen. But please check the other answers specially the one of PEZ, then you get an insight of what screen is able todo.

我选择了Norman Ramsey的答案作为最受欢迎的答案,因为我提出了一些使用命令和屏幕的解决方案。但是请您查看一下其他的答案,特别是PEZ的答案,然后您就可以了解什么屏幕可以做。

9 个解决方案

#1


28  

nohup, disown, and screen are all good but screen is the best because unlike the other two, screen allows you to disconnect from the remote server, keep everything running, and then reconnect later to see what is happening. With nohup and disown you can't resume interacting.

nohup, disown,和screen都很好,但是screen是最好的,因为与其他两个不同,screen允许您断开与远程服务器的连接,保持一切运行,然后再重新连接,看看发生了什么。有了nohup和disown,你就不能继续互动了。

#2


84  

screen! It's the best thing since sliced bread. (Yeah, I know others have already suggested it, but it's so good the whole world should join in and suggest it too.)

屏幕!这是有史以来最好的事情。(是的,我知道其他人已经建议过了,但它太好了,全世界都应该加入进来并提出建议。)

screen is like, like, ummmm ... like using VNC or the like to connect to a GUI destop, but for command shell windows. You can have several shell "windows" open at once in the same screen session. You can do stuff like:

屏幕就像,嗯……就像使用VNC或类似于连接到GUI destop,但是对于命令shell窗口。您可以在同一屏幕会话中同时打开多个shell“windows”。你可以这样做:

  1. Start a screens session using "screen -dR" (get used to using -dR)
    • run some commands in one window
    • 在一个窗口中运行一些命令
    • press CTRL-A,C to create a new window open a file there in vim
    • 按CTRL-A,C创建一个新窗口,在vim中打开一个文件。
    • press CTRL-A,0 to go back to the first window and issue some command on the file you just edited
    • 按CTRL-A,0返回到第一个窗口,并在刚才编辑的文件上发出一些命令
    • CTRL-A, 1 to go back to your vim session
    • CTRL-A, 1返回到您的vim会话
    • CTRL-A, C for yet another window and maybe do "sudo - su" (because you just happen to need a full root shell)
    • CTRL-A、C代表另一个窗口,也可以使用“sudo - su”(因为您恰好需要一个完整的根shell)
    • CTRL-A, 0 and start a background process
    • CTRL-A, 0,启动后台进程
    • CTRL-A, C to create yet a new window, "tail -f" the log for that background process
    • CTRL-A, C创建一个新的窗口,“tail -f”是后台进程的日志
    • CTRL-A, d to disconnect your screen then CTRL-D to disconnect from the server
    • 按下CTRL-A, d断开您的屏幕,然后按下CTRL-D断开与服务器的连接
    • Go on vacation for three weeks
    • 去度假三个星期
    • Log on to the server again and issue "screen -dR" to connect to your existing screen session
    • 再次登录服务器并发出“screen -dR”以连接到现有的屏幕会话
    • check the log in the the fourth window with CTRL-A, 3 (it's like you've been there watching it all the time)
    • 用CTRL-A、3检查第四个窗口中的日志(就好像你一直在那里看它一样)
    • CTRL-A, 1 to pick up that vim session again
    • CTRL-A, 1再次获取vim会话
    • I guess you're starting to get the picture now? =)
    • 我猜你现在开始明白了吧?=)
  2. 启动一个屏幕会话使用“屏幕是”(习惯使用是)运行一些命令在一个窗口按ctrl - a,C来创建一个新窗口打开一个文件在vim按ctrl - a,0回到第一个窗户,发出一些命令在文件编辑ctrl - a,1回到您的vim会话ctrl - a、C为另一个窗口,也许做“sudo -苏”(因为你刚好需要一个完整的根壳)ctrl - a,0和启动后台进程ctrl - a,C还创建一个新窗口,“tail - f”后台进程的日志ctrl - a、d断开您的屏幕然后ctrl - d从服务器断开去度假三个星期再次登录到服务器,问题“屏幕是连接到现有的屏幕会话检查日志与ctrl - a在第四个窗口,3(就像你一直看它)ctrl - a,1再次拾起那个vim会话我猜你现在开始懂了吗?=)

It's like magic. I've been using screen for longer than I can remember and I'm still totally amazed with how bloody great it is.

这就像魔术。我使用屏幕的时间已经超过了我的记忆,我仍然对它的伟大程度感到惊讶。

EDIT: Just want to mention there's now also tmux. Very much like screen, but has some unique features, splitting the windows being the most prominent one.

编辑:我想说的是现在还有tmux。很像屏幕,但是有一些独特的功能,把窗口分成了最突出的一个。

#3


10  

Try using GNU Screen. It allows you to have several shells open at once. And you can disconnect from those running shells (i.e. close session with Putty) and they will keep doing their thing.

尝试使用GNU的屏幕。它允许你同时打开几个壳层。你可以断开那些正在运行的shell(例如关闭session with Putty),它们就会继续做它们的事情。

#4


9  

What you are looking for is nohup.

你要找的是nohup。

See the wiki link for how to use it.

参见wiki链接了解如何使用它。

#5


5  

screen is the best.

屏幕上是最好的。

Try:

试一试:

screen -dmS "MyTail" tail -f /var/log/syslog

屏幕-dmS“MyTail”尾部-f /var/log/syslog

This start command in background.

在后台启动命令。

Use screen -r to list, and or screen -r Mytail to enter session.

使用screen -r列表,或者使用screen -r Mytail输入会话。

If more users need access same session, use: screen -rx MyTail, and both or more users share the session.

如果有更多用户需要访问相同的会话,请使用:screen -rx MyTail,并让两个或多个用户共享会话。

#6


4  

If you can't use screen (because, for instance, your SSH session is being programmatically driven), you can also use daemonize to run the program as a daemon.

如果您不能使用screen(因为,例如,您的SSH会话是通过编程驱动的),您还可以使用守护进程来运行程序。

#7


3  

One way that works well for me is at.

有一种方法对我很有效。

at works like cron, but for a one-time job. I used it today to download a large file without having to keep my session alive.

在像cron这样的工作中,但只做一份一次性的工作。我今天用它来下载一个大文件,而不用让我的会话存活。

for example:

例如:

$ at 23:55
at> wget http://file.to.download.com/bigfile.iso
at> ^D  

You pass at a time (in the future) and it gives you a prompt. You enter the commands you want to run at that time and hit ctrl+d. You can exit out of your session and it will run the commands at the specified time.

你每次(将来)通过,它就会提示你。输入当时想要运行的命令并按ctrl+d。您可以退出会话,它将在指定的时间运行命令。

Wikipedia has more info on at.

*有更多的信息。

#8


1  

./command & disown

#9


0  

ssh localhost && ./command && exit

#1


28  

nohup, disown, and screen are all good but screen is the best because unlike the other two, screen allows you to disconnect from the remote server, keep everything running, and then reconnect later to see what is happening. With nohup and disown you can't resume interacting.

nohup, disown,和screen都很好,但是screen是最好的,因为与其他两个不同,screen允许您断开与远程服务器的连接,保持一切运行,然后再重新连接,看看发生了什么。有了nohup和disown,你就不能继续互动了。

#2


84  

screen! It's the best thing since sliced bread. (Yeah, I know others have already suggested it, but it's so good the whole world should join in and suggest it too.)

屏幕!这是有史以来最好的事情。(是的,我知道其他人已经建议过了,但它太好了,全世界都应该加入进来并提出建议。)

screen is like, like, ummmm ... like using VNC or the like to connect to a GUI destop, but for command shell windows. You can have several shell "windows" open at once in the same screen session. You can do stuff like:

屏幕就像,嗯……就像使用VNC或类似于连接到GUI destop,但是对于命令shell窗口。您可以在同一屏幕会话中同时打开多个shell“windows”。你可以这样做:

  1. Start a screens session using "screen -dR" (get used to using -dR)
    • run some commands in one window
    • 在一个窗口中运行一些命令
    • press CTRL-A,C to create a new window open a file there in vim
    • 按CTRL-A,C创建一个新窗口,在vim中打开一个文件。
    • press CTRL-A,0 to go back to the first window and issue some command on the file you just edited
    • 按CTRL-A,0返回到第一个窗口,并在刚才编辑的文件上发出一些命令
    • CTRL-A, 1 to go back to your vim session
    • CTRL-A, 1返回到您的vim会话
    • CTRL-A, C for yet another window and maybe do "sudo - su" (because you just happen to need a full root shell)
    • CTRL-A、C代表另一个窗口,也可以使用“sudo - su”(因为您恰好需要一个完整的根shell)
    • CTRL-A, 0 and start a background process
    • CTRL-A, 0,启动后台进程
    • CTRL-A, C to create yet a new window, "tail -f" the log for that background process
    • CTRL-A, C创建一个新的窗口,“tail -f”是后台进程的日志
    • CTRL-A, d to disconnect your screen then CTRL-D to disconnect from the server
    • 按下CTRL-A, d断开您的屏幕,然后按下CTRL-D断开与服务器的连接
    • Go on vacation for three weeks
    • 去度假三个星期
    • Log on to the server again and issue "screen -dR" to connect to your existing screen session
    • 再次登录服务器并发出“screen -dR”以连接到现有的屏幕会话
    • check the log in the the fourth window with CTRL-A, 3 (it's like you've been there watching it all the time)
    • 用CTRL-A、3检查第四个窗口中的日志(就好像你一直在那里看它一样)
    • CTRL-A, 1 to pick up that vim session again
    • CTRL-A, 1再次获取vim会话
    • I guess you're starting to get the picture now? =)
    • 我猜你现在开始明白了吧?=)
  2. 启动一个屏幕会话使用“屏幕是”(习惯使用是)运行一些命令在一个窗口按ctrl - a,C来创建一个新窗口打开一个文件在vim按ctrl - a,0回到第一个窗户,发出一些命令在文件编辑ctrl - a,1回到您的vim会话ctrl - a、C为另一个窗口,也许做“sudo -苏”(因为你刚好需要一个完整的根壳)ctrl - a,0和启动后台进程ctrl - a,C还创建一个新窗口,“tail - f”后台进程的日志ctrl - a、d断开您的屏幕然后ctrl - d从服务器断开去度假三个星期再次登录到服务器,问题“屏幕是连接到现有的屏幕会话检查日志与ctrl - a在第四个窗口,3(就像你一直看它)ctrl - a,1再次拾起那个vim会话我猜你现在开始懂了吗?=)

It's like magic. I've been using screen for longer than I can remember and I'm still totally amazed with how bloody great it is.

这就像魔术。我使用屏幕的时间已经超过了我的记忆,我仍然对它的伟大程度感到惊讶。

EDIT: Just want to mention there's now also tmux. Very much like screen, but has some unique features, splitting the windows being the most prominent one.

编辑:我想说的是现在还有tmux。很像屏幕,但是有一些独特的功能,把窗口分成了最突出的一个。

#3


10  

Try using GNU Screen. It allows you to have several shells open at once. And you can disconnect from those running shells (i.e. close session with Putty) and they will keep doing their thing.

尝试使用GNU的屏幕。它允许你同时打开几个壳层。你可以断开那些正在运行的shell(例如关闭session with Putty),它们就会继续做它们的事情。

#4


9  

What you are looking for is nohup.

你要找的是nohup。

See the wiki link for how to use it.

参见wiki链接了解如何使用它。

#5


5  

screen is the best.

屏幕上是最好的。

Try:

试一试:

screen -dmS "MyTail" tail -f /var/log/syslog

屏幕-dmS“MyTail”尾部-f /var/log/syslog

This start command in background.

在后台启动命令。

Use screen -r to list, and or screen -r Mytail to enter session.

使用screen -r列表,或者使用screen -r Mytail输入会话。

If more users need access same session, use: screen -rx MyTail, and both or more users share the session.

如果有更多用户需要访问相同的会话,请使用:screen -rx MyTail,并让两个或多个用户共享会话。

#6


4  

If you can't use screen (because, for instance, your SSH session is being programmatically driven), you can also use daemonize to run the program as a daemon.

如果您不能使用screen(因为,例如,您的SSH会话是通过编程驱动的),您还可以使用守护进程来运行程序。

#7


3  

One way that works well for me is at.

有一种方法对我很有效。

at works like cron, but for a one-time job. I used it today to download a large file without having to keep my session alive.

在像cron这样的工作中,但只做一份一次性的工作。我今天用它来下载一个大文件,而不用让我的会话存活。

for example:

例如:

$ at 23:55
at> wget http://file.to.download.com/bigfile.iso
at> ^D  

You pass at a time (in the future) and it gives you a prompt. You enter the commands you want to run at that time and hit ctrl+d. You can exit out of your session and it will run the commands at the specified time.

你每次(将来)通过,它就会提示你。输入当时想要运行的命令并按ctrl+d。您可以退出会话,它将在指定的时间运行命令。

Wikipedia has more info on at.

*有更多的信息。

#8


1  

./command & disown

#9


0  

ssh localhost && ./command && exit