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

相关推荐
测试员周周2 小时前
【Appium 系列】第16节-WebView-H5上下文切换 — 混合应用的自动化难点
运维·开发语言·人工智能·功能测试·appium·自动化·测试用例
杜子不疼.4 小时前
【C++ AI 大模型接入 SDK】 - DeepSeek 模型接入(上)
开发语言·c++·chatgpt
加号34 小时前
【C#】 串口通信技术深度解析及实现
开发语言·c#
sycmancia5 小时前
Qt——编辑交互功能的实现
开发语言·qt
石山代码5 小时前
C++ 内存分区 堆区
java·开发语言·c++
无风听海6 小时前
C# 隐式转换深度解析
java·开发语言·c#
一只大袋鼠6 小时前
Git 进阶(二):分支管理、暂存栈、远程仓库与多人协作
java·开发语言·git
LuminousCPP7 小时前
数据结构 - 线性表第四篇:C 语言通讯录优化升级全记录(踩坑 + 思考)
c语言·开发语言·数据结构·经验分享·笔记·学习
web3.08889997 小时前
1688 图搜接口(item_search_img / 拍立淘) 接入方法
开发语言·python
один but you8 小时前
从可变参数到 emplace:现代 C++ 性能优化的核心组合
java·开发语言