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

相关推荐
IvanCodes5 小时前
Python 数据处理(十三):JSON、CSV 与数据序列化
开发语言·python
aramae6 小时前
MySQL复合查询(8)
java·c语言·开发语言·后端·算法
damoluomu7 小时前
国产企业级 PHP CMS:哪些能免费商用
php
qq_2518364579 小时前
springboot vue3 开发实现 拼豆管理系统
java·开发语言·ai编程
郑州光合科技余经理10 小时前
同城外卖小程序开发:下单成功后,后台导出能不能对上用户端状态
开发语言·前端·git·后端·uni-app·php·ai编程
知识分享小能手10 小时前
C++ 学习教程,从入门到精通,C++ 入门知识 — 完整知识点(1)
开发语言·c++·学习
MyFreeIT10 小时前
Android PHP Manual
php
+VX:Fegn089510 小时前
计算机毕业设计|基于springboot + vue旅游管理系统(源码+数据库+文档)
java·开发语言·vue.js·spring boot·课程设计
阿里嘎多学长11 小时前
2026-09-20 GitHub 热点项目精选
开发语言·程序员·github·代码托管
weixin_3077791313 小时前
C++代码实现MATLAB中的ode23tb函数功能
开发语言·c++·算法·matlab