bash 关闭终端后在后台运行进程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32243553/
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 13:30:12  来源:igfitidea点击:

Running process in background after closing terminal

macosbashdaemonnohup

提问by John Robins

I am trying to run a script in the background even after closing the terminal. I have searched and searched and tried nohupand disownbut neither seem to be working. When I close a terminal window, I get the typical Closing this window will terminate the running processes: watch.message. That ends up terminating my background process, even when using nohupor disown. What could be the problem?

即使在关闭终端后,我也试图在后台运行脚本。我已经搜索、搜索和尝试nohupdisown但似乎都没有工作。当我关闭终端窗口时,我会收到典型的Closing this window will terminate the running processes: watch.消息。这最终会终止我的后台进程,即使使用nohupdisown。可能是什么问题呢?

My code is a simple two lines

我的代码是简单的两行

cmd="nohup watch -n 1 sudo /etc/block.sh > /dev/null"
$cmd & # blocks automatically  

It is located in .bash_profile, because I want it to start up whenever I open a new terminal.

它位于 中.bash_profile,因为我希望它在我打开新终端时启动。

You can ignore the sudo; I've already found a way to execute a sudo command without entering the password.

您可以忽略 sudo;我已经找到了一种无需输入密码即可执行 sudo 命令的方法。

I am using Mac OSX.

我正在使用 Mac OSX。

回答by tripleee

Starting a subshell and running the nohupcommand from there seems to avoid having Terminal kill it off when exiting.

启动子shell并nohup从那里运行命令似乎可以避免在退出时让终端将其杀死。

bash -c "nohup sh -c 'while true; do date; sleep 1; done' &"

Not very elegant, but works for me.

不是很优雅,但对我有用。

回答by Thoth

This is already answered, but the Screen utility seems like it would be perfect for this.

这已经得到了回答,但 Screen 实用程序似乎非常适合此问题。

  • man screenTo view the documentation for screen.

  • www.ss64.com/osx/screen.htmlto view slightly more user friendly documentation.

  • Start screen with a name and a script to run:

    screen -S GWatch Scripts/gw_watch.sh
    This starts a screen session named 'GWatch' and executes gw_watch.sh.
    
  • man screen查看 screen 的文档。

  • www.ss64.com/osx/screen.html查看稍微更用户友好的文档。

  • 带有名称和要运行的脚本的启动屏幕:

    screen -S GWatch Scripts/gw_watch.sh
    This starts a screen session named 'GWatch' and executes gw_watch.sh.
    

When a screen session is started, one has the option of disconnecting from it. This will leave the screen active in the background. It will remain active even after the user logs out (permissions notwithstanding).

当屏幕会话开始时,可​​以选择与其断开连接。这将使屏幕在后台处于活动状态。即使在用户注销后它仍将保持活动状态(尽管有权限)。

Here is an example:

下面是一个例子:

  1. Create a shell script called 'screencheck.sh'
  2. Put the following into the file (I often use textwrangler and / or nano).

    #!/bin/bash
    
    count=0
    
    while [ $count -lt  ] ; do 
       echo "Count: $count of . Pausing for five seconds."
       sleep 5s
       ((count++))
    done
    
  3. Open two terminal windows.

  4. In one of the terminal windows type screen -ls. You should see a message about no sockets being found.
  5. In the second terminal window, change directory to where the script was saved.
  6. In the second terminal window type screen -S ScreenCheck screencheck.sh 500. screencheck.sh has to be executable.
  7. In the second terminal window, you should see:

    Count: 0 of 500. Pausing for five seconds.
    Count: 1 of 500. Pausing for five seconds.
    Count: 2 of 500. Pausing for five seconds.
    ...
    
  8. Disconnect from the screen session by typing ctrl-a d. That's control + a, release both, d key.
  9. You should see [detached].
  10. In the first terminal, type screen -ls.
  11. You should see something like:

    FCH000: ~: screen -ls
    There is a screen on:
       1593.ScreenCheck (Detached)
    1 Socket in /var/folders/pk/l6b5fhkj6mxfpfh8mtgmstg40000gn/T/.screen.
    
  12. Reattach to the screen session using screen -R ScreenCheck.

  13. You should see something like:

    Count: 226 of 500. Pausing for five seconds.
    Count: 227 of 500. Pausing for five seconds.
    Count: 228 of 500. Pausing for five seconds.
    Count: 229 of 500. Pausing for five seconds.
    ...
    
  1. 创建一个名为“screencheck.sh”的 shell 脚本
  2. 将以下内容放入文件中(我经常使用 textwrangler 和/或 nano)。

    #!/bin/bash
    
    count=0
    
    while [ $count -lt  ] ; do 
       echo "Count: $count of . Pausing for five seconds."
       sleep 5s
       ((count++))
    done
    
  3. 打开两个终端窗口。

  4. 在其中一个终端窗口中键入screen -ls. 您应该会看到一条关于未找到套接字的消息。
  5. 在第二个终端窗口中,将目录更改为保存脚本的位置。
  6. 在第二个终端窗口中输入screen -S ScreenCheck screencheck.sh 500. screencheck.sh 必须是可执行的。
  7. 在第二个终端窗口中,您应该看到:

    Count: 0 of 500. Pausing for five seconds.
    Count: 1 of 500. Pausing for five seconds.
    Count: 2 of 500. Pausing for five seconds.
    ...
    
  8. 通过键入断开与屏幕会话的连接ctrl-a d。那是 control + a,同时释放,d 键。
  9. 你应该看到[detached]
  10. 在第一个终端中,键入screen -ls
  11. 你应该看到类似的东西:

    FCH000: ~: screen -ls
    There is a screen on:
       1593.ScreenCheck (Detached)
    1 Socket in /var/folders/pk/l6b5fhkj6mxfpfh8mtgmstg40000gn/T/.screen.
    
  12. 使用 重新附加到屏幕会话screen -R ScreenCheck

  13. 你应该看到类似的东西:

    Count: 226 of 500. Pausing for five seconds.
    Count: 227 of 500. Pausing for five seconds.
    Count: 228 of 500. Pausing for five seconds.
    Count: 229 of 500. Pausing for five seconds.
    ...
    

To see if it is running after logout, log out and ssh to the computer from another computer. screen -lsshould show the same screen session as before.

要查看注销后它是否正在运行,请注销并从另一台计算机 ssh 到该计算机。screen -ls应该显示与以前相同的屏幕会话。

I hope this helps.

我希望这有帮助。