Turn-shell Implementation Daemon

Source: Internet
Author: User

# #! There is a space followed by a space followed by the full path of the interpreter and must be correct.
#! /bin/ash

Pro_path= ""
# Testpro is an executable program to be guarded, which guarantees it is always running
Program= "Testpro"

# This script keeps looping,while< conditions > Do on one line to add a semicolon after the condition
# keywords or commands such as if, then, while, and do are the beginning of a new expression,
# The expression before a new expression must end with a newline character or a semicolon (;)
#如果条件不是单个常量或变量而是表达式的话, you need to enclose it in [].
The #while, until, and for loops both start with do to form the loop body as done
While true; Do
#休息10秒以确保要看护的程序运行起来了, this time depends on the actual situation

Sleep 10
The $ and \ characters in #单引号 ' are not referenced and escaped, but in double quotes "" is OK!
#单引号中如果还有单引号, all the single quotes are stripped out and the contents of the single quotation mark are output as they are.
# example: Echo ' has ' test '--and have test
# PS aux-->a to show other user-initiated processes;
# U is the user name and time to display the START process;
# x is the process for displaying the system itself;
# PS aux | grep executable name--in the resulting all process information text that is currently started,
# filter text lines that contain the specified text (which executes the program name)
# Note: Suppose PS aux | The grep executable name has output, but the output is not a letter but two,
A line of information text that #一个为查找到的包含有指定文本 (the name of the program can be executed) (a line of text ending with a newline character 0x10),
# One for the grep executable name, you lose yourself too,
# This message is not needed because we just want to know if the executable program that specified the name started
# GREP-V Specifies text-to-output line text information that does not contain the specified text
# wc-l The number of rows in the output file (------and output line break statistics)
# PS aux | grep $PROGRAM |grep-v grep | Wc-l---If a program with the specified program name starts, the result is greater than
pro_now= ' ps aux | grep $PROGRAM | grep-v grep | wc-l '

# Integer comparison:-lt, less than,-le, less than equals,-GT-greater than,-ge-> greater than equals,-eq, equals,-ne-> not equal to
# if [condition] and then put on one line to add a semicolon after the condition
#如果当前指定程序启动的个数小于壹的话
if [$PRO _now-lt 1]; Then
# 0, standard input,1-> standard output, 2-> standard error message output
#/dev/null-->linux's special file, it's like a bottomless pit, all the information redirected to it will disappear!
# 2->/dev/null redirect stderr to/dev/null,1>& 2-, redirect stdout to stderr,
#Starts the specified program directly, andDo not display any output
# The executable program is followed by a space plus a &amp, indicating that the program to be executed is running in the background
./$PROGRAM 2>/dev/null 1>&2&
# date >>./tinfo.log-directional output current datetime to file, add to end of file, if no file, create this file
Date>>./tinfo.log
#echo "Test Start" >> ./tinfo.log--> directed output test start added to end of file
echo "Test Start" >>.Tinfo. Log
# If Branch structure ends
fi

# basically the same as above, is more than a grept, the result is to filter out the information line containing T characters
#--and process stopped, D--and non-disruptive deep sleep, R--process running or ready,s--> to receive signal sleep,
#---has completely died, Z--has completely terminated
pro_stat= ' PS aux|grep $PROGRAM |grep t|grep-vgrep|wc-l '

#如果指定进程状态为已停止的信息大于零的话
if [$PRO _stat-gt 0]; Then
# killall--> to kill the process by name, -9--To send the program a signal value of 9, namely Sigkill (illegal hardware instructions)
#也可以不指定信号, the default is Sigterm, which is a signal value of 15
       killall-9 $PROGRAM
       Sleep 2
       ./$PROGRAM 2>/dev/null 1>&2&
       Date>>./tinfo.log
echo "Test Start" >>./tinfo. Log
fi
The #while, until, and for loops both start with do to form the loop body as done
Done
# exit is used to end the script and return the status value, 0-for success, not 0 for the error code, the value range is 0 ~ 255.
Exit 0

Turn-shell Implementation Daemon

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.