Running scripts in the background is a very practical skill in the background, especially for tasks or services that require long-running. By putting the script in the background to execute, users can continue to use the terminal for other operations without worrying about the script being interrupted due to terminal closing or logout.
Introduction to nohup command
nohup is a command line tool that prevents commands from being suspended due to user logout or terminal shutdown. Its name comes from "no hang up", that is, it does not hang up. In Unix systems, when a user logs out or closes the terminal, a hangup signal (SIGHUP) is sent to the running process, causing the process to terminate. The function of the nohup command is to make the specified command ignore the hangup signal and continue to run.
Basic usage
The basic usage of nohup is very simple, just put it in front of the command you want to run in the background. For example, if you have a script called , want to run it in the background and don't want to interrupt due to terminal shutdown, you can use the following command:
nohup sh &
Here, sh is the script to be run, and the & symbol indicates that the command is put into the background to execute. nohup will ignore the hangup signal to ensure that the script continues to run in the background.
Output redirection
By default, nohup outputs the standard output and standard errors of the command to a file named , located in the current working directory. If you do not want to use the default file, you can specify the output to another file through the redirect operation. For example:
nohup sh > 2>&1 &
- > is a shell command that redirects the output of the command to a file.
- is the specified output file name, and all standard output will be written to this file.
- 2>&1 is a special redirection operation that indicates redirecting standard errors (file descriptor 2) to standard output (file descriptor 1), so that error messages are also written to the file.
- The & symbol puts the entire command in the background to execute.
In this way, you can clearly view the script's running log without losing output information due to terminal shutdown or logout.
The role of the & symbol
The & symbol is used in shell commands to put commands in the background to execute. When adding & at the end of the command, the shell creates a new child process to run the command, and returns to the command prompt, allowing the user to continue typing other commands in the terminal.
Features of background processes
The background process will not block the terminal, and the user can continue to use the terminal for other operations.
The background process can be viewed through the jobs command, brought it back to the foreground using the fg command, or terminated it using the kill command.
If the terminal is closed, the background process will usually receive a hangup signal (SIGHUP), unless the nohup command is used.
Things to note
When using & to put commands in the background for execution, problems may arise if the command depends on some resources of the foreground process (such as standard input). Therefore, when using &, it is usually necessary to properly process the input and output of the command.
The priority of background processes is usually low, and the system will dynamically adjust its operating priority according to the scheduling algorithm to ensure the overall performance of the system.
Practical application scenarios
Long-running tasks
For some tasks that require long-term running, such as data backup, large-scale file processing, complex calculations, etc., the nohup command combined with the & symbol can be used to run related scripts in the background to avoid task interruptions due to terminal shutdown or logout. For example, a data backup script can be run like this:
nohup sh > 2>&1 &
In this way, the backup task will be executed in the background and all output information will be recorded in the file for easier subsequent viewing and analysis.
Service start
When starting some services, they also need to be put in the background to run. For example, the script that starts a web service, start_web.sh, can use the following command:
nohup sh start_web.sh > 2>&1 &
In this way, the web service will be started in the background and all log information will be written to the file to facilitate monitoring of the service's running status.
Timing tasks
For some timing tasks, although usually implemented using cron jobs, in some cases, it can also be done by scripts in conjunction with nohup commands. For example, a script that cleans logs regularly every day can run clean_logs.sh:
nohup sh clean_logs.sh > clean_logs.log 2>&1 &
By putting this command into a script that is executed regularly, you can ensure that the log cleaning task runs automatically in the background and will not be interrupted due to terminal shutdown or logout.
Frequently Asked Questions and Solutions
File permission issues
In some cases, the file generated by nohup by default may not be written due to permission issues. The solution is to create an output file with appropriate permissions in advance and redirect the output to that file. For example:
touch chmod 644 nohup sh > 2>&1 &
The background process was terminated unexpectedly
If the background process is terminated unexpectedly, it may be due to insufficient system resources and the process being killed. The solution is to check the system resource usage to ensure that there are enough resources for the process to run; at the same time, you can view the process status through the ps command, and be cautious when manually terminating the process using the kill command to avoid accidentally killing important processes.
The output file is too large
Long-running scripts may produce a large amount of output information, resulting in excessive output files. The solution is to check the output file size regularly and clean or split as needed. You can use the logrotate tool to manage the rotation, compression and deletion of log files, and keep the output files within a reasonable size range.
This is the article about running scripts in the background using the nohup command in Linux. For more related content on running scripts in the background of Linux nohup, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!