(下载 http://huan.googlecode.com/svn/bash/start_mlnet.sh)
#!/bin/bash
## huanlf AT gmail.com li-jiahuan
## 这个脚本用来启动mlnet
## 这个文件应该放到用户的crontab中启动
## @reboot /path/to/start_mlnet.sh
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
## 确保中文文件名可以正常显示, 需要修改成相应的locale
export LC_ALL=zh_CN.UTF-8
export LANG=zh_CN.UTF-8
mlnet_pid=~/.mldonkey/mlnet.pid
## 检查是否有等待下载的文件
have_files_to_download()
{
grep -wq 'file_filenames' ~/.mldonkey/files.ini
}
## 等待连接上Internet
wait_for_connect()
{
while ! ping g.cn -c1 -w2 &> /dev/null; do
sleep 3
done
}
## 启动服务
start_mlnet()
{
## 如果有pid file, mlnet可能已经在运行
if [ -e "$mlnet_pid" ]; then
pid=$(cat "$mlnet_pid")
## 如果mlnet不在运行中,则开始运行
if ! kill -0 "$pid" &> /dev/null ; then
rm "$mlnet_pid"
( nice -19 mlnet > /tmp/mlnet 2>&1 & )
#echo $! >> "$mlnet_pid"
fi
## 如果没有pid file,开始运行mlnet
else
( nice -19 mlnet > /tmp/mlnet 2>&1 & )
#echo $! >> "$mlnet_pid"
fi
}
## -------------------- main -------------------------
if ! have_files_to_download; then
exit 0
fi
wait_for_connect && start_mlnet