Use shell script to monitor whether the process exists. If it does not exist, the instance will be started. First, the code will be edited:
#!/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, which can query the feature string of a unique process
0 means existence
$? -ne 0 does not exist, $? -eq 0 does exist
Timed execution:
crontab -e
0 04,12,21 * * * /bin/bash /home/mysh/
Test every day at 4pm, 12pm, and 21pm
0 4,12 * * *
minute day month week
* 04,12 * * * This means that it is executed every minute between 4 and 12 o'clock
0 4,12 * * * 4:12:00
Expand relevant knowledge:
If else syntax of shell and logical expressions greater than, less than, etc.:
if ....; then
....
elif ....; then
....
else
....
fi
In most cases, the conditions can be tested using the test command. For example, you can compare strings, determine whether the file exists and is readable, etc.... "[ ] " is usually used to represent conditional tests. It is important to note the spaces here. Make sure the square brackets are spaced.
[ -f "somefile" ]: determine whether it is a file
[ -x "/bin/ls" ]: determine whether /bin/ls exists and has executable permissions
[ -n "$var" ]: determine whether the $var variable has a value
[ "$a" = "$b" ]: Determine whether $a and $b are equal. -r file. User reads as true.
-w file User can write as true
-x file User executable as true
-f file The file is a regular file is true
-d file The file is a directory and is true
-c file The file is a character special file is true
-b file The file is a block special file is true
-s file When the file size is not 0, it is true
-t file is 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, when performing some decision tasks, you need to include if/then conditional judgment. Shell script programming supports such operations, including comparison operations, judging whether the file exists, etc. The basic if conditional command options are: - eq — compare whether the two parameters are equal (for example, if [ 2 –eq 5 ])
-ne —Compare whether the two parameters are not equal
-lt —Is parameter 1 less than parameter 2
-le —Is parameter 1 less than or equal to parameter 2
-gt —Is parameter 1 greater than parameter 2
-ge —Is parameter 1 greater than or equal to parameter 2
-f — Check if a file exists (for example, if [ -f "filename" ])
-d — Check whether the directory exists
Almost all judgments can be implemented using these comparison operators. The commonly used -f command option in scripts checks whether a file exists before executing it.
Determine whether the file exists
#!/bin/sh today=`date -d yesterday +%y%m%d` file="apache_$" cd /home/chenshuo/shell if [ -f "$file" ];then echo "OK" else echo "error $file" > mail -s "fail backup from test" chenshuo@ < fi
Basic shell command
(1) ps aux Display all the processes of the system, one line per line
(2) grep "abc" reads the character stream from standard input and outputs the line containing the string "abc"
(3) grep -v "acb" Read the character stream from standard input and output the line that does not contain the string "abc"
(4) wc -l Read the character stream from standard input and output the number of lines
For example, it is necessary to detect whether the process httpd exists, and the operation process is as follows:
(1) Read all processes in the system
(2) Determine whether the information containing the specified process name exists
Connect through pipeline, the command is as follows:
ps axu | grep "httpd" | grep -v "grep" | wc -l
All processes --> Get the rows containing "httpd" --> Delete grep process information --> Output the last row count
By determining whether the execution result of the command is 0, you can know whether the process exists.
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