该脚本旨在在 5 秒后自杀。但它不会在 5s 后杀死脚本,也不会调用清理。重要的是我想获得i
它何时停止的价值。
#!/bin/sh
trap "cleanup" TERM
#call the cleanup when recive TERM
mainpid=$$
#script's pid
cleanup()
{
echo "cleaup called"
echo "i=$i when it is stopped "
exit 1
}
(sleep 5 && echo "timeout";kill -TERM $mainpid) &
#after 5s it should kill the script
run_test()
{
i=1
sleep 100
i=$$(i+1)
}
run_test 2>&1 > x.log