AlirezaGH
AlirezaGH

Reputation: 399

how to fined a process started with nohup after closing terminal?

I have to ssh to a server for running some codes that take a long time to finish. so I need to use nohup command.

I started multiple processes using the nohup command like this:

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

the problem is that I closed the terminal and when I opened another terminal I couldn't get the process name and ID using jobs -l.

I tried using ps -p but it answers me for all of the above processes with the same answer julia.

my question is " how can I specify which process is which?" note that only Output file name is different in these processes.

and

"how can I prevent such a problem in the future?"

Thanks for your time and answer.

Upvotes: 2

Views: 720

Answers (2)

iamauser
iamauser

Reputation: 11469

One way to distinguish between these processes are through there stdout redirections and there is no good way of doing that using ps command.

If you have pgrep installed, you can use that with a simple for loop to know which pid correspond to which output file. Something like the following,

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

/proc/${pid}/fd/1 represents the stdout for the process with pid. It's a symlink, so you need to use readlink to check the source.

Output:

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

Alternative way would be to use lsof -p $pid, but I find it a bit on the heavy side than what you want to achieve, but the output would be same.

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

Upvotes: 2

pgngp
pgngp

Reputation: 1562

To find the PIDs of such processes, you can use fuser

$ fuser /path/to/outputfile

or lsof

$ lsof | grep "outputfile"

In order to avoid such a situation in future, use GNU Screen on the server (https://linode.com/docs/networking/ssh/using-gnu-screen-to-manage-persistent-terminal-sessions/).

Upvotes: 1

Related Questions