linux 设置程序开机启动
apache 服务
- yum 安装
yum install httpd -y
- 启动,并添加开机启动
systemctl start httpd #启动服务 systemctl enable httpd #设置开机启动
mysql
进入 mysql 的安装目录,添加启动服务:
cd /home/mysql cp support-files/mysql.server /etc/init.d/mysql
设置启动文件执行权限:
chmod a+x /etc/init.d/mysql
使用启动文件测试 mysql 服务启动和停止:
/etc/init.d/mysql start #启动mysql /etc/init.d/mysql stop #关闭mysql
将 mysql 服务加入 chkconfig 管理列表:
chkconfig --add /etc/init.d/mysql
这时我们可以通过 service 命令启停 mysql:
service mysql start service mysql stop
添加开机启动:
chkconfig mysql on
nginx
nginx 开机启动设置和 mysql 类似
- 启动脚本
在 linux 系统的/etc/init.d/目录下创建 nginx 文件,使用如下命令:
vim /etc/init.d/nginx
添加如下内容:
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: NGINX is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/home/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/home/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -n "$user" ]; then
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
fi
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
该脚本来自于 nginx 官网:链接地址
注意:
nginx="/home/nginx/sbin/nginx" NGINX_CONF_FILE="/home/nginx/conf/nginx.conf"
这两项配置要修改成自己 nginx 安装的实际路径。
- 设置开机启动
设置启动文件执行权限:
chmod a+x /etc/init.d/nginx
使用启动文件测试 mysql 服务启动和停止:
/etc/init.d/nginx start #启动nginx /etc/init.d/nginx stop #关闭nginx
将 nginx 服务加入 chkconfig 管理列表:
chkconfig --add /etc/init.d/nginx
这时我们可以通过 service 命令启停 nginx:
service nginx start service nginx stop
添加开机启动:
chkconfig nginx on
备注:如果 nginx 文件是在 windows 系统下编写的,在 linux 环境下启动可能会存在如下的问题:
-bash: /etc/rc.d/init.d/nginx: /bin/sh^M: bad interpreter: No such file or directory
错误分析:
因为操作系统是 windows,在 windows 下编辑的脚本,所以有可能有不可见字符。脚本文件是 DOS 格式的, 即每一行的行尾以\n\r 来标识, 其 ASCII 码分别是 0x0D, 0x0A。
解决方法:
vim filename
然后用命令
:set ff? #可以看到 dos 或 unix 的字样. 如果的确是 dos 格式的。
然后用
:set ff=unix #把它强制为 unix 格式的, 然后存盘退出。
再次运行脚本。
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于