0

我有一个 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 正在运行

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

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

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

4

1 回答 1

1

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

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

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. 调用以上 python 脚本的 Shell 脚本: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 -xvusing #set -xvnohup.out 将没有内容。

截屏

于 2018-10-09T22:52:36.500 回答