Shell Script Planning Templates

Source: Internet
Author: User
Tags jboss

Shell Script Planning Templates

In Linux operations, shell scripts are indispensable tools, but each operator programming habits are different, many times is to implement a function, written out of the script is rotten seven or eight bad.
Scripts must be normalized and should be started in a few ways:
1, the script must record the log, as much as possible to execute command operations written in the log, it is easy to track in the future;
2, the script as far as possible to achieve functional modularization, function name is readable;
3, the script must have incoming parameters;
4, the script execution must be a single process, that is, the script can only run one time period;
5, the script execution process must lock the mechanism;
6, the script comment is indispensable;
7, the script is best unified entrance (main function), personal preferences;
Roughly divided into these 7 points, operations must standardize the process, do not do rogue operation, responsibility for their own work, but also easy for other colleagues to view or take over, I personally do not advocate scripting complications, make a bunch of people can not understand.

For example, on-line is an indispensable work of operation, I test to achieve a very simple function:
1. JBoss Container releases Java code (regardless of profile update)
1) Close the service;
2) Copy the code to the specified directory;
3) Start the service;
2, the server open a few JBoss instances (by Business modularity), automatically identify each container (directory:/data/)
3, according to the modular release version, update only one business module version at a time

#!/bin/bash
#######################################################
# $Name: release.sh
# $Version: v1.0
# $Function: Release for JBoss Script
# $Author: Verrion Jian
# $Create Date:2016-09-22
# $Description: Sub module to release for firm
#######################################################

# define Scripting environment variables
Shell_name= "Release.sh"
Shell_dir= "/data/scripts"
Shell_log= "${shell_dir}/${shell_name}.log"
Lock_file= "/tmp/${shell_name}.lock"
Package_dir= "/data/code"

# Record Log
Log_output () {
Log_info=$1
echo "$ (date" +%y-%m-%d ") $ (date" +%h-%m-%s "): ${shell_name}: ${log_info}" >> ${shell_log}
}

# scripting usage, automating listing of all instances that exist on the server
Usage () {
Get_modules
Str= ' echo ${module_array[@]} |sed s/[[:space:]]/' | ' /g '
echo $ "Usage: $ A [$str]" && exit
}

# Add lock file
Lock () {
Touch ${lock_file}
}

# Delete lock file
Unlock () {
Rm-f ${lock_file}
}

# Determine if the code temp directory and file exist
Is_package_exist () {
if [!-D $PACKAGE _dir];then
Log_output "The script exec as insufficient--and the release directory is Non-existent"
echo "The release Directory is Non-existent" && exit
Fi


package_num= ' ls $PACKAGE _dir |wc-l '
If [$package _num-eq 0];then
Log_output "The script exec as insufficient--and the packages for release is empty"
echo "The packages for release is empty,please check directory:" $PACKAGE _dir "and upload packages" && exit
Fi
}

# list the number of JBoss containers above the server
Get_container () {
Container_name= ' ls-l/data/|grep JBoss | awk ' {print $9} ' |xargs '
}

# List Instance module information, process number, etc. (such as: Test_login.war as Java code package)
Get_modules () {
Get_container
for i in $container _name
        Do
war_num= ' ls/data/$i/server/default/deploy/test*war | wc-l '
IF [$war _num-ne 1];then
Log_output "The container deploy to not standard--> Num of the war_package can only one"
echo "the contain Er deploy to not standard--> Num of the war_package can only one "&& exit
Else
War_name= ' ls/data/$i/server/default/deploy/test*war | cut-d/-f7 | cut-d--f1 '
pid= ' ps-ef |grep $i |grep-v grep |awk ' {print $} '
Module_name= $war _name "" $modul E_name
pids= $pid "" $pids
jboss_name= $i "" $jboss _name
Fi
Done
module_array= ($module _name)
pid_array= ($pids)
jboss_array= ($jboss _name)
}

# Modular release on-line
Release_module () {
# Ensure a process is running in best
If [-F ' $LOCK _file "];then
Log_output "${shell_name} is running"
echo "${shell_name}" is running && exit
Fi

# Ensure the package exist
Is_package_exist

# denfie varchar
Packagename=$1
Packagenum=$2
packagenum= ' Expr $packagenum-1 '
target_pid=${pid_array[$packagenum]}
target_jboss=${jboss_array[$packagenum]}
Target_dir= "/data/$target _jboss/server/default/deploy"
package_str= ' ls $PACKAGE _dir '

Log_output "Release module for platform system start"
Lock
If [[$package _str =~ $packagename]];then
#kill-9 $target _pid
echo "kill-9 $target _pid"
log_output "kill-9 $target _pid"
#echo $targ Et_pid
Sleep 3
#rm-rf $target _dir/$packagename *
echo "RM-RF $target _dir/$packagename *"
L Og_output "RM-RF $target _dir/$packagename *"
#ls $target _dir/$packagename *
#cp-R $PACKAGE _dir/$packagenam e* $target _dir
echo "Cp-r $PACKAGE _dir/$packagename * $target _dir"
log_output "Cp-r $PACKAGE _dir/$package name* $target _dir "
#ls $PACKAGE _dir/$packagename *
Else
Log_output" The package of publish Director Y isn't found "
Echo" the package of publish directory was not found "&& exit
Fi
Log_output" re Lease module for platform system stop "
Unlock
}

# Main Function entry
Main () {
Tag=0
If [-Z $];then
Usage
Fi

Get_modules
For I in $module _name
Do
Let Count+=1
if [$i = $];then
Pkgname=$1
pkgnum= $count
Release_module "$pkgname" "$pkgnum"
Tag=1
Break
Fi
Done


If [$tag-eq 0];then
Module_name= ""
Usage
Fi
}

# Execute Script
Main $

Shell Script Planning Templates

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.