Use a shell script to monitor whether a process exists or not, the instance that is started comes with an if judgment detail condition

Source: Internet
Author: User
Tags readable

#!/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 for a unique process's characteristic string
0 indicates the existence of the
$? -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, 12 o'clock noon, night 21 o'clock Detection
0 4,12 * * *
Time-sharing Week
* 04,12 * * This refers to 4 points and 12 points per minute of execution
0 4,12 * * * 4 points 12 points 0 minutes to execute once

For more details, please see my Baidu experience: using shell script to monitor the existence of the process does not exist then start the tutorial _ Baidu Experience
Http://jingyan.baidu.com/article/59a015e3a3a404f795886542.html

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

Expand your knowledge about:

The If else syntax of 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 the condition.   For example, you can compare strings, determine whether the file exists and whether it is readable, etc... The condition test is usually represented by "[]". Note that the space here is important. The space to ensure the square brackets. [-F "somefile"]: Determine whether a file [-X "/bin/ls"]: Determine whether the/bin/ls exists and have executable permissions [-n "$var]: Determine whether the $var variable has a value [" $a "=" $b "]: Determine whether a $ A and $b are equal          -r file user readable as true-W file user can write as true-X file user can execute as true-F files file to true-D f ile file for directory True-c file for character special files for real-B file files for block special files for true-s file files non-0 o'clock for true-t file when the device specified by the descriptor (default = 1) is terminal For true  ######################################################### shell scripts with conditional selection      Simple shell scripts are generally competent for tasks that do not contain variables. However, when you perform some decision-making tasks, you need to include the if/then criteria to judge. Shell scripting supports such operations, including comparison operations, determining whether a file exists, and so on. The basic if Condition command options are:-eq-compare two parameters for equality (for example, if [2–eq 5])-ne-Compare two parameters are unequal-lt-parameter 1 is less than parameter 2-le-parameter 1 is less than equals parameter 2-gt-parameter 1 is greater than parameter 2-ge-parameter 1 is Greater than or equal to the parameter 2-f-checks if a file exists (for example, if [-F "filename"]) the D-Check directory exists almost all of the judgments can be implemented with 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/shtoday= ' date-d yesterday +%y%m %d ' file= ' aPache_$today.tar.gz "Cd/home/chenshuo/shellif [-F" $file "];thenecho" OK "Elseecho" error $file ">error.logmail-s" FA Il backup from Test "[email protected] <error.logfi=============================================

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

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

PS Axu |      grep "httpd" |      Grep-v "grep" | Wc-l
All processes--get rows containing "httpd"---delete grep process information--Output last line count

You can tell whether a process exists by judging whether the command 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

Use a shell script to monitor whether a process exists or not, the instance that is started comes with an if judgment detail condition

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.