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

相关推荐
段一凡-华北理工大学1 小时前
向量数据库实战:选型、调优与落地~系列文章12:文本分块策略实战:chunk_size 怎么选?重叠多少?
开发语言·数据库·后端·oracle·rust·工业智能体·高炉智能化
Ljwuhe2 小时前
C++——多态
开发语言·c++
心平气和量大福大2 小时前
C#-WPF-Window主窗体
开发语言·c#·wpf
从零开始的代码生活_4 小时前
C++ 继承详解:访问控制、对象模型、菱形继承与设计取舍
开发语言·c++·后端·学习·算法
云小逸4 小时前
【C++ 第七阶段:模板、泛型编程与工程综合详解】
开发语言·c++
GIS阵地4 小时前
QgsSingleBandPseudoColorRenderer 完整详解(QGIS 3.40.13 C++)
开发语言·前端·c++·qt·qgis
yaoxin5211235 小时前
470. Java 反射 - Member 接口与 AccessFlag
java·开发语言·python
groundhappy5 小时前
idalib安装和codex ida-mcp配置
linux·开发语言·python
TlSfoward5 小时前
抓包代理链路下的 TLS 指纹变化分析 TLSFOWARD抓包工具
数据库·爬虫·网络协议·搜索引擎·php
小钻风33666 小时前
Spring Boot 文件上传详解:深入理解 MultipartFile 的使用与原理
java·开发语言