Tuesday, September 18, 2012


When a UNIX process ends it also terminates all the child process that started under the parent PID (PPID). A process tree command on UNIX terminal can show you entire tree structure of PID and  PPID.
#pstree -A -p pid

However today what I want to discuss here is a common problem we face while working on a long running process and want to leave the terminal/ or log out the server  - still keeping the process intact when I am back to the console.

For ex. I started a long running SCP command and when I am back after couple of hrs. it should still be running.  Ofcourse we can use screen utility  for this - BUT I wanted to explore the 'disown` UNIX command for this task.

The How:

[root@DebaTestBox ~]# scp ABigFile.bak  user@remoteServer:/destination/location/

ABigFile.bak                                 12% 2864KB   12.8MB/s 3:56:23 ETA

The ETA is ~4hrs still.

[root@DebaTestBox ~]# bg+1
[root@DebaTestBox ~]# disown %1

Done!  Now you can safely exit your terminal and return back later to check the progress. The job will still be running uninterrupted. So now - disown tells the shell to forget that the specified job is a child process of the shell, meaning that the shell won't kill it on exit.

What actually happened:

When you ran disown %1 (i.e. first background) job - the pid  31614 is nolonger attached to your shell now.

[root@DebaTestBox ~]# ps -ef | grep scp
10001    31614 28489  0 07:35 pts/1    00:00:00 scp ABigFile.bak  user@remoteServer:/destination/location/
10001    31615 31614  2 07:35 pts/1    00:00:00 /usr/bin/ssh -x -oForwardAgent no -oPermitLocalCommand no -oClearAllForwardings yes user@remoteServer scp -t /destination/location/
10001    31618 28489  0 07:35 pts/1    00:00:00 grep scp

Where 31615 is pid of BASH shell. See below

[root@DebaTestBox ~]# pstree -A -p 31614
So had to logged out of the Shell - even your SCP process would have died. 

[root@DebaTestBox ~]#

Now see the output after I disowned and when logged back in after some hrs. 

[root@DebaTestBox ~]# ps -ef | grep scp
10001    31614     1  0 07:35 ?        00:00:00 scp ABigFile.bak  user@remoteServer:/destination/location/

The SCP process is now part of PID 1

Important Point: * disown will NOT actually move the process in the pstree immediately. Movement will happen when you close the shell.

Below are some important switches you can use with  disown command -
  • -a -  This will disown all the jobs  (here you don't have to specify a job number e.g. disown %1)
  • -h - make disown behave more like nohup (i.e. the jobs will stay in your current shell's process tree until you exit your shell) This allows you to see all the jobs that this shell started.
  • -r - only disown running jobs. 
Hope it helps.


1 comment:

  1. Win Exciting and Cool Prizes Everyday @ www.2vin.com, Everyone can win by answering simple questions.Earn points for referring your friends and exchange your points for cool gifts.


Why Database CI/CD?

Making the Database Part of Your Continuous Delivery Pipeline The database, unlike other software components and code or compiled co...