Tutorial

Nohup Command in Linux

Published on August 3, 2022
author

Pankaj

Nohup Command in Linux

When exiting the shell of a Linux System, all running processes are usually terminated or hang up. So what do you do If you still want to keep the processes running even exiting the shell/terminal? This is where the nohup command comes in.

Nohup Command

Nohup, short for no hang up is a command in Linux systems that keep processes running even after exiting the shell or terminal. Nohup prevents the processes or jobs from receiving the SIGHUP (Signal Hang UP) signal. This is a signal that is sent to a process upon closing or exiting the terminal. In this guide, we take a look at the nohup command and demonstrate how it can be used.

Nohup Command Syntax

Nohup command syntax is as follows;

nohup command arguments

OR

nohup options

Let’s see how the command comes into play

Checking the version of Nohup

You can begin by checking the version of Nohup using the syntax below

nohup --version

Output check version of nohup command

Starting a process using Nohup

If you want to keep your processes/jobs running, precede the command with nohup as shown below. The jobs will still continue running in the shell and will not get killed upon exiting the shell or terminal.

nohup ./hello.sh 

Output Nohup command with regular commands From the output above, the output of the command has been saved to nohup.out to verify this run,

cat nohup.out

Output Cat Nohup Out file Additionally, you can opt to redirect the output to a different file as shown

nohup ./hello.sh > output.txt

Once again, to view the file run

cat output.txt

Output Redirect Nohup Output To A text File To redirect to a file and to standard error and output use the > filename 2>&1 attribute as shown

nohup ./hello.sh > myoutput.txt >2&1 

Output Redirect to  Standard Out And Standard Error

Starting a process in the background using Nohup

To start a process in the background use the & symbol at the end of the command. In this example, we are pinging google.com and sending it to the background.

nohup ping google.com &

Output Nohup Run Process In The Background To check the process when resuming the shell use the pgrep command as shown

pgrep -a ping

Output Pgrep Ping Google If you want to stop or kill the running process, use the killcommand followed by the process ID as shown

kill 2565

Output Kill  PID

Summary

  1. All processes that are run using the nohup command will ignore the SIGHUP signal even upon exiting the shell.
  2. Once a job is started or executed using the nohup command, stdin will not be available to the user.
  3. By default, the nohup.out is used as the default file for stdout and stderr.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
September 5, 2021

Hi. Thanks for the nice tips. I am a newbie in this field and I have a simple question. well, I am using nohup in my work in the background. well. I am facing a problem which is. once I submit the job it working well in the background. no issue. but I have 50+ commands to run. so it is creating a long queue. now the problem is that each job take some time to finish and because of that a long-waited queue got cancel automatically. is there any way to keep it in the queue or prolonged the queuing time or another way by which PID keep activate itself until its turn come for execution? love to hear from your expertise. Thanks

- Malik Waqar Arshad

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    July 18, 2020

    How do I use nohup to run script. Ànd how can I see/monitor the logs

    - Murugan

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      July 1, 2020

      great for tcpdump : nohup sudo tcpdump -i any -nn -w /var/tmp/file.pcap -C 100 -W 50 “src x.x.x.x and dst y.y.y.y” & •to find the process that u created : - pgrep -a tcpdump - ps aux | grep then : sudo kill

      - Edgar Lipnitsky

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        June 13, 2020

        When you run this command normally, does it produce any output as you are redirecting the output in a file.

        - Hemant Ganwgar

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          April 2, 2020

          how do i run nohup for this type of cmd cat out.csv | cut -d “,” -f3,4 | sed ‘s/,/\t/g’ | sort -n -k 2 > out.txt nohup cat out.csv | cut -d “,” -f3,4 | sed ‘s/,/\t/g’ | sort -n -k 2 > out.txt & - doesn’t work

          - dinesh

            Try DigitalOcean for free

            Click below to sign up and get $200 of credit to try our products over 60 days!

            Sign up

            Join the Tech Talk
            Success! Thank you! Please check your email for further details.

            Please complete your information!

            Featured on Community

            Get our biweekly newsletter

            Sign up for Infrastructure as a Newsletter.

            Hollie's Hub for Good

            Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

            Become a contributor

            Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

            Welcome to the developer cloud

            DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

            Learn more