shell查看ps进程状态
#!/bin/bash
RED="\033[31m" # Error message
GREEN="\033[32m" # Success message
YELLOW="\033[33m" # Warning message
BLUE="\033[36m" # Info message
PLAIN='\033[0m'
colorEcho() {
echo -en "${1}${@:2}${PLAIN}"
}
slogon() {
echo "#########################################################"
echo -e "#\t${GREEN}${@:1}${PLAIN}\t"
echo "#########################################################"
echo ""
}
<<COMMENT
awk用法:https://wangchujiang.com/linux-command/c/awk.html
$1: 进程归属用户是谁,正则写法:/正则表达式/,比如:/[0-9]+/,/^100*/
$2: pid在第几个字段
$3: 运行程序在第几个字段
$4: 过滤名称是什么,正则写法:mysqld\s,\w
get_process_status $1 $2 $3 $4
调用示例:
get_process_status "/mysql/" 2 8 "mysqld\s"
COMMENT
function get_process_status {
user=$1
pid=$2
command=$3
process=$4
print_debug_time
sh=`ps -ef | grep -v grep | grep -E $process | awk '
{
user=$1
if(user ~ '$user'){
pid=$'$pid';
print "[" $'$command' "]" " is running, pid is " pid
};3
}'`
if [[ $sh == '' ]]
then
echo $process "no found";
else
echo $sh;
fi
}
<<COMMENT
获取日期和时间
COMMENT
function print_debug_time {
colorEcho $GREEN `date +%Y-%m-%d` `date +%H:%M:%S`;
echo -n " ";
}
slogon 检查相关进程是否启动正常
get_process_status "/mysql/" 2 8 "mysqld\s"
get_process_status "/root/" 2 8 "usmt"
get_process_status "/root/" 2 9 "mysqld_safe"
get_process_status "/root/" 2 8 "libvirtd"
get_process_status "/root/" 2 8 "guacd"
# get_process_status "/root/" 2 8 "container-port\s6379"
# get_process_status "/root/" 2 8 "container-port\s4822"
# get_process_status "/root/" 2 16 "asbv"
<<COMMENT
检查docker ps状态
COMMENT
function check_docker_status {
array=(/redis/ /guacd/)
for i in ${array[*]}; do
print_debug_time
docker ps --filter "status=running" | awk '
NR>1{
name=$2
if(name ~ '$i'){
print "[docker ps]: names='$i' is running";
}
}'
done
}
<<COMMENT
检查pm2 list状态
COMMENT
function check_pm2_status {
status='online'
print_debug_time
pm2 list | awk '
NR==4{
name=$4
status=$18
if(var = "online"){
print "[pm2 list]: "'name'" is "'var'
}
}'
}
slogon 检查pm2 list状态
check_pm2_status
slogon 检查docker ps状态
check_docker_status
<<COMMENT
检查某个目录是否存在
COMMENT
function isExistPath {
path=$1;
print_debug_time
if [[ ! -d $path ]]; then
colorEcho $RED "$path is no found!"
else
colorEcho $GREEN "$path is exist!"
fi
}
slogon 检查/home/arcsoft_lib目录是否存在
isExistPath /home/arcsoft_lib
echo ""
调试脚本
![image](https://img2022.cnblogs.com/blog/2466616/202208/2466616-20220803152620946-1004819835.png)