当前位置:网站首页>Ups and esxi realize automatic shutdown after power failure
Ups and esxi realize automatic shutdown after power failure
2022-06-24 05:47:00 【Rokas. Yang】
ESXi Is a well-known commercial virtualization software , Only a few advanced UPS To adapt to this kind of enterprise software , Ordinary UPS Power off , Script linkage is required , Once a... Is detected action, Trigger or delay the shutdown action (UPS It can last for dozens of minutes ), Avoid file systems 、 Hard disk 、 The power supply is damaged , Do more harm than good , Empathy , As long as a device with a script like function is plugged in UPS, Can achieve the function of power-off and automatic shutdown , and UPS The highly supported devices can be configured through the data communication line and background , Relatively convenient , Like a cloud of light NAS.
One 、 Turn on ESXI Of ssh function , and ssh Sign in ESXI establish ups Script
1. Enter into ESXI web Console , Turn on ssh function
2.ssh Connect to ESXI, Create necessary files and scripts
Script logic : Every time 1 minute ping Specify at one time IP, At a time ping once , If it reaches 2 Time , Three minutes later ping once , If it still doesn't work , Write the record time to the log ups.log, And execute the shutdown command , You can change the specific time and logic at will :
[[email protected]:~] cd /vmfs/volumes/data #cd To the corresponding hard disk volume , The name of your hard drive shall prevail [[email protected]:/vmfs/volumes/5f174c56-6a79f5cc-c990-a03e6ba0a187]mkdir ups [[email protected]:/vmfs/volumes/5f174c56-6a79f5cc-c990-a03e6ba0a187]cd ups [[email protected]:/vmfs/volumes/5f174c56-6a79f5cc-c990-a03e6ba0a187/ups]touch ups.log # Write a log with [[email protected]:/vmfs/volumes/5f174c56-6a79f5cc-c990-a03e6ba0a187/ups]vim ups.sh # Create the following script #!/bin/sh UPS_LOG=/vmfs/volumes/data/ups/ups.log count=0 IP=192.168.1.200 # Write your gateway IP, As long as the power is off ping It doesn't work IP Will do while :;do ping -c 1 $IP > /dev/null if [ $? -eq 0 ]; then : else count=$(expr $count + 1); fi sleep 60 if [ $count -ge 2 ]; then echo "$(date) AC Power maybe off, checking again after 3 minutes ! " >> $UPS_LOG sleep 180 ping -c 1 $IP &>/dev/null if [ "$?" -eq 0 ]; then echo "$(date) Checkagain, AC Power OK ! " >> $UPS_LOG else echo "$(date) AC Power is already off, shut down NAS Now! " >> $UPS_LOG /bin/shutdown.sh halt fi else continue fi done [[email protected]:/vmfs/volumes/5f174c56-6a79f5cc-c990-a03e6ba0a187/ups]chmod +x ups.sh # Just give me permission to execute , It is not necessary to 777 [[email protected]:/vmfs/volumes/5f174c56-6a79f5cc-c990-a03e6ba0a187/ups]
3. Verify that the script takes effect
ESXI Of sh It's a link to busybox Of , Equivalent to a cropped version bash, So grammar cannot be combined with bash Exactly the same as :
The execution permission has been added before ,./ups.sh and sh ups.sh Will do , To see the script processing logic , Add one -x Parameters :
ping Yes. , therefore $_ The return code of the last command is 0, Benchmarking expression , Meet the conditions ,: Placeholders mean doing nothing , Sleep 60 I'll continue in seconds ping, This indicates that the script is normal , If you want to see the entire script processing logic ,ping An impassable IP that will do .
Two 、 Daemons and NOHUP
There are two ways , One is the daemon , One is nohup+ Background operation , Write to the startup script , Choose one of the two methods
1. The daemon mode
The so-called daemon , seeing the name of a thing one thinks of its function , Is to guard the process it wants to guard , How to achieve ? The simplest daemon is to check whether the process is running normally at a specified interval , Call the startup script or command to make the process run without running , Always guard its whole life cycle .
[[email protected]:/vmfs/volumes/5f174c56-6a79f5cc-c990-a03e6ba0a187/ups]vim ups_daemon.sh # Write the following script #!/bin/sh result=$(ps -c |awk '!/awk/&&/ups.sh/{print}'|wc -l) # adopt awk and wc Determine whether the process is running if [ "${result}" -lt "1" ]; then /vmfs/volumes/data/ups/ups.sh & echo "$(date) UPS daemon process running" >> /vmfs/volumes/data/ups/ups.log fi exit 0 [[email protected]:/vmfs/volumes/5f174c56-6a79f5cc-c990-a03e6ba0a187/ups]
Write to boot auto start :
[[email protected]:/vmfs/volumes/5f174c56-6a79f5cc-c990-a03e6ba0a187/ups]cat /etc/rc.local.d/local.sh /bin/kill $(cat /var/run/crond.pid) /bin/echo '*/3 * * * * /vmfs/volumes/data/ups/ups_daemon.sh' >> /var/spool/cron/crontabs/root /bin/crond exit 0 [[email protected]:/vmfs/volumes/5f174c56-6a79f5cc-c990-a03e6ba0a187/ups]
ESXI Of crond After the process restarts, the scheduled tasks written will be cleared , Only the system's , Write the startup and auto startup script to ensure that each restart occurs at crond There are daemon tasks inside , Execute the daemon script every three minutes , That is, every three minutes ups.sh Whether the script is running properly .
2.NOHUP
Only for lazy people , One command is enough , Automatic operation after startup or restart :
[[email protected]:/vmfs/volumes/5f174c56-6a79f5cc-c990-a03e6ba0a187/ups]vim /etc/rc.local.d/local.sh # Write the following { nohup sh /vmfs/volumes/data/ups/ups.sh; } &>/dev/null & #nohup Running scripts in the background , It is forbidden to output on the console stdout or stderr, Disable output of text [[email protected]:/vmfs/volumes/5f174c56-6a79f5cc-c990-a03e6ba0a187/ups]
After using this method, the script will not run until the next boot , In order not to restart, you can run it once :
{ nohup sh /vmfs/volumes/data/ups/ups.sh; } &>/dev/null &3. Ensure that the process is running normally in the background
There is no need to go into details about this ,ps The command looks directly at , And others linux Distribution ps The input parameters are somewhat different :
Here we are , Whole UPS and ESXI Linkage has been completed , Repeated power-off test , Once and for all ; If you do it locally DDNS, Can cooperate with dnspod Of D Monitoring function , Once you feel it IP:PORT or URL cannot access , texting / Email alert , Truly realize the perception of power failure and recovery time at home thousands of miles away .
边栏推荐
- How to apply for a company domain name? Does it cost money to apply for a company domain name?
- How to build a website with a domain name? What steps need to be taken?
- Learning routes and materials for cloud native O & M engineers
- What is domain name registration? Do you still need to purchase ECS after domain name registration?
- What is the meaning of domain name being walled and what is the solution
- Pylin tool usage
- Progress update | optimization and upgrading of shard nodes
- How to file a personal domain name? What are the benefits of domain name filing?
- How about the work domain name? Does the work domain name need real name authentication?
- Select MySQL database from the command prompt window
猜你喜欢

How should we learn cloud native in 2022?

Answer questions! This article explains the automated testing framework in software testing from beginning to end
Learning routes and materials for cloud native O & M engineers
Easy to understand JDBC tutorial - absolutely suitable for zero Foundation
What cloud native knowledge should programmers master?
随机推荐
How do virtual hosts bind domain names? Can binding failure be used normally?
Pylin tool usage
Mysql database backup under Windows Environment
How to build a website with a domain name? Is the website domain name free to use?
What is the JS interface security domain name? What are the common domain names
Talk about my working experience in Tencent and byte
Live video: real time large screen analysis based on streaming computing Oceanus (Flink)
Typora software installation
[latest offer] 2-core 4G server is limited to 188 yuan, and it is in the process of being snapped up
How to resolve the domain name to IP? How long does it take for the domain name resolution to take effect?
[Tencent cloud] enterprise micro marketing, private domain traffic value growth and operation efficiency improvement
What is a first level domain name? What are the steps to purchase a primary domain name?
Is the prospect of cloud computing in the security industry worth being optimistic about?
What should I pay attention to in server hosting and migration?
Learning routes and materials for cloud native O & M engineers
PV and PVC analysis and use in kubernetes
What enlightenment does it consulting giant Accenture get from the blackmail attack?
How to file a personal domain name? What are the benefits of domain name filing?
How to apply for a website domain name and what problems should be paid attention to
How to resolve the primary domain name and how to operate it