Cheap VPS & Xen Server

Residential Proxy Network - Hourly & Monthly Packages

Simple Bash Script To Work As A Daemon


If you need some snippets or codes to run for ever, but not more that one instance, you need to rapidly check the code, or script and if it has died! It’s the time to run it again.

To be sure of this function, just add the following lock handler at the top of your script (before the main script functionality):

#!/bin/bash

######### Lock checker ###########

myName="`echo $0 | awk '{print $NF}' FS='/'`"

lockDir="/var/lock/"

lockFile=$lockDir$myName.pid

currentPID=$$

oldPID="`cat $lockFile`"

oldderExist=` kill -0 $oldPID 2>/dev/null ; echo $? `

if [ "$oldderExist" == "0" ]; then echo "An Other Instance Is Running...! PID:$oldPID" ; exit ; else echo $currentPID > $lockFile ; fi

######### Lock checker ###########

Comments

comments