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

相关推荐
码农小韩4 分钟前
基于Linux的C++学习——动态数组容器vector
linux·c语言·开发语言·数据结构·c++·单片机·学习
木风小助理5 分钟前
`mapfile`命令详解:Bash中高效的文本至数组转换工具
开发语言·chrome·bash
yyy(十一月限定版)15 分钟前
初始matlab
开发语言·matlab
LawrenceLan15 分钟前
Flutter 零基础入门(九):构造函数、命名构造函数与 this 关键字
开发语言·flutter·dart
listhi52016 分钟前
基于MATLAB的支持向量机(SVM)医学图像分割方法
开发语言·matlab
hui函数21 分钟前
如何解决 pip install 编译报错 g++: command not found(缺少 C++ 编译器)问题
开发语言·c++·pip
Tisfy30 分钟前
网站访问耗时优化 - 从数十秒到几百毫秒的“零成本”优化过程
服务器·开发语言·性能优化·php·网站·建站
济61737 分钟前
嵌入式C语言(第一期)
c语言·开发语言
XiaoHu020738 分钟前
Linux多线程(详细全解)
linux·运维·服务器·开发语言·c++·git
苏宸啊1 小时前
C++(二)类和对象上篇
开发语言·c++