Use While to solve the file waiting Problem

Source: Internet
Author: User

A friend asked me for help a few days ago. He said he encountered a problem in Unix system management. He deploys a database system in Unix systems. Now he hopes to implement remote backup for this database. His solution is to first use the database's own functions for local backup (the database does not have an automatic remote backup function), and then copy the backup file to a remote location. If the backup is correct, that is, back up according to the plan, such as backing up at 12 o'clock every night, this is a good solution. You only need to use the CRON Task Scheduler function provided by the Unix system. But now the problem is that sometimes this backup is uncertain. For example, the database administrator sometimes needs to perform temporary backup, so he hopes that the temporary backup can also automatically implement remote backup. Therefore, the cron Task Scheduler function is useless.

I. Solutions.

If there are few temporary backup tasks, you can manually back up them remotely. Then, use the cron task plan to perform planned remote backup. If temporary backup is frequent and users are eager for the remote backup function, they can only use other backup methods. I hope this solution can help solve this problem. This idea is simple, that is, let the operating system check whether a backup file is generated at a fixed time (such as 10 minutes. If the backup file is generated, the system moves (not copies) the backup file to a remote backup.

However, I would like to introduce another solution, that is, using the while loop to solve the problem of waiting for this file. In fact, this file wait is not a problem encountered by the system engineer, but has a certain degree of universality. For example, an application needs to read data from a file, which is generated by another application or different processes of the same application. Therefore, the original application can run smoothly only after another program or process has created the file. Therefore, from the perspective of programming, you need to use a script to detect whether the specified file has been successfully created. If it is created, execute a command to make it automatically call a program. For example, a parameter file is required for unattended installation. At this time, the system engineer can write a script to determine whether the parameter file exists. If yes, install it immediately. If it does not exist, wait. The installation process is started after the configuration file is created normally (at this time, the system engineer does not need to restart the script program ).

II. Specific implementation.

The above idea is mainly implemented using the while loop. With the while loop, the operating system will repeatedly execute a group of commands until the control command returns a false status value. To implement the unattended installation process above, you can use the following script.

While [! -R profile.txt] # cyclic Condition Statement

Do # cyclic operation statement

Sleep 30

Done

./Setup. sh

What are the meanings of each part and precautions when writing this script? Specifically, System Engineers need to master the following content. These are the most basic things to write a while loop structure.

First, you need to wait until the writing of common cyclic condition statements. Generally, any Unix Command or test content can be used as a cyclic condition. That is to say, as long as you have mastered Common Unix commands, you can write loop statements. Even so, sometimes when writing this circular statement, System Engineers often need to have some creativity or experience. Indeed, the final loop statements are composed of basic operating system commands. But sometimes they often need to be flexibly combined. For example, the loop condition statement used above is relatively simple, but not necessarily can be obtained by every system engineer. Author this! -R profile.txt indicates that a file cannot be read. Generally, the files created are currently readable. If the system determines that the file is unreadable, the file does not exist (as long as the permission has not been adjusted ). In other words, sometimes System Engineers often need to consider the problem from another angle. In this case, convert "file does not exist" to "file unreadable ". At this time, this loop Condition Statement is easy to write. Unfortunately, what many System Engineers lack is the ability to switch. This process requires a solid foundation, a certain degree of innovation capability, and a considerable accumulation of work experience. For this reason, the author suggests that to use a Unix or Linux operating system well, you should consciously exercise yourself in your work.

Second, you need to understand the writing skills of Operation statements. Sometimes, in order to meet certain requirements, the Operation statements in the while loop structure do not write some actual operation statements. The operation statement is written outside the loop. As in the above case, I used the statement sleep 30 in the internal operation statement section. What does this mean? If the entire While loop structure is combined, it means that if the conditions are met (the profile configuration file is not readable, that is, it does not exist), the following loop is executed every 30 seconds. If the conditions are not met (the profile configuration file is readable and exists), the system jumps out of this loop and runs subsequent statements. That is to say, at this time, the actual operation (start an installation process) is placed outside the loop. Sleep 30 is used inside the loop to control the cycle execution time. Of course, you can also write some practical operations in this loop. For example, some similar error messages such as "no preparation file is found" are displayed on the screen or the installation log. These similar friendly prompts can improve program friendliness. There are many similar processing mechanisms. System Engineers should make a summary in their daily work. In addition, if you look at the script programs written by other engineers, you will often be able to get a lot of valuable content. Imitation is often a shortcut to enhance your business capabilities in the shortest time.

Finally, pay attention to the writing format and specifications. When writing a loop structure, whether it is a while loop or a for loop, if you can consciously use the TAB or space key, it can significantly improve the readability of the Code. For example, it is recommended that the execution statement of the loop internal structure be written with the do or done keyword branch and in the indent format. Although these rules are not followed immediately, the script program has no substantial impact. However, there will be a lot of difficulties in the subsequent maintenance and reading. Especially when there are many internal statements in the loop. Therefore, it is best for System Engineers to comply with these code writing specifications. After all, some complex functions can be implemented only through team collaboration. If everyone can abide by the same writing standards, it will undoubtedly improve the team's collaboration. In addition, you need to pay attention to the syntax content. If you do not do it, there is no semicolon ending character. These syntactic errors are reported by the system during execution, so the problem is not serious. This only increases the chances and workload of rework.

3. Use while to implement an infinite loop.

As in the case of remote backup of the database above, it may be more appropriate to implement it through an infinite loop. For example, the operating system executes a loop every 30 minutes. The operation that cannot be performed in a loop is to first use the if statement to determine whether the backup file has been updated within 30 minutes. If there is an update, copy it or move it to a remote server. No matter whether there are any updates, this loop will be executed every 30 minutes. In this case, an infinite loop is required. With the meaning of the while LOOP condition, this infinite loop is easy to implement. You only need to set the loop Condition Statement to true. That is, while true; do operation statement; done &.

In this loop structure, the author uses true as the loop condition, and its loop condition is always true, then the operating system will continue to loop until the user manually terminates. Note that if the loop statement is relatively simple, it can be written in the same line. Only a semicolon (;) is needed to identify a suitable position (such as before a keyword. In addition, to avoid interference with other processes, it is best to place these infinite loop operations in the background, which can ensure the relative independence between processes. In this case, you only need to add the & Symbol after the done keyword to tell the operating system to put this infinite loop into the background for running.

However, when writing an infinite loop, you must pay attention to the occurrence of an infinite loop. For example, an operation in a loop is time-consuming. Before this operation is completed, the new cycle starts again. Over time, system resources will be exhausted. Therefore, it is best to set some conditions inside the program, such as the usage of CPU and other resources, to force the end of the infinite loop. The author believes that this is a safer solution, which can effectively avoid the impact of infinite loops on the operating system performance.

  1. How to configure the Netware server in Linux
  2. Development of Linux systems on embedded devices
  3. In-depth introduction to how the Linux kernel works (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.