Bash 脚本没有 运行 在后台使用 nohup

Bash script not running in background with nohup

我有一个 bash 激活 anaconda 环境的脚本和 运行 一个 python 调度程序脚本,每分钟将日志写入文件。如果我 运行 只是脚本,它工作得很好。

[user@host proj]$ test.sh

按 ctrl-C 后,我看到每分钟都有日志。

[user@host proj]$ cat logs/log.log
Test job 1 executed at : 2018-10-09 14:16:00.000787
Test job 1 executed at : 2018-10-09 14:17:00.001890
Test job 1 executed at : 2018-10-09 14:18:00.001861

但是当我在后台使用 nohup运行 相同的脚本时

[user@host proj]$ nohup test.sh &
[1] 24884
[user@host proj]$ nohup: ignoring input and appending output to ‘nohup.out’

我可以看到 top 脚本和 python 是 运行ning

24949 user  20   0  113172   1444   1216 S  0.0  0.0   0:00.00 test.sh
24952 user  20   0  516332  66644  17344 S  0.0  0.8   0:00.65 python

但是我看不到要写入日志文件的任何内容。

不确定出了什么问题。非常感谢任何指导我正确方向的建议。

我假设 proj 目录在您的 PATH 变量中。

如果 nohup.out 中没有打印任何内容,那么我认为您没有任何内容可以回显到 nohup.out 文件中。尝试在您的 shebang 行下方使用 set -xv,即如果您使用 bash,则为 #/bin/bash。现在在 运行 shell 脚本上,nohup 将至少有一个 python 脚本的条目,你是 运行 来自你的 shell 脚本,如下所示。

user@host:~/scripts$ cat nohup.out

python /home/madbala/scripts/append_file.py + python /home/madbala/scripts/append_file.py

我已尝试重现您的情况如下:

  1. Python 脚本名称(抱歉,我不太了解 python):append_file.py

    #!/usr/bin/python
    from datetime import datetime
    import time
    with open("test.txt", "a") as myfile:
       for x in range(15):
          myfile.write('appended text %s \n' %datetime.now())
          time.sleep(2)
    
  2. Shell 调用上面的脚本 python 脚本:run_py_script.sh

      #!/bin/bash
      set -xv
      python /home/madbala/scripts/append_file.py
    

现在在 运行 nohup run_py_script.sh & 上,我得到了 set -xv 的输出(这实际上启用了 shell 脚本中的详细日志记录 - 你也可以只使用set -x).

当我使用 #set -xv 注释掉 set -xv 时 nohup.out 将没有内容。