How to do operation and maintenance without being rogue-SHELL script

This article is organized and released based on the content shared by DevOpsDays guests. Welcome to pay attention to the "Efficient Operation and Maintenance" official account. Efficient Operation and Maintenance is committed to creating a F2F direct transportation and sharing community, and grows happily with operation and maintenance.

About the Author:

image

Zhao Shundong

Initiator of China SaltStack User Group, GOPS Gold Lecturer, Gold Author

Jianghu is known as the leader of Zhao, who was responsible for the command automation architecture and operation and maintenance of a certain unit of the armed police. He has been engaged in Internet operation and maintenance since he retired in 2008; once led the team in charge of the operation and maintenance of an e-commerce in China, the founder of UnixHot operation and maintenance community, "SaltStack Author of "Introduction and Practice".


Introduction: Everyone is a civilized person, especially those who do operation and maintenance. That's a gentleman. How can you be a hooligan? Quickly take a look at how to write SHELL scripts to avoid being rogue.

In the following case, we use the case of MySQL database backup SHELL script to illustrate:

Shell scripts that do not record logs are hooligans!

We often encounter a distressing thing in our work, what does a Shell script do, when does it start to execute, and when does it end. Especially for database backups, we want to know the backup time of our MySQL database. Therefore, it is particularly important to add logs to the script. Then our script should have a log function, dedicated to recording logs: get

image

In the above script, I wrote a log function shell_log. Every time we record a log, we directly execute shell_log and pass the log content as the first parameter to it. Try it now.

[root@linux-node2 shell]# catshell_template.sh.log 2016-08-27 06-01-19 : shell_template.sh :shell beginning ,write log test 2016-08-27 06-01-19 : shell_template.sh :shell success ,write log test

Shell scripts that can be executed directly are easy to play rogues?

Can a script be executed directly? Isn't it straightforward to execute? Just imagine, you have temporarily written a particularly important script, and what you are doing may be destructive. What should you do if it is accidentally executed by someone else./? And in many cases, we may have multiple functions of a script, so it is necessary for users to choose to execute them.

image

In the above script, we wrote the shell_usage function to tell users how to use this script. At the same time, I want to emphasize that, like writing SHEEL, we are often process-oriented. It is recommended to use functions as the unit, so that the script is very clear and readable. Quickly execute the following to see the effect.

[root@linux-node2 shell]# ./shell_template.sh Usage: ./shell_template.sh {backup}

Unlocked SHELL script is to let others play rogue

Can multiple people execute the script you write at the same time? If not, what will happen if multiple people execute it together? Think about it if you are a little cold sweat. So, don't leave traps for our other friends. But if your company is your only operation and maintenance, don't you really need to be afraid? Imagine if it is a scheduled task to run this script again, the last time it did not finish running, and then it runs again when the time comes? Then, then, then, the consequences are terrible.

image

I added two functions shell_lock and shell_unlock to the script. It is very simple, that is, to create a lock file. Then when you execute it, first judge whether the lock file exists. If it exists, it means that other users are executing it, and then exit. If you haven't created the lock file yourself, start execution, and delete the lock file after execution.

Okay, now you can quickly open another window to try if you can execute this script, or go to the /tmp directory to see if the lock file is created. Please note: If you exit with a known exception, you must delete the lock file as well.

Let others remember your good!

For a functional script, there seems to be something missing. Yes, it is the comment! We need to explain what this script does. Or after you leave your job, after others see this script, I will wipe it. Who wrote such an awesome script? Don't be afraid, write your name. Due to limited space, only a part of the screenshot is shown:

image

Of course, there are many scripting techniques, which cannot be described one by one. However, if you can master the above three techniques, you immediately feel that the scripts you write are a bit tall. Is there anything wrong with it?

Download script template

If you think these tips are more useful and want to download them for your own use, what should you do? I have been put in the operation and maintenance community, please copy the link below to open it.

https://www.unixhot.com/article/60

So, in the end, we no longer play rogues when writing scripts, so what about other operation and maintenance work? Hurry up and pay attention to our WeChat public account below. The series of articles "How to do operation and maintenance without being a rogue" is coming to you.


Guess you like

Origin blog.51cto.com/15127563/2665726