• Linux
  • FreeBSD
  • Networking
  • Python
  • AWS
  • WebDev
  • About Us

How to use nohup to run a command in the background

Written by
Linux Leave a Comment

nohup stands for no hangup is a commandline utility that ignores the HUP signals coming to the commands running on a shell. So when a shell is terminated , the processes associated with that shell recieves a hup signal . So that those processes gets stopped or hangup . So the nohup helps us to run the command even after we logout or disconnects from the shell . This is a very useful utility for crtitical processes running on the system that takes long time , so we don’t have to wait until it gets finished .

Basic nohup syntax is :

nohup command arguments
nohup command

By default , all the outputs and error messages associated with nohup will be appended to a file called nohup.out and it will be in the current directory or in the home directory.

To redirect the output to a specific file :

nohup command > commandout.txt &

To start a process in the background:

nohup command &

The ‘&’ command wiill instruct the shell to run the command in the background . The ‘fg’ command will bring it back to the forefront .

If you want to ignore all the outputs and error messages associated with the process, you can use the /dev/null

nohup command > /dev/null 2>&1 &

The /dev/null is a null device , which is a special device that discards all the informations coming to it . The 2>&1 is to redirect the ‘stderr’ and ‘stdout’ to the same place which is /dev/null in the above command . So the above command runs in the background and discards all the output and errors associated with it .

So if we run a nohup command like this , the process ID/PID for it is 3772. So the next time when you login back and want to stop this command , you can use kill -9 process id . If you forget the process id you can grep it again as below :

kill -9 3772

© Copyright 2020.TechieNix. All Rights Reserved.