Nohup Command in Linux

28/12/2020
Chưa phân loại

Bash nohup command tutorial

The meaning of nohup is ‘no hangup‘.  Normally, when we log out from the system then all the running programs or processes are hangup or terminated.  If you want to run any program after log out or exit from Linux operating system then you have to use nohup command. There are many programs that require many hours to complete. We don’t need to log in for long times to complete the task of the command. We can keep these type of programs running in the background by using nohup command and check the output later. Some example of using nohup command are memory check, server restart, synchronization etc. How you can use a nohup command on Ubuntu to run a program in the background is shown in this tutorial.

You can check the version of nohup command by using the following command.

$ nohup   –version

nohup command syntax:

You can use the nohup command by two ways.

  1. nohup command [args…]
  2. nohup option

Using nohup with commands

Example-1: Using nohup command without ‘&’

When you run nohup command without ‘&’ then it returns to shell command prompt immediately after running that particular command in the background. In the following example, nohup run bash command without ‘&’ to execute sleep1.sh file in the background.  The output of the nohup command will write in nohup.out the file if any redirecting filename is not mentioned in nohup command. For the following command, you can check the output of sleep1.sh by checking the output of nohup.out file.

$ nohup bash sleep1.sh
$ cat nohup.out

You can execute the command in the following way to redirect the output to the output.txt file. Check the output of output.txt.

$ nohup bash sleep2.sh > output.txt
$ cat output.txt

Example-2: Using nohup command with ‘&’

 When nohup command use with ‘&’ then it doesn’t return to shell command prompt after running the command in the background. But if you want you can return to shell command prompt by typing ‘fg’

$ nohup bash sleep1.sh &
$ fg

Example-3: Using nohup command to run multiple commands in the background

You can run multiple commands in the background by using nohup command. In the following command, mkdir and ls command are executed in the background by using nohup and bash commands. You can get the output of the commands by checking output.txt file.

$ nohup bash -c ‘mkdir myDir && ls’> output.txt
$ cat output.txt

Example-4: Start any process in the background by using nohup

When any process starts and the user closes the terminal before completing the task of the running process then the process stops normally. If the run the process with nohup then it will able to run the process in the background without any issue. For example, if you run the ping command normally then it will terminate the process when you close the terminal.

$ ping -i 10 google.com

You can check the list of all running command by using pgrep command. Close the terminal. Re-open the terminal and run pgrep command with -a option. No list of running command will display because all running commands are terminated when the terminal was closed.

$ pgrep -a ping

Run ping command with nohup command. Re-open the terminal and run pgrep command again. You will get the list of the process with process id which is running.

$ nohup ping -i 15 google.com &

$ pgrep -a ping

You can stop any background process by running kill command. Just run kill command with particular process id which is running. Here, the process id of the running process is 7015. Run kill command in 7015 to terminate the process.

$ kill 7015

The uses of nohup command are explained by using very simple examples in this tutorial. Hope you will get a clear idea of the function of nohup command and able to apply this command for various purposes.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

[Zabbix] Giám sát Windows Server bằng zabbix-agent

Zabbix là một công cụ monitor đa nền tảng, do vậy việc cài zabbix-agent cho Windows Server là điều không...
30/12/2020

Install Aircrack-ng on Ubuntu

Aircrack-ng is a whole suite of tools for Wireless Security Auditing. It can be used to monitor, test, crack or attack...
29/12/2020

Linux on Lenovo ThinkPad: Product Reviews and How to Guide

Lenovo ThinkPads have always received a lot of love from the Linux community, in part thanks to their excellent Linux compatibility....
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Reliable IPv4 and IPv6 Subnet Rental Services: The Perfect Solution for Global Businesses
23/12/2024

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024