Linux: View and kill disowned process

bashlinuxprocess

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!

Best Answer

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...

Related Question