一个思路是在script初期检测系统中是否存在同名进程。
if [ `ps -ef | grep "test.sh" | grep -v "grep" | wc -l` -gt 2 ] then echo "This script is already running. Exit." else while true do echo "I am running" sleep 5 done fi
如果是第一次运行,那么首行语句检测到的同名进程会有两个:第一个是启动这个script的进程,第二个是反引号内的命令。