Use shell script to monitor if a process exists nonexistent instance _linux shell

Source: Internet
Author: User

Use shell script to monitor whether a process exists without a startup instance, first on the code dry:

#!/bin/sh
ps-fe|grep processstring |grep-v grep
if [$-ne 0]
then
echo "START process ..."
else< C6/>echo "runing ..."
fi

#####
Processstring represents a process feature string that can be queried to a unique process's character string
0 indicates the existence of
$? -ne 0 does not exist, $? -eq 0 Exists

Timed execution:

Crontab-e
0 04,12,21 * * */bin/bash/home/mysh/monitorprocess.sh
Every night 4 o'clock, noon 12 o'clock, night 21 o'clock Test
0 4,12 * * * *
Time-Sharing and Lunar Week
* 04,12 * * This refers to the 4-point and 12-point execution per minute
0 4,12 * * * 4 point 12 point 0 minute execution

Expand related knowledge:

The If else syntax for the shell and the logical expression greater than, less than, and so on:

If ...; Then

....

Elif ...; Then

....

Else

....

Fi

In most cases, you can use test commands to test conditions.   For example, you can compare strings, determine whether a file exists and whether it is readable, and so on ... You typically use [] to represent a conditional test. Note that the spaces here are important. To ensure that the square brackets are blank.
[f "Somefile"]: To determine whether a file
[-X "/bin/ls"]: Determine if/bin/ls exists and has executable permissions
[-N ' $var]: Determine if the $var variable has a value
["$a" = "$b"]: Determine if $a and $b are equal-r file users can read as True
-W file user can write as true
-X file user can execute as true
-F file is true for regular files
-D file is true for directory
-C file is true for character special files
-B file is a block special file is True
-S file file non-0 o'clock is true
-T file True when the device specified by the file descriptor (default is 1) is a terminal
######################################################### shell script with conditional selection
Simple shell scripts are generally competent for tasks that do not contain variables. However, in the implementation of a number of decision-making tasks, it is necessary to include the if/then of the conditional judgment. Shell scripting supports such operations, including comparing operations, determining whether a file exists, and so on. The basic if Condition command options are:-eq-compares two arguments for equality (for example, if [2–eq 5])
-ne-compare two arguments for unequal
-lt-parameter 1 is less than parameter 2
-le-parameter 1 is less than or equal to parameter 2
-gt-parameter 1 is greater than parameter 2
-ge-parameter 1 is greater than or equal to parameter 2
-f-Check if a file exists (for example, if [-F "filename"])
D-Check if the directory exists
Almost all judgements can be implemented using these comparison operators. The common-f command option in a script checks to see if it exists before executing a file.

To determine whether a file exists

#!/bin/sh
today= ' date-d yesterday +%y%m%d ' file=
"apache_$today.tar.gz"
Cd/home/chenshuo/shell
if [-F "$file"];then
echo "OK"
else
echo "error $file" >error.log
mail-s "fail backup from Test" Chensh uo@soufun.com <error.log
fi

Shell Basic Command
(1) PS aux display system all process, one line
(2) grep "ABC" reads the character stream from the standard input, outputting the line containing the string "ABC"
(3) Grep-v "ACB" reads a stream of characters from the standard input and outputs rows that do not contain the string "ABC"
(4) Wc-l reads the character stream from the standard input, the output line number

For example, to detect whether a process httpd exists, the process is as follows:
(1) Read all processes of the system
(2) Determine if the information that contains the specified process name exists
Through a pipe connection, the commands are as follows:

PS Axu |      grep "httpd" |      Grep-v "grep" | Wc-l
All processes--> get rows containing "httpd"--> delete grep process information--> output the last number of rows

You can tell whether a process exists by determining whether the command's execution result is 0.

The script is as follows:

#!/bin/sh while
true;do
    count= ' ps-ef|grep http|grep-v grep '
    if [$? '!= ' 0 "];then
echo  " >> >>no Httpd,run It "
service httpd start
else
echo" >>>>HTTPD is runing ... "
fi
Sleep 5 Done

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.