Use shell script to monitor if a process exists that does not exist the instance that is started

Source: Internet
Author: User
Tags readable

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
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

Please see the details of my Baidu experience: the use of shell script to monitor the existence of a process does not exist to start the tutorial _ Baidu Experience
Http://jingyan.baidu.com/article/59a015e3a3a404f795886542.html

=======================================

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 test the condition with a test command.   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"]: Determines whether a file [-X "/bin/ls"]: Determines whether/BIN/LS exists and has executable permissions [-n ' $var]: Determine if the $var variable has a value ["$a" = "$b"]: Determine if $a and $b   Equality          -r file user readable as true-W file user can write as true-F file user can execute as true-* * * file is true for regular files -d file for the directory is true-C file for character special files for the true-B file for the block special file for the true-s file is the same as the real---t files, non 0 o'clock is true 1 The specified device for the terminal is true   ######################################################### 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-compares two arguments to be unequal-lt-parameter 1 is less than the parameter 2-le-parameter 1 is less than or equal to the parameter 2-gt-parameter 1 is greater than the parameter 2-ge- Whether the parameter 1 is greater than or equal to the parameter 2-f-check that a file exists (for example, if [-F "filename"])-D to check whether the directory exists with almost all judgments 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. ################################################################## determine if the 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 chenshuo@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.