存档

文章标签 ‘shell’

Nginx、php-cgi启动停止脚本

2009年12月18日 admin 2 条评论

为了方便Nginx和php-cgi的启动停止写了一个脚本,将下面脚本保存为/etc/init.d/nginxd,支持service nginxd start|stop|restart|reload|status

注意:标亮的行可能需要按你的环境修改

#!/bin/sh

# source function library
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

RETVAL=0
prog="nginx"

nginxDir=/usr/local/nginx
nginxd=$nginxDir/sbin/nginx
nginxConf=$nginxDir/conf/nginx.conf
nginxPid=$nginxDir/logs/nginx.pid

nginx_check()
{
	if [[ -e $nginxPid ]]; then
		ps aux |grep -v grep |grep -q nginx
		if (( $? == 0 )); then
			echo "$prog already running..."
			exit 1
		else
			rm -rf $nginxPid &> /dev/null
		fi
	fi
}

phpcgi_check()
{
	netstat -tunlp |grep -q php-cgi
	if (( $? == 0 )); then
		echo "php-cgi already running..."
		return 1
	fi
}

phpcgi_start()
{
	phpcgi_check
	if (( $? == 0 )); then
		echo -n $"Starting php-cgi:"
		daemon /usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u nobody -g nobody -C 64 -f /usr/bin/php-cgi
		RETVAL=$?
		echo
                [ $RETVAL = 0 ] && touch /var/lock/subsys/php-cgi
                return $RETVAL
	fi
}

phpcgi_stop()
{
	echo -n $"Stopping php-cgi:"
	phpcgi_pid=`netstat -tnlp |grep php-cgi |awk '{print $7}' |awk -F'/' '{print $1}'`
        kill -9 $phpcgi_pid &>/dev/null
        RETVAL=$?
	killall -9 php-cgi &>/dev/null
	RETVAL=$RETVAL+$?
	if (( $RETVAL == 0 )); then
		echo_success
	else
		echo_failure
	fi
        echo
	[ $RETVAL = 0 ] && rm -f /var/lock/subsys/php-cgi
}

start()
{
	nginx_check
	if (( $? != 0 )); then
		true
	else
		echo -n $"Starting $prog:"
		daemon $nginxd -c $nginxConf
		RETVAL=$?
		echo
		[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
		return $RETVAL
	fi
}

stop()
{
	echo -n $"Stopping $prog:"
	killproc $nginxd
	RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx $nginxPid
}

reload()
{
	echo -n $"Reloading $prog:"
	killproc $nginxd -HUP
	RETVAL=$?
        echo
}

case "$1" in
        start)
		phpcgi_start
                start
                ;;
        stop)
		phpcgi_stop
                stop
                ;;
        restart)
		phpcgi_stop
                stop
		phpcgi_start
                start
                ;;
        reload)
                reload
                ;;
        status)
                status $prog
                RETVAL=$?
                ;;
        *)
                echo $"Usage: $0 {start|stop|restart|reload|status}"
                RETVAL=1
esac
exit $RETVAL

在脚本中使用gmail作为邮件传输代理(MTA)发信

2009年3月8日 admin 没有评论

我们可以在shell脚本中通过使用msmtp设置gmail作为邮件传输代理(MTA)发送邮件。

1.安装msmtp
如果需要支持gmail请安装openssl

shell# tar xzvf openssl-0.9.7m.tar.gz
shell# cd openssl-0.9.7m
shell# ./config –prefix=/usr/local/openssl
shell# make
shell# make install

shell# tar -jxvf msmtp-1.4.16.tar.bz2
shell# cd msmtp-1.4.16
shell# ./configure –with-libssl-prefix=/usr/local/openssl/
如果使用gmail邮箱则必须支持TLS/SSL
Install prefix ……. : /usr/local
TLS/SSL support …… : yes (Library: OpenSSL)
GNU SASL support ….. : no
GNU Libidn support … : no
NLS support ………. : yes

shell# make
shell# make install
shell# mkdir -p /etc/ssl/certs/

上传ca-certificates.crt到/etc/ssl/certs/下。

2.配置msmtp
在用户的home目录下(一般为/home/<username>)创建.msmtprc

defaults
# 定义msmtp日志文件
logfile /root/.msmtp.log
# 定义ibaihe帐号,在msmtp的配置文件中可以定义多个帐号
account ibaihe
# 设置邮件服务器
host mail.ibaihe.com
from user@ibaihe.com
# 开启认证
auth login
# 设置认证的用户名和密码
user user@ibaihe.com
password <PWD>
# 定义gmail帐号,需要msmtp支持TLS/SSL,如果不支持请安装openssl后重新编译安装msmtp
account gmail
host smtp.gmail.com
from ibaihe@gmail.com
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
auth on
user ibaihe2008@gmail.com
password <PWD>
port 587
# 默认使用baihe帐号,也可以在mutt配置文件中通过-a参数指定使用其他帐号
# 如:set sendmail=”/usr/local/bin/msmtp -a gmail”
account default:gmail
配置完成后修改.msmtprc的所有权为本用户所有,权限为600
shell# chown user.group .msmtprc
shell# chmod 600 .msmtprc
3.配置mutt
mutt一般会默认安装,如果没有找到此命令,请自行安装。
在用户的home目录下(一般为/home/<username>)创建.muttrc
# 使用msmtp发送邮件
set sendmail=”/usr/local/bin/msmtp”
# 邮件发送时使用的用户名,一般会显示在发件人栏
set realname=”Alert”
set use_from=yes
set editor=”vi”
4.测试
echo “Hello world,Hello China.” |mutt -s “Test Mail” istone@gmail.com
分类: Linux服务 标签: , ,