nohup & 利用shell写php守护脚本

用到知识(自己百度)

nohup

linux中?,#等代表什么

shell判断文件是否存在

Linux Shell-------if -eq,if -ne,if -gt

1 nohup php /www/wwwroot/xxx/think testxxx

默认输出内容到 执行命令的当前目录下的nohup.out文件里

2 nohup php /www/wwwroot/xxx/think test > /dev/null 不接受输出,重定向到空

3 nohup php /www/wwwroot/xxx/think test > /dev/null 2>test.log 把错误重定向到test.log文件里

4 nohup php /www/wwwroot/xxx/think test > /dev/null 1>logt.log 把输出重定向到log.log文件里

脚本规则:

NAME="xiaoming"

输出变量:双引号+大括号形式 "你是一个好孩子${NAME}"

制作shell脚本步骤

1 脚本代码

bash 复制代码
#!/bin/bash


PHP="/usr/bin/php" #php可执行文件

XPZ_NAME="test" #自定义的服务名称
XPZ_SHELL_PATH="/www/shell/php/${XPZ_NAME}"
XPZ_SCRIPT="/www/wwwroot/xxx/think" #服务脚本
XPZ_PID="${XPZ_SHELL_PATH}/${XPZ_NAME}.pid" #服务pid
XPZ_LOG="${XPZ_SHELL_PATH}/${XPZ_NAME}.log" #服务运行日志记录错误信息
#判断程序是否运行
static_script(){
	ps -aux | grep ${XPZ_SCRIPT} | grep ${XPZ_NAME} |grep -v grep
	if [ $? -eq 0 ]
	then
		echo $0' is running!'
		running=1
	elif [ $? -ne 0 ]
	then
		echo $0' is not running!'
		running=2
	fi
}

#启动脚本
start_script(){
	static_script   #先判断脚本是否在执行
	if [ ${running} -eq 1 ]
	then
		echo $0 " is running!"
	else
		if [ ! -d ${XPZ_SHELL_PATH} ];then

			mkdir ${XPZ_SHELL_PATH}

		fi
		if [ ! -f ${XPZ_LOG} ];then
			touch ${XPZ_LOG}
		fi
		if [ ! -f ${XPZ_PID} ];then
			touch ${XPZ_PID}
		fi
		echo "start"$0"..."
		cd ${XPZ_SHELL_PATH}
		nohup ${PHP} ${XPZ_SCRIPT} ${XPZ_NAME}>/dev/null 2>${XPZ_LOG} & #把错误重定向到XPZ_LoG文件里
		echo $! > ${XPZ_PID}
		echo "start finish ,pid$!" 
	fi
}
#停止脚本
stop_script(){
	static_script
	if [ ${running} -ne 1 ];then
		echo $0"no starting "$?...
	else
		PHPPIDS=`ps -aux |grep ${XPZ_SCRIPT} | grep ${XPZ_NAME} |grep -v grep | awk '{print $2}'`
		for kill_pid in ${PHPPIDS}
		do
			kill -TERM ${kill_pid} > /dev/null 2>&1
			echo "Kill pid ${kill_pid}..."
		done
		echo 'stop complect'
	fi

}

#重启脚本
reload_script(){

	stop_script
	echo "正在重启..."
	sleep 3
	start_script
	echo "重启完成..."

}



handle(){
	case $1 in
	start)
		start_script
		;;
	stop)
		stop_script
		;;
	status)
		static_script
		;;
	reload)
		reload_script
		;;
	restart)
		reload_script
		;;
	*)
		echo "test OF THIS SERVER IS ${0} status|start|stop|restart"
		;;
	esac
}


if [ $# -eq 1 ];then
	handle $1
else
	echo "test OF THIS SERVER IS '${0}' status|start|stop|restart"
fi

2 把文件权限设成 755(或者 chmod a+x test.sh

此时脚本已经可以用了

./test.sh stop

3 如果想用service命令,类似这样service phptest stop

需要把shell脚本复制到/etc/init.d/目录下,并把名字改成phptest

cp test.sh /etc/init.d/phptest

现在就可以用

service phptet stop

启动php后台程序了

4 如果要设置成开机自启动

chkconfig phptest on

相关推荐
xiaobai12 32 分钟前
二叉树的遍历【C++】
开发语言·c++·算法
DieSnowK8 分钟前
[项目][WebServer][Makefile & Shell]详细讲解
开发语言·c++·http·makefile·shell·项目·webserver
Freak嵌入式9 分钟前
全网最适合入门的面向对象编程教程:50 Python函数方法与接口-接口和抽象基类
java·开发语言·数据结构·python·接口·抽象基类
冷凝女子12 分钟前
【QT】基于HTTP协议的网络应用程序
开发语言·qt·http
知识分享小能手15 分钟前
mysql学习教程,从入门到精通,SQL 删除数据(DELETE 语句)(19)
大数据·开发语言·数据库·sql·学习·mysql·数据开发
鸽芷咕23 分钟前
【Python报错已解决】libpng warning: iccp: known incorrect sRGB profile
开发语言·python·机器学习·bug
白总Server29 分钟前
MongoDB解说
开发语言·数据库·后端·mongodb·golang·rust·php
XyLin.32 分钟前
Msf之Python分离免杀
开发语言·python·网络安全·系统安全
声学黑洞仿真工作室34 分钟前
Matlab Delany-Bazley和Miki模型预测多孔材料吸声性能
开发语言·人工智能·算法·matlab·微信公众平台
计算机学姐42 分钟前
基于python+django+vue的家居全屋定制系统
开发语言·vue.js·后端·python·django·numpy·web3.py