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

18

我想让一个脚本在关闭终端后仍能在后台运行。我已经搜索并尝试使用nohupdisown,但似乎都不起作用。当我关闭一个终端窗口时,会出现典型的关闭这个窗口将终止正在运行的进程: watch。提示信息。即使使用了nohupdisown,我的后台进程仍会被终止。可能出了什么问题?

我的代码只有两行。

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

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

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

我正在使用Mac OSX。


次要相关:http://mywiki.wooledge.org/BashFAQ/050 - tripleee
警告信息似乎来自于OSX终端应用程序;这肯定不是Bash的一个特性。您是否检查过进程是否仍在运行,尽管出现了警告?如果没有,您知道是什么导致了它停止运行吗? - tripleee
进程(我定义的cmd)在终端启动后运行。当我尝试关闭终端时,该进程仍在运行。只有在关闭终端窗口后,cmd才会停止。我将尝试检查OSX终端首选项以查看是否是其原因。 - John Robins
好的,我通过切换OSX终端首选项来消除了警告,但问题仍然存在(cmd仍然被杀死)。 - John Robins
你的意思是你改变了“关闭前询问”偏好设置?那只是显然地禁用了对话框。 - tripleee
显示剩余2条评论
3个回答

26

在子shell中启动并运行nohup命令似乎可以避免终端在退出时杀死该命令。

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

不是很优美,但对我来说有效。


13
这个问题已经有答案了,但是Screen工具似乎非常适合这种情况。
  • man screen To view the documentation for screen.

  • www.ss64.com/osx/screen.html to 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.
    

当启动一个screen会话时,用户可以选择断开与其的连接。这将使得screen在后台仍然保持活跃状态,即使用户退出登录(权限除外)。

以下是一个例子:

  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 $1 ] ; do 
       echo "Count: $count of $1. 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.
    ...
    

为了检查退出后是否正在运行,请注销并从另一台计算机ssh进入计算机。screen -ls命令应该会显示与之前相同的屏幕会话。

希望这有所帮助。


屏幕非常老旧。虽然我也在使用它,但很多人已经转向了 tmux - tripleee

0

nohup 不是跨平台的解决方案。

Linux(其他):

nohup [terminal-command] > [output-file-name] &

Windows:

START /B [command-prompt-command] 2>&1

有一个更好的解决方案适用于所有平台,即pm2。 我们可以使用pm2运行任何脚本。

npx pm2 start "my-test -script' --name test"
npx pm2 list
npx pm2 delete test

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接