nohup: command not found

The nohup (“no hangup”) command prevents a process from ending when the user logs off. For example, if an administrator launches a backup script, and then logs off the system, the script would stop running. By placing the nohup command in front of the normal command, the script would continue even after the administrator logged off.

Syntax

The syntax of the nohup command is:

# nohup {command/script}

If you encounter the below error while running the nohup command:

nohup: command not found

you may try installing the below package as per your choice of distribution:

Distribution Command
OS X brew install coreutils
Debian apt-get install coreutils
Ubuntu apt-get install coreutils
Alpine apk add coreutils
Arch Linux pacman -S coreutils
Kali Linux apt-get install coreutils
CentOS yum install coreutils
Fedora dnf install coreutils
Raspbian apt-get install coreutils

Examples

1. Running nohup without specifying an output file

Let’s do an example and test that it actually does continue working after you have logged out. We’ll kick off a script called script.sh. The contect of the script is as follows:

# cat script.sh
#!/bin/sh
while(true)
do
 echo "Hello World!"
 sleep 10
done

Lets run the script in backgroupd with nohup command:

# nohup script.sh & 
[1] 179 
$ nohup: appending output to `nohup.out`

As you can see above we have not specified an output file. Thus the standard output and standard error by default goes into the nohup.out file in the directory from where the script has been invoked. Also the number 179 Specified the process ID(PID) of the job. This PID can be verified in the “ps -ef” command output as shown below.

Now exit the shell, log back in again, and run the following command:

$ ps x | grep script.sh
179  ?   S N  0:01 sh /root/script.sh 
506  p2 S     0:00 grep script.sh

The script is still working. Use “ps -ef | grep ps1” if your system does not support ps x. You can also check the output file nohup.out for more confirmation.

# cat nohup.out
Hello World!
Hello World!
Hello World!
Hello World!
...
NOTE: If you run another command using nohup, the output is appended to the existing nohup.out file. Be careful when running multiple commands from the same directory, because all the output is sent to the same nohup.out file, which can get confusing.

Running nohup specifying an output file

Now the above script can also be run nohup command and specifying a custom output file as shown below:

# nohup script.sh > /tmp/script.out 2>&1
[1] 14235

Conclusion

If you are running a process and you don’t think it will be completed by the time you log out for the day, use the nohup command. Nohup will continue processing when you exit your account. Nohup means no hang up.

Related Post