2

I use nohup command to run shell scripts as the following:

nohup ./run.sh &

My question is: After logging out the terminal and coming back later, how can I retrieve the information about this process if it is finished or still running?

2 Answers 2

0

In general you can examine the output of ps x. Either there is run.sh there or not. If there is, it may be the process you seek or some other run.sh. Further investigation may be required.

There is however this trick. Note nohup prints a message like this:

 nohup: ignoring input and appending output to /home/alli/nohup.out

Now invoke:

lsof /home/alli/nohup.out

If your run.sh invoked with nohup is still running, it should appear in the output of lsof (twice, because nohup redirects file descriptor 1 and 2 to nohup.out). If you used nohup more than once, lsof should return multiple processes. If you used nohup more than once with the same executable, you may need to check /proc/$pid/ (example) to tell them apart.

I would use tmux in the first place. In fact I do use it on a daily basis.

0
1

You can store the value of the nohupped process in a file using $! right after you started the process with

nohup the_process &

Then when you come back read the PID and check if the process is running. If yes, check if it's the kind of process you want. (The PID might have been reassigned to another process.)

If it doesn't disrupt the process's functioning, you can also pass the process a fake argument that will act as a hint for you when you read the output of ps. Eg.:

$ cat p.bash 
#!/bin/bash
sleep 10
$ nohup ./p.bash some_id_hint &
[1] 14139
nohup: ignoring input and appending output to 'nohup.out'
$ echo $!
14139
$ ps 14139
  PID TTY      STAT   TIME COMMAND
14139 pts/1    S      0:00 /bin/bash ./p.bash some_id_hint

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .