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

How to use shell scripts to monitor whether a process exists and start if it does not exist

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

Share

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

Xiaobian to share with you how to use shell scripts to monitor whether the process exists if it does not exist to start, I hope you have something to gain after reading this article, let us discuss it together!

Use shell script to monitor whether the process exists or not. If it does not exist, it will start the instance. First, the code dry goods:

#!/ bin/shps -fe|grep processString |grep -v grepif [ $? -ne 0 ]thenecho "start process..... "elseecho "runing..... "fi

#####

processString represents the process characteristic string, which can query the unique process characteristic string.

0 indicates existence

$? No, no. 0 doesn't exist. -eq 0 exists

Timed execution:

crontab -e

0 04,12,21 * * * /bin/bash /home/mysh/monitorprocess.sh

4:00 p.m., 12:00 p.m., 21:00 p.m.

0 4,12 * * *

Time-sharing day month week

* 04,12 * * * This is performed every minute between 4:00 and 12:00

0 4,12 * * * 4:12:00 0 0 minute execution once

Expand relevant knowledge:

shell if else syntax and logical expressions such as greater than, less than, etc.:

if ....; then

....

elif ....; then

....

else

....

fi

In most cases, you can use the test command to test conditions. For example, you can compare strings, determine whether files exist and whether they are readable, etc. A conditional test is usually denoted by " [ ]." Note that the spaces here are important. Make sure the square brackets are blank.

[ -f "somefile" ]: Determine if it is a file

[ -x "/bin/ls" ]: Determine whether/bin/ls exists and has executable permissions

[ -n "$var" ]: Determine if $var variable has value

[ "$a" = "$b" ]: Determine if $a and $b are equal-r file User readable as true

-w file User can write true

-x file User executable is true

-f file File is regular File is true

-d file File is directory is true

-c file File is character Special file is true

-b file file is block special file is true

-s file True if file size is non-zero

-t file True when the device specified by the file descriptor (default 1) is terminal

#########################################################

Simple shell scripts are generally adequate for tasks without variables. However, when performing some decision tasks, you need to include if/then conditional judgments. Shell script programming supports such operations, including comparison operations and determining whether files exist. Basic if conditional command options are: - eq -Compare two parameters for equality (e.g. if [ 2 -eq 5 ])

-ne -compares two parameters to see if they are unequal

-lt -Is parameter 1 less than parameter 2

-le -whether parameter 1 is less than or equal to parameter 2

-gt -Is parameter 1 greater than parameter 2

-ge -whether parameter 1 is greater than or equal to parameter 2

-f -checks if a file exists (e.g. if [ -f "filename" ])

-d -Check if directory exists

Almost all judgments can be made using these comparison operators. The-f command option is commonly used in scripts to check for the existence of a file before executing it.

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 "fail backup from test" chenshuo@soufun.com Get the line containing "httpd"--> Delete grep process information--> Output the last number of lines

By judging whether the execution result of the command is 0, you can know whether the process exists.

The script is as follows:

#!/ bin/shwhile true;do count=`ps -ef|grep http|grep -v grep` if [ "$? " != "0" ];thenecho ">>>>no httpd,run it"service httpd startelseecho ">>>>httpd is runing... "fisleep 5done After reading this article, I believe you have a certain understanding of" how to use shell scripts to monitor whether a process exists or not. If it does not exist, start it."If you want to know more about relevant knowledge, please pay attention to the industry information channel. Thank you for reading!

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