Linux: View and kill disowned process

5

2

I have started a program in Linux using & and disown.
I wish to see if it is still running and possibly to kill it.

I started the process with commands like these:

(env)bash-4.2$ python manage.py update_rollups &

[1] 29144

(env)bash-4.2$ disown

I can no longer find it with ps aux, nor kill it based on the pid 29144.
I want to know if the process is still running (under init?).

I hope you can help!

user715198

Posted 2017-04-07T10:26:40.340

Reputation: 53

Welcome on Super User. Give feedback to the answerers, remember that you can [edit] your own question, upvote, downvote the answers and choose one of them if you think it solves your question. – Hastur – 2017-04-07T11:57:19.980

Answers

5

In brief

With & and disown you do not change the PID [1] of the process.
If you do not see it in the ps -p <YOURPID> output, it is not any more running.
You can over-check it with an additional echo $? [2] after the ps (or kill) command, checking if the program exits with an exit code different from 0 (typically 1).

Understanding your commands.

  • Background: when you launch the command with the final & you send it in background.
    This means that:

    • It is present in the job list of your shell (in your example is the number [1] and you can refer to it as %1; (try the command jobs).
    • You can bring it in foreground and in background with fg and bg.
    • It is (still) "owned" by the (linked to the parent) shell: if the shell receives a SIGHUP signal, it will send a SIGHUP signal to the process too.

      $ sleep 1h &
      [1] 10795
      $ jobs
      [1]+  running           sleep 1h & 
      $ ps -l -p 10795       
      F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
      0 S  1000 10795  8380  0  80   0 -  3107 hrtime pts/57   00:00:00 sleep
      
  • Disown: with the command disown you remove the job from the shell's job list, but you do not change its PID.

    $ disown
    $ jobs
          # <---- No jobs
    $ ps -l -p 10795
    F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
    0 S  1000 10795  8380  0  80   0 -  3107 hrtime pts/57   00:00:00 
    

    Note the same PPID (the shell still exists).
    Now we kill the shell.

    $ kill 8380   # Here we kill the shell 
    $ ps -l -p 10795
    F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
    0 S  1000 10795  5339  0  80   0 -  3107 hrtime pts/57   00:00:00 sleep
    

    There is another PPID, the 5339, that with another invocation of ps, ps -p 5339, you will discover to be an init instance:

    $ ps -p 5339
    PID TTY          TIME CMD
    5339 ?        00:02:20 init
    

pstree: a quicker way.

You can see with pstree more quickly.

Before the disown and kill the bash commands:

$ pstree -s -p 10795 
init(1)───lightdm(1199)───lightdm(5259)───bash(8380)───sleep(10795)

After the disown and kill the bash:

$ pstree -s -p 10795 
init(1)───lightdm(1199)───lightdm(5259)───init(5339)───sleep(10795)

Note: of course all the PIDs in your case will be different...

Hastur

Posted 2017-04-07T10:26:40.340

Reputation: 15 043

extremely thorough answer and exactly what I needed. – jimh – 2019-11-15T22:46:49.243

@jimh Thank you. Glad that you liked it. – Hastur – 2019-11-16T17:10:57.847

3

If it's not in ps auxf, then it's not running. If you run kill 29144 and get "No such process", that also means the process is not running.

user1686

Posted 2017-04-07T10:26:40.340

Reputation: 283 655

Hmm, I don't see the process with ps auxf, or could it be hidden in one of these?

WEB0117 32409 0.3 0.2 583004 39360 ? Sl 12:49 0:01 _ WEB0117 -k start

(WEB0117 is the user Im running as) – user715198 – 2017-04-07T11:01:35.610

2

Each process has a folder in the /proc filesystem with it's pid. If the folder doesn't exist, the process isn't running.

For example

/proc/29144/

you can view the process commandline

cat /proc/29144/cmdline

example output:

/usr/sbin/smbd

or check the process file status

stat /proc/29144/exe

example output:

File: /proc/29144/exe -> /usr/sbin/smbd
  Size: 0               Blocks: 0          IO Block: 1024   symbolic link
Device: 3h/3d   Inode: 78497       Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2017-04-07 12:18:01.719011505 +0200
Modify: 2017-04-07 12:18:01.369010535 +0200
Change: 2017-04-07 12:18:01.369010535 +0200
 Birth: -

Michael D.

Posted 2017-04-07T10:26:40.340

Reputation: 698