This tutorial will teach us about the Nohup command in Linux. The Nohup is short for no hang-up and is a Linux command that keeps processes running without an active shell or terminal. This means that if we quit or close the terminal window, the process will still be active in the background thanks to this command. If we do not have service for some specific process not using the nohup will terminate the process after every terminal closing.
In the next paragraphs, we will show you how to use the nohup command with practical examples. Let’s get started!
Prerequisites
- A server with Ubuntu 24.04 or any Linux OS
- User privileges: root or non-root user with sudo privileges
Update the System
We will use Ubuntu 24.04 OS in this blog post, but you can choose any Linux distro. It is recommended that the system packages be updated to their latest versions before executing any Linux command. To do that you can execute the following:
sudo apt update -y && sudo apt upgrade -y
Check Nohup command version
To check the nohup version execute the following command:
nohup --version
You will receive output similar to this:
root@host:~# nohup --version nohup (GNU coreutils) 9.4 Copyright (C) 2023 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later . This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Jim Meyering.
Nohup Syntax
The syntax of the nohup command is the following:
nohup command [options] &
command: is the command or script that we want to execute.
options: are the arguments or flags responsible for the command’s behavior.
&: Is the ampersand on the end of the command that instructs the shell to run the command in the background.
Nohup command with examples
We will show you some real examples of the nohup command, such as data backups, script executions, server log analysis, and database backup commands.
Download Huge File: We want to download a massive file from the Internet but cannot supervise the process. To do that, we can execute the command below:
nohup wget http://website.com/hugegamefile.iso &
Once you execute this, the file will be downloaded in the background.
MySQL Database backup: If we have to make a database dump but the database size is massive, let’s say 100GBs, then we can execute the following command:
nohup mysqldump -u root -p > dump.sql &
Script Execution: To execute a bash script with nohup command, you can use the following one:
nohup bash hello.sh &
You should get output similar to this:
root@host:~# nohup bash hello.sh & [1] 138571 root@host:~# nohup: ignoring input and appending output to 'nohup.out'
As you can see, the script’s execution started in process 138571, and the script’s output will be written to a nohup.out file.
root@host:~# cat nohup.out Hello World
Nohup full explanation
If you want to know more about the nohup command, you can execute this in your Linux terminal:
man nohup
You will get the output with a full description:
NOHUP(1) User Commands NOHUP(1) NAME nohup - run a command immune to hangups, with output to a non-tty SYNOPSIS nohup COMMAND [ARG]... nohup OPTION DESCRIPTION Run COMMAND, ignoring hangup signals. --help display this help and exit --version output version information and exit If standard input is a terminal, redirect it from an unreadable file. If standard output is a terminal, append output to 'nohup.out' if possible, '$HOME/nohup.out' other‐ wise. If standard error is a terminal, redirect it to standard output. To save output to FILE, use 'nohup COMMAND > FILE'. NOTE: your shell may have its own version of nohup, which usually supersedes the version described here. Please refer to your shell's documentation for details about the options it supports.
That’s it! You have learned some basic nohup commands in Linux with examples.
If you have difficulties with the nohup command, our Linux admins will help you with any aspect of it. All you have to do is sign up for one of our monthly management or per-incident server support plans. Do not hesitate to contact us anytime. We are available 24/7.
If you liked this post, please share it with your friends or leave a reply below. Thanks.