Objective
We may need to run a service on a Linux host, and if it shuts down or kills, the service stops, which affects the daily tasks. For example, a BI project database extraction, using TASKCTL scheduling, the daily fixed time for data extraction, if the host TASKCTL related service process is closed, it will affect the data extraction!
Scheduled Tasks (scheduled)
In Linux, the tasks performed by a cycle are typically cron
handled by this daemon [ps -ef|grep cron]
. cron
reads one or more configuration files that contain the command line and its invocation time.
cron
The configuration file is called "crontab" and is shorthand for "cron table".
The following is a brief talk about crontab content.
Classification
The task scheduling under Linux is divided into two categories: system task scheduling and user task scheduling.
System Task Scheduling : The work to be performed by the system periodically, such as writing cache data to hard disk, log cleanup, etc. In the/etc directory there is a crontab file, this is the System Task Scheduler configuration file.
The/etc/crontab file includes the following lines:
SHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=""HOME=/# run-parts51 * * * * root run-parts /etc/cron.hourly24 7 * * * root run-parts /etc/cron.daily22 4 * * 0 root run-parts /etc/cron.weekly42 4 1 * * root run-parts /etc/cron.monthly
The first four rows are the environment variables that are used to configure the Crond task to run, the shell variable specifies which shell the system will use, this is bash, and the second line of the path variable specifies the path to the System execution command. The third line of the mailto variable specifies that Crond's task execution information will be emailed to the root user, and if the value of the mailto variable is null, the task execution information is not sent to the user, and the home variable in line fourth specifies the home directory to use when executing the command or script.
User Task Scheduling : Users to perform regular work, such as user data backup, scheduled email reminders and so on. Users can use the Crontab tool to customize their own scheduled tasks. All user-defined crontab files are saved in the /var/spool/cron
directory. Its file name is the same as the user name, and the consumer permissions file is as follows:
/etc/cron.deny
The users listed in this file are not allowed to use the crontab command
/etc/cron.allow
Users listed in this file are allowed to use the crontab command
/var/spool/cron/
directory where all user crontab files are stored, named by user name
Create a task
crontab -e
, then there will be a VI editing interface, and then enter a certain format of content into the inside: Wq save exit, that is, create a timed task.
The contents of this format include six fields, where the first five fields are the time the specified command was executed, and the last field is the command to be executed.
Spaces or tabs are used to separate each field. The format is as follows:
day-of-month month-of-year day-of-week commands
corresponding to the legal 00-59
value 00-23
01-31
01-12
0-6 (0 is sunday)
commands(代表要执行的脚本)
,,,,,
In addition to the numbers there are several special symbols that are asterisks "*"
, slashes "/"
, dashes "-"
, commas ","
.
*
Represents all the numbers in the range of values,
/
Represent each of the 5 units of meaning, /5
-
Represents a number to a number,
,
Separate several discrete numbers.
For example: 0 0 * * * sh /home/sh/monitor.sh
This script is executed 0:0 A.M. every day monitor.sh.
More information can be found in: Add timed tasks under Linux
Crond Service-related commands
/sbin/service crond start //启动服务/sbin/service crond stop //关闭服务/sbin/service crond restart //重启服务/sbin/service crond reload //重新载入配置/sbin/service crond status //查看服务状态
To see if the Crontab service is set to boot, execute command: ntsysv
This command needs to be the root user.
Add to boot auto start: chkconfig --level 5 crond on
View later:chkconfig --list|grep crond
Whether the listener service process exists
On the top of the scheduled task, a fixed time each day to execute the shell script, then this script we can write on listening to the content of the service process.
monitor.sh
Here is an example of Taskctl related services :
#!/bin/shNOWTIME=$(date +%Y%m%d_%H%M%S)ps -fe|grep emnls|grep -v grepif [ $? -ne 0 ]thenecho $NOWTIME" stopped.....">>/home/mars/checkprolog.txteminitctlinitctlstartelseecho $NOWTIME " running....." >>/home/mars/checkprolog.txtfi
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
Which, NOWTIME=$(date +%Y%m%d_%H%M%S)
get the current system time, nothing to say, notice that there is a space behind the date ! emnls
the name of the TASKCTL core service process. $? -ne 0
does not exist, $? -eq 0
exists.
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 to make sure that the square brackets are blank spaces.
[ -f "somefile" ]
: Determine if it is a file
[ -x "/bin/ls" ]
: Determine /bin/ls
If there are any and executable permissions
[ -n "$var" ]
: Determines $var
whether a variable has a value
[ "$a" = "$b" ]
: Judging $a
and $b
whether equal
The judgment of a file:
-r file 用户可读为真-w file 用户可写为真-x file 用户可执行为真-f file 文件为正规文件为真-d file 文件为目录为真-c file 文件为字符特殊文件为真-b file 文件为块特殊文件为真-s file 文件大小非0时为真-t file 当文件描述符(默认为1)指定的设备为终端时为真
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 —比较两个参数是否相等
(for example, if [2–eq 5])
-ne —比较两个参数是否不相等-lt —参数1是否小于参数2-le —参数1是否小于等于参数2-gt —参数1是否大于参数2-ge —参数1是否大于等于参数2-f — 检查某文件是否存在(例如,if [ -f "filename" ])-d — 检查目录是否存在
Almost all 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.
Reference article : Use a shell script to monitor if a process exists that does not exist the instance that is started
Whether the Linux listener process exists and joins a timed task