我正在尝试捕获退出(ctrl + c)信号以执行一些清理活动。
在shell脚本中,我跟随波纹管功能进行陷阱,
#To trap ctrl-c signals
trap ctrl_c INT
#To trap exit signals
trap on_exit EXIT
function ctrl_c() {
echo "exited by user"
exit
}
function on_exit() {
echo "exited by user"
exit
}
我如何在Makefile中实现相同目标?
答案 0 :(得分:1)
您不能在信号中捕获信号。尝试使用.INTERMEDIATE
进行清理并阅读5.6 Interrupting or Killing make
all: test.out
test.out: test.tmp
sleep 10
test.tmp:
echo x>test.tmp
.PHONY: all
.INTERMEDIATE: test.tmp
Make捕获中断信号并删除这些文件(如果它们是图形的一部分,并且在启动之前不存在):
$ make
echo x>test.tmp
sleep 10
make: *** [Makefile:4: test.out] Interrupt
make: *** Deleting intermediate file 'test.tmp'
如果真正需要在make退出时执行某些程序,请在初始化某些变量时生成一个后台进程,并让它等待主make进程终止:
DUMMY := $(info launching the moniror process...)$(shell some-command& )
答案 1 :(得分:1)
可以显式调用bash解释器并在那里定义陷阱:
test.out:
bash -c "trap 'rm test.tmp' EXIT; \
echo x>test.tmp; \
cp test.tmp test.out"