Shell script code Summary for the detection process _linux shell

Source: Internet
Author: User

A simple shell script to find out if a critical service is running and is suitable for Linux or UNIX operating systems.
The script can also send notifications using e-mail.

Code:

Copy Code code as follows:

#!/bin/bash
# Name:service.chk Service Detection Script
# # According to their own environment modified
_pgrep= "/usr/bin/pgrep"
_mail= "/usr/bin/mail"

# # Environment variables
_chklist= "/usr/bin/php-cgi/usr/sbin/nginx/usr/sbin/lighttpd/usr/sbin/mysqld/usr/sbin/apache2/usr/sbin/named/ Usr/sbin/pgsqld "

# # Yes | No
_sendemail= "No"

# # Email
_email= "Test@jb51.net"

# # Do not modify the following configuration
_failed= "false"
_service= "Service:"

_running () {
Local p= "${1##*/}"
Local s= "true"
$_pgrep "${p}" >/dev/null | | {s= "false"; _failed= "true"; _service= "${_service} $";}
[["$s" = "true"]] && echo "$ Running" | | {echo-n "not running"; [[!-f ' $]] && echo ' [not found] ' | | echo; }
}

# # Header
echo "Service status on ${hostname} @ $ (date)"
echo "------------------------------------------------------"

# # Check If your service is running or not
For S in $_chklist
Todo
_running "$s"
Done

# # Send A quick email update (good for cron jobs) # #
[[' $_failed ' = ' true ' && ' $_sendemail ' = ' yes ']] && {_mess= ' $_service failed on $HOSTNAME @ $ (date);
$_mail-s ' Service not found ' "$_email" < "${_mess}";
}

Results:



Here are some small pieces of code, we can also refer to the following

Script one:

Copy Code code as follows:

#!/bin/sh
Program=xxxx #进程名
Sn= ' Ps-ef | grep $program | grep-v grep |awk ' {print $} ' #获得进程端口号
If [' ${sn} ' = '] #如果为空 to indicate that the process did not start
Then
Nohup/home/oracle/xxxx & #后台启动进程
echo Start OK!
Else
Echo Running
Fi

Script two:

Copy Code code as follows:

#!/bin/sh
Ps-ef |grep./fileserver >/dev/null 2>&1 #检测进程写入/dev/null
If [$?-eq 0] #0为正常
Then
Echo Logprocess Run ok!
Else
Nohup/home/oracle/xxxx &

echo Start OK!
Fi


Script Three:

Copy Code code as follows:

#!/bin/sh

Count= ' Ps-fe |grep "a.out" | Grep-v "grep" | Wc-l '
If [$count-lt 1]; Then
/root/sh/restart.sh


Script four:

Copy Code code as follows:

Pname= "AUTHD"
pathname=/root/cauthd/build/
Length= ' ps-ef|grep ' $PNAME ' |grep-v grep|cut-b 49-200|wc-c '
if test $LENGTH-eq 0
Then
CD $PATHNAME
Nohup $PNAME >/dev/null

Script Five:

Copy Code code as follows:

#! /bin/bash
echo "Please enter the process name:"
Read process
echo "The process you are looking for is $process, looking for ..."
PS > Text1
grep "$process" Text1

Declare-i a=$?
If [$a-eq 0]
Then
echo "This process exists"
Else
echo "This process does not exist"
Fi
RM Text1

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.