Write a bash shell script

Source: Internet
Author: User
Tags echo display

Write a bash shell script
The script is as follows: it is used to monitor the number of mongo connections and restart the system.
#! /Bin/bash
# Source/etc/profile
Conn = '/usr/bin/mongo -- quiet <EOF
Db. serverStatus (). connections
Exit
EOF
`
Conn1 =$ {conn #*:}
Curr =$ {conn1% ,*}
# PID = $ (ps-ef | grep "mongodb" | grep-v grep | awk '{print $2 }')
Echo "$ curr"
If [$ curr-le 5800]
Then
Echo "it's OK"
Else
Echo "the connect count is too big, it will be restart"
For PID in $ (ps-ef | grep "maxConns" | grep-v grep | awk '{print $2 }')
Do
Echo "$ PID"
Kill-9 $ PID
Done
Echo "the process is killed"
Nohup numactl -- interleave = all/usr/local/mongodb/bin/mongod -- dbpath =/home/mongo/mongodb/data/-- log
Path =/home/mongo/mongodb/logs/mongodb. log -- maxConns = 6000 -- logappend &>/dev/null &
Echo "restart OK"
Fi
Problem 1: conn = '/usr/bin/mongo -- quiet <EOF is the sentence. In bash, the reference command result is usually ''or $ ()
In this sentence, we thought that the EOF situation was changed differently. At the beginning, I put 'or) behind the end EOF, always prompting a syntax error.
Then start another line 'or), and the operation is successful.
Problem 2: there is no problem in manually executing the Command Script. If you put it in crontab, the execution will not take effect.
I checked the information on the Internet, which may be the impact of environment variables. So I added source/etc/profile and source ~ /. Bash_profile
But it does not take effect. Because the redirection log is added when the crontab is created, the log is always stuck in the output it will be restart.
View the process in ps and find that the process is gone but mongo is not restarted. At first, I thought it was because nohup was used in bash.
Stay in that place, but the problem still persists. So I added the echo display PID before kill-9 $ pid, and found that there will be more than one pid
Therefore, a for loop is added. Later I found it wrong. After kill-9, I can add multiple PIDs. So there is no movement. Then, an echo is added to the kill-9 Statement.
"The process is killed", which cannot be displayed in logs all the time. I started to know what was going on. Why are there multiple PIDs? Why are the mongo processes?
Why is the echo output after kill disabled. The reason is that the process itself is killed by kill in the crontab. Crontab generates a sub-shell process. When awk is used for filtering
Because mongo is included, it is also filtered out, which is why multiple PIDs exist. Kill is killed, so the echo after kill is not displayed.
So I changed a filter condition and solved it with maxConns!


Related Article

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.