shell script : stop program

来源:互联网 发布:软件项目招标评分标准 编辑:程序博客网 时间:2024/06/09 21:00

今天同事让我写一个停止自己工程的脚本.
写了一会,他说发现已经有这个脚本了,不跟我玩了。
回来自己将这个实验做完。
测试程序:根据退出标记文件,进行安全退出。
脚本:先做出退出标记文件,睡眠,如果程序还不退出,杀掉。

实验

实验环境:debian7.5

实验工程下载点

linux_shell_script_to_stop_prog.zip

实验工程

// @file main.cpp// @brief test prog to quit or be kill#include <stdlib.h>#include <stdio.h>#include <unistd.h>// shell script gen file : touch /tmp/cmd_quit.flag#define EXIST_FLAG_FILE_NAME "/tmp/cmd_quit.flag"bool is_file_exist(const char* psz_file_pathname);int main(int argc, char* argv[]){    long l_cnt = 0;    printf("prog was running\n");    do {        sleep(2);        printf("%8.8ld runing ...\n", ++l_cnt);    } while (!is_file_exist(EXIST_FLAG_FILE_NAME));    printf("THE END\n");    return EXIT_SUCCESS; // EXIT_SUCCESS or EXIT_FAILURE}bool is_file_exist(const char* psz_file_pathname) {    if (NULL ==  psz_file_pathname) {        return false;    }    if (0 == access(psz_file_pathname, F_OK)) {         return true;    }     else {         return false;    }}

实验工程的Makefile

# ==============================================================================# @file Makefile# @brief#   lostspeed 2017-07-10#   for \my_script\stop_prog\test_prog# @note#   必须在Makefile目录下新建至少一个子目录!, 否则产生的SUBOBJ和ROOTOBJ有重名,导致编译失败#   为了不建立无意义的文件夹(e.g. 只有一个主文件main.cpp), 在Makefile统计目录建立一个文件夹main#   将主文件main.cpp放到main中,就可以编译过了##   从博客上回帖的Makefile内容,每行前面的\t都变成空格了,bash不认,杯具啊## ==============================================================================MAKE_VER = ./main/Makefile 1.0.0.1 build 2017-07-10 23:15BIN = test_prog_to_endTARGETS = ${BIN}CC = g++CFLAGS = -Wall \    --std=c++11 \    -gINC_PATH = -I.# 要包含的库(.a, .o), 要写上相对路径或绝对路径,而不能让编译器去到库路径中去找MY_LIBS =PRJ_LIBS = ${MY_LIBS}LIB_LINK_OPT = -lstdc++ -pthread -lpthread -lrt -ldlSUBDIR = $(shell ls -d */)ROOTSRC = $(wildcard *.cpp)ROOTOBJ = $(ROOTSRC:.cpp=.o)SUBSRC = $(shell find $(SUBDIR) -name '*.cpp')SUBOBJ = $(SUBSRC:.cpp=.o)help:    @echo make help    @echo "command list:"    @echo   make clean    @echo   make all    @echo   make rebuild    @echo TARGETS = ${TARGETS}show_version:    @echo ================================================================================    @echo ${MAKE_VER}    @echo ================================================================================    ls -l -p --time-style="+%Y-%m-%d %H:%M:%S" $(find `pwd`)    @echo --------------------------------------------------------------------------------clean:    clear    make show_version    @echo ================================================================================    @echo make clean    @echo BIN = $(BIN)    @echo ROOTOBJ = $(ROOTOBJ)    @echo SUBOBJ = $(SUBOBJ)    @echo ================================================================================    rm -f $(BIN) $(ROOTOBJ) $(SUBOBJ)all: ${TARGETS}    make show_version    @echo ================================================================================    if [ -f $(BIN) ] ; \    then \        echo "build ok :)" ; \    else \        echo "build failed :(" ; \    fi;    @echo ================================================================================$(BIN): $(ROOTOBJ) $(SUBOBJ)    ${CC} ${CFLAGS} ${INC_PATH} \    $(ROOTOBJ) $(SUBOBJ) \    ${PRJ_LIBS} ${LIB_LINK_OPT} \    -o ${BIN}.cpp.o:# $^ is main/main.cpp# $< is main/main.cpp# $@ is main/main.o    @echo $<    @echo build $^ ...    ${CC} ${CFLAGS} ${INC_PATH} -c $^ -o $@rebuild:    make clean    make all

停止程序的脚本

#!/bin/bash# # @file stop_test_prog_to_end.sh# shell script to stop test_prog_to_endPROG_NAME="test_prog_to_end"cmd_quit_flag_file="/tmp/cmd_quit.flag"pid=""let "prog_is_run=0"let "prog_is_runing=0"function sh_main {    clear    ps aux |grep $PROG_NAME    fn_get_pid $PROG_NAME    if [ "$pid" == "" ] ; \    then \        let "prog_is_run=0" ; \        echo "*prog not run, needn't to stop :)" ; \    else \        let "prog_is_run=1" ; \        echo "*prog was run, stop now, please wait..." ; \        fn_kill_proc $PROG_NAME $pid ; \    fi;    if [ 1 = $prog_is_run ] ; \    then \        fn_check_prog_is_stop_ok_and_show_result PROG_NAME; \    fi;    fn_rm_file "$cmd_quit_flag_file"}function fn_touch_file {    if [ ! -f $1 ]; then        touch $1    else        echo "found $1, needn't touch"    fi}function fn_rm_file {    if [ ! -f $1 ]; then        echo "not found $1, needn't rm"    else        rm $1    fi}function fn_check_prog_is_stop_ok_and_show_result {    fn_get_pid $1 ; \    if [ -z pid ] ; \    then \        echo "**prog not run, stop success :)" ; \    else \        if [ "$pid" != "" ] ; \        then \            echo "**prog was run, stop failed, sorry :(" ; \        else \            echo "**prog not run, stop ok :)" ; \        fi;    fi;}function fn_get_pid {    pid=$(pidof $1)    echo "$1 's PID = [$pid]"}function fn_notify_prog_to_quit {    # some notify action    fn_touch_file "$cmd_quit_flag_file"    # wait prog was quit    # now sleep a while    sleep $1}# fn_kill_proc $PROG_NAME $pidfunction fn_kill_proc {    fn_notify_prog_to_quit 3    fn_get_pid $1 ; \    if [ -z pid ] ; \    then \        echo "prog not run, needn't kill :)" ; \    else \        if [ "$pid" != "" ] ; \        then \            echo "prog was run, will be force killed, please wait a moment..." ; \            # if prog not end after notify, kill it force            kill -9 $2 ; \            echo "was killed $1" ; \        else \            echo "*prog not run, needn't kill :)" ; \        fi;    fi;}sh_main "$@"# ================================================================================# run result for killed# ================================================================================# root       6211  0.0  0.0  18720   884 pts/1    S+   01:35   0:00 ./test_prog_to_end# root       6212  0.0  0.0  11152  1504 pts/2    S+   01:36   0:00 /bin/bash ./stop_test_prog_to_end.sh# root       6215  0.0  0.0   8196   892 pts/2    S+   01:36   0:00 grep test_prog_to_end# test_prog_to_end 's PID = [6211]# *prog was run, stop now, please wait...# test_prog_to_end 's PID = [6211]# prog was run, will be force killed, please wait a moment...# was killed test_prog_to_end# PROG_NAME 's PID = []# **prog not run, stop ok :)# ================================================================================# run result for stop for safe quit# ================================================================================# root       6286  0.0  0.0  18720   884 pts/1    S+   01:39   0:00 ./test_prog_to_end# root       6287  0.0  0.0  11152  1504 pts/2    S+   01:39   0:00 /bin/bash ./stop_test_prog_to_end.sh# root       6290  0.0  0.0   8196   888 pts/2    S+   01:39   0:00 grep test_prog_to_end# test_prog_to_end 's PID = [6286]# *prog was run, stop now, please wait...# test_prog_to_end 's PID = []# *prog not run, needn't kill :)# PROG_NAME 's PID = []# **prog not run, stop ok :)
原创粉丝点击