---
title: "如何保证sh脚本只有一个进程在运行"
author: "jacky.jihao"
date: "2009-03-18T14:52:47Z"
tags: []
slug: "sh"
draft: false
lastmod: "2009-03-18T14:53:17Z"
id: 350933
url: "https://jihao.iteye.com/blog/350933"
---

```
#!/usr/bin/sh

LOCKFILE=jihao.lock
if [ -f ${LOCKFILE} ] && kill -0 `cat ${LOCKFILE}`; then
    echo "filecollector is already running"
    exit
fi
 
# make sure the lockfile is removed when we exit and then claim it
trap "rm -f ${LOCKFILE}; exit" INT TERM EXIT
echo $$ > ${LOCKFILE}

 
 
sleep 100
rm -f ${LOCKFILE}
```

 

BTW: [http://stackoverflow.com/](http://stackoverflow.com/) 很好很实用