Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the shell script code used to detect the process

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you what the shell script code used to detect the process is, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

A simple shell script to find out if a critical service is running, suitable for Linux or Unix operating systems.

The script can also use email to send notifications.

Code:

#! / bin/bash

# Name: service.chk service detection script

# # modify according to your own environment

_ 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 million dollars /}"

Local s = "true"

$_ pgrep "${p}" > / dev/null | | {s = "false"; _ failed= "true"; _ service= "${_ service} $1,";}

[["$s" = "true"]] & & echo "$1 running" | | {echo-n "$1 not running"; [[!-f "$1"]] & & echo "[$1 not found]" | | echo;}

}

# # header

Echo "Service status on ${HOSTNAME} @ $(date)"

Echo "- -"

# # Check if your service is running or not

For s in $_ chklist

Do

_ 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}"; } 结果:

Here are some small pieces of code, which you can also refer to

Script one:

The code is as follows:

#! / bin/sh

Program=XXXX # process name

Sn= `ps-ef | grep $program | grep-v grep | awk'{print $2}'`# get the process port number

If ["${sn}" = ""] # if it is empty, the process is not started

Then

Nohup / home/oracle/XXXX & # background startup process

Echo start ok!

Else

Echo running

Fi

Script 2:

The code is as follows:

#! / bin/sh

Ps-ef | grep. / FileServer > / dev/null 2 > & 1 # detect process writes / dev/null

If [$?-eq 0] # 0 is normal

Then

Echo logprocess run ok!

Else

Nohup / home/oracle/XXXX &

Echo start ok!

Fi

Script 3:

The code is 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 4:

The code is 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:

The code is as follows:

#! / bin/bash

Echo "Please enter the process name:"

Read process

Echo "the process you are looking for is $process, and you are looking for..."

Ps > text1

Grep "$process" text1

Declare-I axioms?

If [$a-eq 0]

Then

Echo "this process exists"

Else

Echo "the process does not exist"

Fi

Rm text1

The above is all the content of the article "what is the shell script code used to detect the process". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report