How to resolve the issue of nohup not working after closing the terminal in Linux?
In Linux, to prevent a task from terminating when closing the terminal after executing it with the nohup command, the following solution can be taken:
- To use the screen command: screen is a terminal multiplexing tool that allows you to create a virtual terminal session and run it in the background. First, install screen. If you are using the apt package manager, you can use the following command: sudo apt-get install screen. Once installed, use the screen command to create a new session: screen, and then execute your tasks in that session. After your tasks are completed, you can use the Ctrl+a d command to suspend the session to the background. When you close the terminal, log back into the system, and use the screen -r command to resume the session, your tasks will continue to run.
- When using the nohup command, you can redirect the output to a file at the same time. For example, you can run “nohup command > output.txt &” to save the output of the task to the output.txt file. This way, the task will continue to run even after closing the terminal, and the output will be saved in the file for later viewing.
- By using the disown command, you can detach a task from the current terminal session after executing the nohup command, ensuring that it will not be affected by closing the terminal. To do this, first use the jobs command to view the task list in the current terminal session, then use disown %jobnumber to detach the task from the session. For example: disown %1. This way, the task will continue to run in the background without being affected by closing the terminal.
Here are several ways to solve the problem of tasks becoming inactive when using the “nohup” command in Linux. Choose the most suitable method based on your actual situation.