As an experienced CMS website operation personnel for information security, I know that a stable and controllable system operation environment is crucial for content publishing and website optimization.An efficient content management system developed based on the Go language, every link in its deployment process is worth our in-depth understanding.start.shIn the startup script,nohup ... 2>&1 &This command is a key component to ensure the stable operation of the system. It cleverly handles the lifecycle of the program and the output stream, providing great convenience for the daily operation and maintenance of our website.
start.shThe core startup command parsing in the script
In AnQiCMSstart.shIn the startup script, we often see code snippets like this:nohup $BINPATH/$BINNAME >> $BINPATH/running.log 2>&1 &.This is not just a simple startup command, it includes multiple important shell operators, which together ensure the robust operation of AnQiCMS service and the effective management of its output.Understanding the meaning of these operators is crucial for our website operations personnel to carry out daily fault diagnosis and performance monitoring.
Firstly,nohupThe command (no hang up) is located at the beginning of the instruction, its function is to make our AnQiCMS program continue to run in the background after it starts, even if we close the terminal session (such as SSH connection) that started it.For a website that needs to provide 7x24 hour uninterrupted service, this is crucial.It ensures that AnQiCMS service will not be unexpectedly terminated due to the offline status of the operator, thus maintaining the continuous availability of the website.
Following immediately.$BINPATH/$BINNAMEThis represents the complete path and name of the AnQiCMS executable file, which is the core application we hope to keep running.This command is followed by a subtle handling of the program output stream.
Integration and recording of standard output and error output
The core output stream processing logic is reflected in>> $BINPATH/running.log 2>&1this part. In Linux systems, each running program has several default file descriptors:
0Represents standard input (stdin).1Represents standard output (stdout), which is usually the log information, prompt information, etc. of a program running normally.2Represents standard error (stderr), used to output error and warning information generated during program execution.
Here are the>> $BINPATH/running.logFirstly, redirect the standard output (stdout, file descriptor 1) to$BINPATH/running.logthis file.>>The meaning of the operator is 'append redirection', which means that each time the program starts or content is written to the file, it will be appended to the end of the file instead of overwriting the existing content.This is very beneficial for retaining historical run records for subsequent analysis and problem tracing.
while2>&1The standard error (stderr, file descriptor 2) is redirected to the position where the standard output (stdout, file descriptor 1) is currently pointing. Because the standard output has been redirected torunning.logFile, therefore, the final effect of this command is: whether it is the log information normally printed by the AnQiCMS application or any errors or warnings encountered during runtime, they will all be written torunning.logThis is in the log file.This integration method greatly simplifies our monitoring and troubleshooting process for the AnQiCMS running status, as all important runtime information is concentrated in one place.
最后,命令末尾的&符号告诉操作系统将整个nohup命令(包括AnQiCMS程序的执行及其输出重定向)放到后台执行。这意味着,在执行start.shAfter the script, the terminal will immediately return to the command prompt, and we can continue to perform other operations in the current terminal, while the AnQiCMS service is quietly providing services for the website in the background.
The importance from the perspective of operations.
From the perspective of website operation,nohup ... 2>&1 &This combination method provides multiple guarantees. First,nohupand&The combination ensures the high availability and ease of use of AnQiCMS service, we do not have to worry about service interruption due to terminal closure, nor do we need to occupy a terminal window for a long time.2>&1 >> running.logThe log management strategy is the lifeline for daily monitoring and emergency fault handling by operations personnel.It ensures that all running data (including normal business logs and potential error information) is fully recorded, forming a continuous and comprehensive system operation track.When the website encounters access exceptions, functional errors, or performance bottlenecks, this unified log file will be the primary basis for locating problems and analyzing causes.running.logLearn about the behavior of AnQiCMS at specific time points in the file, quickly find the source of the problem, and thus minimize the downtime of the website and its impact on user experience.Therefore, a deep understanding and good use of this command are essential skills for every AnQiCMS operator.
Frequently Asked Questions
Q1: If instart.shthe script is omitted2>&1What will happen?
Answer: If omitted2>&1,nohup $BINPATH/$BINNAME >> $BINPATH/running.log &the command will only redirect the standard output (STDOUT) torunning.logFile. While the standard error (STDERR) will be output tonohup.outfile (this isnohupThe default behavior of the command, if no other redirection is specified). This means that if any errors or warnings occur during the operation of AnQiCMS, these messages will not appearrunning.login, but it is recorded in another independent filenohup.out. This will bring inconvenience to fault diagnosis because it is necessary to check two log files simultaneously to obtain complete running information.
Q2: Why log redirection is used>>Instead>?
Answer: Use>>It is to 'append' log content torunning.logThe end of the file. This means that each time AnQiCMS starts or writes to the log, new content will be added to the end of the existing content in the file, preserving the historical records. If using>(Override redirection), each time AnQiCMS starts,running.logThe original content of the file will be cleared and overwritten, which will result in the loss of all old runtime logs and historical fault information, seriously hindering the ability to perform long-term monitoring and issue tracing.
Q3: How to viewrunning.logfile to monitor the running status of AnQiCMS?
Answer: You can use various Linux/Unix commands to viewrunning.logFile. The most commonly used methods include:
tail -f $BINPATH/running.log: English tracking the latest content at the end of the file. This is the most effective way to monitor the output of running services, as it continuously displays newly generated log lines.cat $BINPATH/running.log: Show the entire content of the file. Suitable when the file is not too large and all historical records need to be viewed.less $BINPATH/running.log: Page through file content, supporting scrolling up/down, search, and other functions. Suitable for large files, it allows for convenient browsing. Please$BINPATH/running.logReplace with the actual path of your AnQiCMS log file.