shell training camp Day29

Exercise 86In CentOS 6 system, we can use ntsysv to shut down the services that do not need to be started, of course, we can also use chkconfig tool ...

Exercise 86
In CentOS 6 system, we can use ntsysv to shut down the services that do not need to be started, of course, we can also use chkconfig tool to implement.

Write a shell script and use the chkconfig tool to shut down the infrequent services. The script needs to be written interactively, and we need to provide it with the name of the closed service.

#!/bin/bash
LANG=en

while :
do
chkconfig --list 2>/dev/null|grep '3:on' |awk '' > /tmp/on_sev.txt
Echo - e "\ 033 [these services are enabled in 32m system: \ 033[0m"
cat /tmp/on_sev.txt
echo
read -p "Please select a service from this list: " s

if ! grep -qw "$s" /tmp/on_sev.txt then echo -e "\033[31m The service name you provided is not open.\033[0m" continue fi chkconfig $s off break

done

Exercise 87
In the production environment, it is often encountered that tomcat cannot shut down completely, that is to say, the java process cannot be shut down completely by using the shutdown.sh script of tomcat. Therefore, with the help of shell script, you need to kill the process and then start it.

Write a shell script to achieve the above functions. The command to kill a process completely is kill-9 PID.

Reference answer
#!/bin/bash dir=/usr/local/tomcat/bin/ java_pc() { pgrep java|wc -l } cd $dir ./shutdown.sh count=0 while [ $count -lt 5 ] do n=`java_pc` if [ $n -gt 0 ] then killall java count=$[$count+1] sleep 1 else break fi done n=`java_pc` if [ $n -gt 0 ] then killall -9 java fi n=`java_pc` if [ $n -gt 0 ] then echo "Tomcat Unable to force kill." exit fi cd $dir ./startup.sh //Exercise 88 //At least two methods are used to remove the suffixes of. Bak from all the file names under the current directory in batches. For example, 1.txt.bak is 1.txt after it is removed #!/bin/bash for f in `ls -d ./*.bak ` do # mv $f `echo $f|sed 's/.bak$//'` f1=`echo $f|awk -F '.bak$' '' ` mv $f $f1 done //Exercise 89 //Write a shell script to query the expiration time of the specified domain name, and send a reminder email every day one week before the expiration. #!/bin/bash [email protected] #Current date time stamp, used to compare with the expiration time of domain name t1=`date +%s` #Check whether the whois command exists. If not, install the jwhois package is_install_whois() { which whois >/dev/null 2>/dev/null if [ $? -ne 0 ] then yum install -y epel-release yum install -y jwhois fi } notify() { #e_d=`whois $1|grep 'Expiry Date'|awk ''|cut -d 'T' -f 1` e_d=`whois $1|grep 'Expiration'|tail -1 |awk '' |awk -F 'T' ''` #If the value of E ﹣ D is empty, filter the keyword 'Expiration Time' if [ -z "$e_d" ] then e_d=`whois $1|grep 'Expiration Time'|awk ''` fi #Convert domain name expiration date to timestamp e_t=`date -d "$e_d" +%s` #Count the total number of seconds in a week n=`echo "86400*7"|bc` e_t1=$[$e_t-$n] e_t2=$[$e_t+$n] if [ $t1 -ge $e_t1 ] && [ $t1 -lt $e_t ] then python mail.py $mail_u "Domain $1 will to be expired." "Domain $1 expire date is $e_d." fi if [ $t1 -ge $e_t ] && [ $t1 -lt $e_t2 ] then python mail.py $mail_u "Domain $1 has been expired" "Domain $1 expire date is $e_d." fi } #Check whether the last whois query process running exists #If it exists, you need to kill the process to avoid affecting the execution of this script if pgrep whois &>/dev/null then killall -9 whois fi is_install_whois for d in aaa.com bbb.com aaa.cn do notify $d done //Exercise 90 //Write a shell script, when we execute it, prompt to input the ip and root password of the other party, and then automatically add the local public key to the other party's machine, so as to realize key authentication. #!/bin/bash read -p "Enter a IP address: " ip read -p "Enter the root Password: " pasd is_install() { if ! rpm -q $1 &>/dev/null then yum installl -y $1 fi } is_install openssh-clients is_install expect if [ ! -f ~/.ssh/id_rsa.pub ] then echo -e "\n" |ssh-keygen -P '' fi cat > key.expect <<EOF #!/usr/bin/expect set host [lindex \$argv 0] set passwd [lindex \$argv 1] spawn ssh-copy-id root@\$host expect { "yes/no" { send "yes\r"; exp_continue} "password:" { send "\$passwd\r" } } expect eof EOF chmod a+x key.expect ./key.expect $ip $pasd

3 December 2019, 16:49 | Views: 3681

Add new comment

For adding a comment, please log in
or create account

0 comments