Created
January 10, 2012 07:38
-
-
Save binux/1587684 to your computer and use it in GitHub Desktop.
防止多重启动的守护进程
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
if [ ! $STY ]; then | |
screen -S $(basename $0 .sh) -U -d -m $0 $* | |
exit 0 | |
fi | |
if [ ! $LOCK ]; then | |
LOCK=$(echo $0 $* | md5sum | cut -f 1 -d " ").lock | |
fi | |
LOCK=~/$LOCK | |
_cleanup () { | |
cleanup | |
[ $(jobs -p) ] && kill $(jobs -p) | |
[ $(jobs -p) ] && sleep 1 | |
[ $(jobs -p) ] && sleep 5 | |
[ $(jobs -p) ] && kill -9 $(jobs -p) | |
rm -f $LOCK | |
exit 0 | |
} | |
if [ -f $LOCK ]; then | |
LOCK_PID=$(cut -f 1 -d " " $LOCK) | |
kill -0 $LOCK_PID && echo "$(date "+%d/%b/%Y:%H:%M:%S") still got $LOCK" || rm -f $LOCK | |
exit 1 | |
fi | |
trap '_cleanup' 0 1 2 3 15 22 24 | |
echo "$$ $(date "+%d/%b/%Y:%H:%M:%S") $0 $*" >> $LOCK | |
init | |
FAILED=0 | |
while : | |
do | |
[ ! -f $LOCK ] && exit 1 | |
[ -f $LOCK ] && [ $(cut -f 1 -d " " $LOCK) -ne $$ ] && exit 1 | |
START=$( date +%s ) | |
run | |
[ $(jobs -p) ] && wait $(jobs -p) | |
STOP=$( date +%s ) | |
[ $( expr $STOP - $START ) -lt 3 ] && FAILED=$( expr $FAILED + 1 ) || FAILED=0 | |
if [ -x $(dirname $0)/sendmail.py ]; then | |
echo "$$@$(hostname) $(date "+%d/%b/%Y:%H:%M:%S") time:$( expr $STOP - $START ) $0 $*" | $(dirname $0)/sendmail.py "$(hostname) $(basename $0) restart!" | |
fi | |
[ $FAILED -ge 3 ] && sleep 300 | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
init () { | |
echo "init" | |
cd ~ | |
} | |
run () { | |
echo "run" | |
sleep 1 | |
} | |
cleanup () { | |
: | |
} | |
source $(dirname $0)/daemon.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- encoding: utf-8 -*- | |
# vim: set et sw=4 ts=4 sts=4 ff=unix fenc=utf8: | |
# Author: Binux<huangkuan@duokan.com> | |
# http://binux.me | |
# Created on Wed 11 Jan 2012 04:52:06 PM CST | |
import sys | |
import smtplib | |
from sys import argv as args | |
from email.mime.text import MIMEText | |
smtp_host = "smtp.exmail.qq.com" | |
smtp_port = 465 | |
using_ssl = True | |
username = "" | |
password = "" | |
fromaddr = "" | |
toaddrs = ["", ] | |
subject = args[1] if len(args) > 1 else "pysendmail" | |
msg = sys.stdin.read() | |
################################################# | |
outer = MIMEText(msg) | |
outer['Subject'] = subject | |
outer['From'] = fromaddr | |
outer['To'] = ", ".join(toaddrs) | |
outer.preamble = 'You will not see this in a MIME-aware mail reader.\n' | |
if using_ssl: | |
smtp = smtplib.SMTP_SSL(smtp_host, smtp_port) | |
else: | |
smtp = smtplib.SMTP(smtp_host, smtp_port) | |
if username and password: | |
assert smtp.login(username, password)[0] == 235, "login error" | |
smtp.sendmail(fromaddr, toaddrs, outer.as_string()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
if [ ! $STY ]; then | |
screen -S $(basename $0 .sh) -U -d -m $0 $* | |
exit 0 | |
fi | |
if [ ! $LOCK ]; then | |
LOCK=$(echo $0 $* | md5sum | cut -f 1 -d " ").lock | |
fi | |
LOCK=~/$LOCK | |
_cleanup () { | |
cleanup | |
[ $(jobs -p) ] && kill $(jobs -p) | |
[ $(jobs -p) ] && sleep 1 | |
[ $(jobs -p) ] && sleep 5 | |
[ $(jobs -p) ] && sleep 10 | |
[ $(jobs -p) ] && kill -9 $(jobs -p) | |
rm -f $LOCK | |
exit 0 | |
} | |
if [ -f $LOCK ]; then | |
LOCK_PID=$(cut -f 1 -d " " $LOCK) | |
kill -0 $LOCK_PID && echo "$(date "+%d/%b/%Y:%H:%M:%S") still got $LOCK" || rm -f $LOCK | |
exit 1 | |
fi | |
trap '_cleanup' 0 1 2 3 15 22 24 | |
echo "$$ $(date "+%d/%b/%Y:%H:%M:%S") $0 $*" >> $LOCK | |
init | |
run | |
[ $(jobs -p) ] && wait $(jobs -p) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment