确保一次只运行一个shell脚本的快速和肮脏的方法确保在给定时间只运行一个shell脚本的快速和肮脏的方法是什么?
3 回答
当年话下
TA贡献1890条经验 获得超9个赞
LOCKFILE=/tmp/lock.txtif [ -e ${LOCKFILE} ] && kill -0 `cat ${LOCKFILE}`; then echo "already running" exitfi# make sure the lockfile is removed when we exit and then claim ittrap "rm -f ${LOCKFILE}; exit" INT TERM EXIT echo $$ > ${LOCKFILE}# do stuffsleep 1000rm -f ${LOCKFILE}
kill -0
trap
kill -9
).
收到一只叮咚
TA贡献1821条经验 获得超4个赞
flock(1)
#!/bin/bash( # Wait for lock on /var/lock/.myscript.exclusivelock (fd 200) for 10 seconds flock -x -w 10 200 || exit 1 # Do stuff) 200>/var/lock/.myscript.exclusivelock
(
)
util-linux
添加回答
举报
0/150
提交
取消