Run job in background

Ho to make commands keep running after leaving a terminal session or ssh session?

Most of the time you login into remote server via ssh. If you start a shell script or command and you exit (abort remote connection), the process / command will get killed. This is because Linux Operating System, by default will end the process if you exit your session. Sometime the  job or command takes a long time ,  it is not secure to let your session open while you are not there, or maybe not possible to let it open if you are over a ssh connection, so you may want to use nohup command to let the process running even when you exit your session or terminal window.

If you are not sure when the job will finish, then it is better to leave job running in background. However, if you logout the system, the job will be stopped. What do you do?

  1. use the “screen” command
  2. use the  “nohup” command
  3. use the “disown” command
  • The “screen” utility was outlined on a previous article. Just open a shell terminal and type screen  -ls . You get the name(s) of the screen session-terminal(s) where a process was set to be run in back-ground (bg) . Now use “screen -R session-terminal name ” and BINGO , you can manipulate the process if it is still running (not finished his job)
  • The “nohup” utility is an  ” out of the box” utility , just run any command but in front set the keyword “nohup“  example :  nohup  cat /dev/zero > /dev/null  &

Be sure to add the & sign so the command is executed in background. Now even if the terminal is closed, the process / command will continue to run. Just open a new terminal and run:
ps -aux |grep nameof-command (ps aux |grep cat ) and BINGO you get the PID of the process.

  • The “disown” command has the same functionality but is implemented differently.
    Just enable a process to run in the background with the & . The terminal will assign a job-ID to this process, let’s say 5065 . Now use disown 5065 , and the process will be inherited by the init process , so even if the terminal is closed the process will keep running

How does disown and nohup commands work? What they do is that they connect all your process to the parent process of the computer which happens to be init (the granddaddy of all process running on the computer). Viewing all the process on the computer help us understand it better, look a the output of the pstree command, the init is first (root of the inverted tree) process which radiates all the other process in the computer. Now if you run the top command you will see all the processes that were manipulated with disown and nohup with a PPID of 1 (init) .

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s