redirect nohup output to file

How to Redirect Nohup Output to File

By default, all Linux processes run on terminal foreground. But when you close terminal and exit session all such processes are killed by the shell. Sometimes you may need to run certain processes and tasks in background in Linux. Nohup (for No Hangup) is the command to run a process and ignore kill signal sent by user or terminal, whether it is a command, software or script. Once you run a process using Nohup it will keep the process running even if you close terminal or end session. So sometimes you may need to view output & error messages of nohup processes. In such cases, you can redirect nohup output to file. In this article, we will look at how to redirect nohup output to file.


How to Redirect Nohup Output to File

Here is the basic syntax for nohup command.

nohup [command] [ARGS]

Here is an example to run a script in bakground

$ nohup /home/ubuntu/test.sh
nohup: ignoring input and appending output to nohup.out

Please note, in above example, our script will continue to run in foreground, even if you close the terminal or end the session. If you want to run it in background you need to add ampersand ‘&’ operator after the command.

$ nohup /home/ubuntu/test.sh &

By default, nohup command sends output of commands to nohup.out file in your present working directory.. You can view its content in any file editor.

$ sudo cat nohup.out

If you want to redirect the output of nohup command to file, just add it at the end of command after > redirection operator. Here is an example.

$ sudo nohup /home/ubuntu/test.sh > /home/ubuntu/data.txt
nohup: ignoring input and redirecting stderr to stdout

In this case, the output of shell script will be sent to /home/ubuntu/data.txt file.

If you want to send a nohup process to background but also redirect its output to file, you need to add ampersand only after the filename where you are sending nohup output, as shown below.

$ sudo nohup /home/ubuntu/test.sh > /home/ubuntu/data.txt &

If you also want to send the standard output and error to the file add file descriptors 1 and 2 as shown below.

$ sudo nohup /home/ubuntu/test.sh > /home/ubuntu/data.txt 2>&1 &

In the above example, the shell script will be sent to run in background, its output, standard output and standard message will all be send to /home/ubuntu/data.txt.

If you don’t want any nohup output to be captured, then just redirect everything to /dev/null.

$ sudo nohup /home/ubuntu/test.sh > /dev/null 2>&1 &

In this article, we have looked at how to redirect nohup output to file. Please note, nohup command only continues to run the process even after you close the terminal or session. It does not send the process to background. For that, you need to add ampersand operator at the end of command.

Also read:

How to Install Dependencies with APT
How to Install DPKG Dependencies in Linux
How to Disable Unnecessary Services in Linux
Top SFTP Command Examples
How to Use Journalctl command in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *