echo "! Waiting for ping process to exit" wait $pid echo "! Ping process exited or shell interrupted"
if ps -p $pid >/dev/null; then echo "! process ${pid} still alive, try kill it" kill -2 $pid fi wait $pid echo "! Ping process exited or shell interrupted" }
go | tee -i /tmp/test_log.txt
这个脚本我们暂时取名为test.sh,我遇到的问题就是,这个程序在Terminal运行的过程中,Ctrl + C 可以触发进程正常的退出,但是我的Python程序中,给我启动的子进程发 SIGINT却没有任何反应。为了解释其中的原理,需要了解一些容易被忽略的基础原理。
1. 当你按 Ctrl + C 时,究竟发生了什么
在一般的认知中,Ctrl + C 等同于给终端的前台进程发送 SIGINT 信号,但是精确来说,其实是给前台进程组(Foreground Process Group) 中的所有进程发送 SIGINT 信号 。而前台进程组可以理解为在当前Termnial可以接受你键盘交互的进程,以及由它启动的所有子进程。
When the shell is interrupted, it will wait for the running command to exit. If this child’s status indicates it exited abnormally due to that signal, the shell cleans up, removes its signal handler, and kills itself again to trigger the OS default action (abnormal exit). Alternatively, it runs the script’s signal handler as set with trap, and continues.
If the shell is interrupted and the child’s status says it exited normally, then Bash assumes the child handled the signal and did something useful, so it continues executing. Ping and top both trap SIGINT and exit normally, which is why Ctrl-C doesn’t kill the loop when calling them.
CyberDog在小米发布会上作为“One More Thing” 亮相之后,当时即圈粉无数。作为一个机器人行业从业者,对四足机器人并不感到十分新奇。但是CyberDog完成度极高的工业设计,几乎没有妥协的硬件平台,相对业界如此低廉的价格,和致力构建开源社区的态度,还是让我感到非常的激动。于是我抱着试一试的态度提交了申请资料,然后在埋头工作的某一天,被电话通知自己成为了首批CyberDog众测的体验用户。
1. Lucky dog meet CyberDog - 初见铁蛋
Lucky Dog 是我,因为抽中CyberDog是我久违的觉得幸运和喜悦的时刻。从下单那刻起,我不停的刷新着物流进度,期待着狗蛋的到来。而我对于机器人开箱的幻想,一直来源于一部电影《机器管家》,机器人Andrew能自己睁开眼睛,并且从包装盒里走出来。