一键自动往crontab添加条目脚本

本文发布时间: 2019-Mar-22
脚本的优点:只要将 监控脚本 被监控程序 和重启脚本放在同一个目录下,并将被监控程序运行起来,即可自动添加到crontab中,建议配置mooon提供的process_monitor.sh一起使用。#!/bin/sh# writed by yijian on 2013/1/19# generic script used to install an iterm into crontab# http://code.google.com/p/mooon/source/browse/trunk/common_library/shell/install_crontab.sh# $monitor_name used to monitor the process named by $process_name,# if $process_name is hangup, $monitor_name will reboot it by $start_name.# before using $monitor_name, $process_name should be running,# the installation will get the path information automatically.# **PLEASE NOTE**: all files must be placed in the same directory.process_name=$1 # the fullname of process monitored (but not included directory)monitor_name=$2 # the path of monitor script (maybe included directory)start_name=$3 # the path of start script (maybe included directory)timedate_fields=$4 # five time and date fieldsme=`whoami`# check cmdlineif test $# -ne 4; thenecho "usage: process_name monitor_name start_name time_date"echo "EXAMPLE1: $0 mooon process_monitor.sh start_test.sh "* * * * *""echo "EXAMPLE2: $0 mooon /tmp/process_monitor.sh /tmp/start_test.sh "* * * * *""echo "monitor_name or start_name must be in the same directory with process_name, "echo "if monitor_name or start_name don't include directory"exit 1fi# get pid & usernameeval $(ps -C $process_name h -o pid,euser | awk -F" " '{ printf("pid=%snuser=%sn",$1,$2); }')if test $? -ne 0; thenecho "$process_name NOT RUNNING now"exit 1fiif test "X$me" != "X$user"; thenecho "$process_name NOT RUNNING now"exit 1fi# get file pathfilepath=`readlink /proc/$pid/exe`if test $? -ne 0; thenexit 1fi# get directory pathbinpath=`dirname $filepath`# check filesif test -x $monitor_name; thenmonitor_path=$monitor_nameelsemonitor_path=$binpath/$monitor_namefiif test -x $start_name; thenstart_path=$start_nameelsestart_path=$binpath/$start_namefiif test ! -x $monitor_path; thenecho "$monitor_path not exist"exit 1fiif test ! -x $start_path; thenecho "$start_path not exist"exit 1fi# install crontab if not existcrontab -l | grep "$start_path" > /dev/null 2>&1if test $? -ne 0; thencrontab -l > crontab.tmpecho -e "n# the following monitor added by `basename $0`($process_name) on `date +"%Y-%m-%d %H:%M:%S"`" >> crontab.tmpecho "$timedate_fields $monitor_path $process_name $start_path" >> crontab.tmpcrontab crontab.tmpif test $? -ne 0; thenexit 1firm crontab.tmpfiexit 0


(以上内容不代表本站观点。)
---------------------------------
本网站以及域名有仲裁协议。
本網站以及域名有仲裁協議。

2019-May-25 12:58pm
栏目列表