linux 设置程序开机启动

本贴最后更新于 2120 天前,其中的信息可能已经时移世改

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 格式的, 然后存盘退出。
再次运行脚本。

  • Apache
    27 引用 • 35 回帖
  • Linux

    Linux 是一套免费使用和自由传播的类 Unix 操作系统,是一个基于 POSIX 和 Unix 的多用户、多任务、支持多线程和多 CPU 的操作系统。它能运行主要的 Unix 工具软件、应用程序和网络协议,并支持 32 位和 64 位硬件。Linux 继承了 Unix 以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统。

    951 引用 • 943 回帖
  • MySQL

    MySQL 是一个关系型数据库管理系统,由瑞典 MySQL AB 公司开发,目前属于 Oracle 公司。MySQL 是最流行的关系型数据库管理系统之一。

    693 引用 • 537 回帖 • 1 关注
  • NGINX

    NGINX 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 NGINX 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本 0.1.0 发布于 2004 年 10 月 4 日。

    315 引用 • 547 回帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...
upaths
Artists used the lies to tell the truth, while politicians used them to cover the truth up. 深圳