111

I am accessing a server running CentOS (linux distribution) with an SSH connection. Since I can't always stay logged in, I use "nohup [command] &" to run my programs.

I couldn't find how to get a list of all the programs I started using nohup. "jobs" only works out before I log out. After that, if I log back again, the jobs command shows me nothing, but I can see in my log files that my programs are still running.

Is there a way to get a list of all the programs that I started using "nohup" ?

1
  • Found an excellent article that can help with the question: cyberciti.biz/faq/…. Most importantly, it's helpful if you can interpret the output of the ps command, especially the stat column. If you don't, go read the article NOW ;-) Commented Apr 1, 2020 at 7:41

7 Answers 7

159

When I started with $ nohup storm dev-zookeper ,

METHOD1 : using jobs,

prayagupd@prayagupd:/home/vmfest# jobs -l
[1]+ 11129 Running                 nohup ~/bin/storm/bin/storm dev-zookeeper &

NOTE: jobs shows nohup processes only on the same terminal session where nohup was started. If you close the terminal session or try on new session it won't show the nohup processes. Prefer METHOD2

METHOD2 : using ps command.

$ ps xw
PID  TTY      STAT   TIME COMMAND
1031 tty1     Ss+    0:00 /sbin/getty -8 38400 tty1
10582 ?        S      0:01 [kworker/0:0]
10826 ?        Sl     0:18 java -server -Dstorm.options= -Dstorm.home=/root/bin/storm -Djava.library.path=/usr/local/lib:/opt/local/lib:/usr/lib -Dsto
10853 ?        Ss     0:00 sshd: vmfest [priv] 

TTY column with ? => nohup running programs.

Description

  • TTY column = the terminal associated with the process
  • STAT column = state of a process
    • S = interruptible sleep (waiting for an event to complete)
    • l = is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)

Reference

$ man ps # then search /PROCESS STATE CODES

3
  • 10
    Method 2 does not list the programs that were started with nohup. It lists the processes that are not attached to a TTY, which is a completely different thing: processes started with nohup remain attached to their parent TTY until it is closed; processes that are not attached to a TTY were not necessarily started with nohup. That is, method 2 lists, among other processes, some of the programs running with nohup.
    – Arcturus B
    Commented Jun 25, 2018 at 12:22
  • 7
    jobs doesn't work for me, only works for a same ssh session, if I disconnect and connect again it no longer lists the previously listed running nohup process
    – Mojimi
    Commented Feb 15, 2019 at 11:05
  • 3
    $ ps xw work for me. Commented Nov 9, 2021 at 6:47
40

Instead of nohup, you should use screen. It achieves the same result - your commands are running "detached". However, you can resume screen sessions and get back into their "hidden" terminal and see recent progress inside that terminal.

screen has a lot of options. Most often I use these:

To start first screen session or to take over of most recent detached one:

screen -Rd 

To detach from current session: Ctrl+ACtrl+D

You can also start multiple screens - read the docs.

3
  • 8
    Seems to be a good alternative to me. I also found out that i could find the PIDs with "ps auwx" Commented Jun 6, 2013 at 12:46
  • 13
    Question clearly asks Is there a way to get a list of all the programs that I started using "nohup"?, yet this answer doesn't even slightly address the question. Why is it accepted? Commented Dec 23, 2015 at 10:09
  • 6
    Because back in 2013, it was the only answer that helped. Commented Feb 8, 2016 at 7:26
33

If you have standart output redirect to "nohup.out" just see who use this file

lsof | grep nohup.out
4
  • 2
    That is a smart way. But in my case this returns no results. Although I'm sure that the nohup command is still runing, and it's using the nohup.out file.
    – Julius Š.
    Commented Jun 7, 2017 at 8:47
  • 1
    It's ok in all TTY, but show double process for each nohup! Commented Aug 22, 2018 at 16:18
  • @NabiK.A.Z. from man lsof: "In general threads and tasks inherit the files of the caller, but may close some and open others, so lsof always reports all the open files of threads and tasks." I couldn't find information on nohup in its man, but it seems it uses two threads, not one. Commented Jun 12, 2019 at 9:56
  • 1
    Won't work if the output is redirected to some other files Commented May 13, 2021 at 9:40
10

You cannot exactly get a list of commands started with nohup but you can see them along with your other processes by using the command ps x. Commands started with nohup will have a question mark in the TTY column.

1
  • 4
    "ps x" didn't help, but I found out that "ps auxw" does. Commented May 30, 2013 at 12:03
10

You can also just use the top command and your user ID will indicate the jobs running and the their times.

$ top

(this will show all running jobs)

$ top -U [user ID]

(This will show jobs that are specific for the user ID)

1
  • Additionally, the nice thing about this is that it stays on your console so you can watch the job and know when it finishes
    – rgilligan
    Commented Nov 26, 2019 at 17:12
2
sudo lsof | grep nohup.out | awk '{print $2}' | sort -u | while read i; do ps -o args= $i; done

returns all processes that use the nohup.out file

1

From personal experience, I prefer the top command, see javapoint for more examples how to use top. As user Jcrow06 already suggested, running top will show you all running jobs on the server and if one is only interested in the jobs of a specific [userid], one can simply run top -u [userid].

The benefit of running top is that all running processes are shown (including nohup processes, even when started in the background), also ones that are not from the current terminal session or from a different user. Furthermore, one can see with top how much CPU and memory the task takes and for how long it is already running. Which can be helpful to determine if it is still running correctly.

Note top by default orders tasks on CPU usages, but one can adjust the ordering if needed. Furthermore, if you notice unwanted tasks, you can kill them easily by pressing k followed by the processID PID.

1

Not the answer you're looking for? Browse other questions tagged or ask your own question.