【问题标题】:how to fined a process started with nohup after closing terminal?关闭终端后如何找到以 nohup 启动的进程?
【发布时间】:2018-07-06 10:03:54
【问题描述】:

为了运行一些需要很长时间才能完成的代码,我必须通过 ssh 连接到服务器。所以我需要使用nohup 命令。

我使用 nohup 命令启动了多个进程,如下所示:

  nohup julia test.jl > Output1.txt &
  nohup julia test.jl > Output2.txt &
  nohup julia test.jl > Output3.txt &
  nohup julia test.jl > Output4.txt &

问题是我关闭了终端,当我打开另一个终端时 我无法使用 jobs -l 获取进程名称和 ID。

我尝试使用ps -p,但它以相同的答案julia回答了我所有上述过程。

我的问题是“我如何指定哪个进程是哪个?”请注意,这些过程中只有输出文件名不同。

“我怎样才能防止以后出现这样的问题?”

感谢您的时间和回答。

【问题讨论】:

    标签: linux process terminal nohup


    【解决方案1】:

    区分这些进程的一种方法是通过stdout 重定向,而使用ps 命令没有很好的方法。

    如果您安装了pgrep,您可以将其与简单的for loop 一起使用,以了解哪个pid 对应于哪个输出文件。类似于以下内容,

    for pid in $(pgrep julia); 
    do 
       echo -n "$pid: "; 
       readlink -f /proc/${pid}/fd/1; 
    done
    

    /proc/${pid}/fd/1 表示带有pid 的进程的stdout。这是一个符号链接,所以你需要使用readlink来查看来源。

    输出:

    12345: /path/to/output1.txt
    12349: /path/to/output2.txt
    12350: /path/to/output3.txt
    

    另一种方法是使用lsof -p $pid,但我发现它比你想要实现的有点重,但输出是一样的。

    for pid in $(pgrep julia); 
    do 
       lsof -p $pid | awk -v var=$pid '/1w/ {print var": "$9}'; 
    done
    

    【讨论】:

      【解决方案2】:

      要查找此类进程的PID,可以使用fuser

      $ fuser /path/to/outputfile
      

      lsof

      $ lsof | grep "outputfile"
      

      为了避免以后出现这种情况,请在服务器上使用 GNU Screen (https://linode.com/docs/networking/ssh/using-gnu-screen-to-manage-persistent-terminal-sessions/)。

      【讨论】:

      • 谢谢。我现在正在使用屏幕,它似乎是防止此类事件的完美解决方案。感谢您的帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-16
      • 2012-02-05
      • 2015-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多