Makefile学习笔记13|使用makefile生成后台启动和停止的脚本
希望看到这篇文章的朋友能在评论区留下宝贵的建议来让我们共同成长,谢谢。
这里是目录
本节将为上一节的makefile编写两个脚本,分别是后台启动脚本和停止脚本,并且将这两个脚本也拷贝到/usr/bin/目录下。
后台启动脚本
我们给后台启动脚本命名为start_test_install。其内容就是执行命令nohup test_install 3 &
,其中的nohup是不打印输出信息的意思。
# test_install/makefile
TARGET=test_install
OBJS=test_install.o
CC:=g++
OUT=/usr
STARTSH=start_$(TARGET)
# $(1) $(TARGET); $(2) $(OUT)
# $(3) para1; $(4) para2; ....
define Install
@echo "begin install "$(1)
-mkdir -p $(2)
cp $(1) $(2)
@echo "begin make start shell"
echo "nohup $(1) $(3) &" > $(STARTSH)
chmod +x $(STARTSH)
cp $(STARTSH) $(2)
@echo "end make start shell"
@echo $(1) "install success!"
endef
$(TARGET):$(OBJS)
install:$(TARGET)
$(call Install,$(TARGET),$(OUT)/bin/,5)
.PHONY:install
测试一下。输入命令make install
,生成脚本start_test_install,可以输入命令cat start_test_install
看一下它的内容是不是nohup test_install 5 &。然后输入命令start_test_install
运行一下后台启动脚本,再输入命令ps -ef|grep test_install
看一下进程的信息。输出信息会保存在nohup.out文件中,nohup.out文件的内容可以通过命令tail -f nohup.out
查看。
停止脚本
停止脚本名为stop_test_install,要做的事情是把进程停止掉。其内容就是执行命令killall test_install
,直接结束进程。
# test_install/makefile
TARGET=test_install
OBJS=test_install.o
CC:=g++
OUT=/usr
STARTSH=start_$(TARGET)
STOPSH=stop_$(TARGET)
# $(1) $(TARGET); $(2) $(OUT)
# $(3) para1; $(4) para2; ....
define Install
@echo "begin install "$(1)
-mkdir -p $(2)
cp $(1) $(2)
@echo "begin make start shell"
echo "nohup $(1) $(3) &" > $(STARTSH)
chmod +x $(STARTSH)
cp $(STARTSH) $(2)
@echo "end make start shell"
@echo "begin make stop shell"
echo "killall $(1)" > $(STOPSH)
chmod +x $(STOPSH)
cp $(STARTSH) $(2)
@echo "end make stop shell"
@echo $(1) "install success!"
endef
$(TARGET):$(OBJS)
install:$(TARGET)
$(call Install,$(TARGET),$(OUT)/bin/,5)
.PHONY:install
这里声明两个变量STARTSH和STOPSH是为了方便后面uninstall的编写。
测试一下。输入命令make install
,生成脚本start_test_install与stop_test_install。先输入命令start_test_install
启动进程test_install,再输入命令ps -ef|grep test_install
看一下进程是否存在。最后输入命令stop_test_install
结束进程test_install,此时输入命令ps -ef|grep test_install
就查不到进程test_install了。
都看到这里了,可以给个点赞或者评论吗?达瓦里希( ̄^ ̄)ゞ