Simple Bash Script To Work As A Daemon
Want to support HowtoForge? Become a subscriber!
Simple Bash Script To Work As A DaemonIf 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 ###########
|



Recent comments
1 hour 48 min ago
2 hours 48 min ago
6 hours 35 min ago
7 hours 49 min ago
11 hours 25 min ago
18 hours 40 min ago
1 day 3 hours ago
1 day 5 hours ago
1 day 20 hours ago
1 day 22 hours ago