南京网站开发南京乐识强,电子商务与网站建设结业论文,国家新闻大事,网站建设最高管理权限目录
前言
两种方式
/etc/systemd/system/
进入 /etc/systemd/system/ 文件夹
创建 nginx.service 文件
重新加载 systemd 配置文件
编辑 配置开机自启
/etc/init.d/
进入 /etc/init.d/ 文件夹
创建 mysql 文件
编写脚本内容
添加/删除系统服务
配置开机自启
…目录
前言
两种方式
/etc/systemd/system/
进入 /etc/systemd/system/ 文件夹
创建 nginx.service 文件
重新加载 systemd 配置文件
编辑 配置开机自启
/etc/init.d/
进入 /etc/init.d/ 文件夹
创建 mysql 文件
编写脚本内容
添加/删除系统服务
配置开机自启
查看 chkconfig 下的开机自启动服务
编辑总结
文件模板
脚本模板 前言
在服务器上安装各种软件比如 nginxmysqlredis 等等如果没有配置环境变量每次启动都需要进入到对应的安装目录下执行命令来启动。即使配置了环境变量在启动时可能也需要指定某些启动参数需要额外得输入相对麻烦。
针对以上的问题可以将制作成系统服务通过 service 命令 或者 systemctl 命令来启动
service 服务名称 startsystemctl start 服务名称
两种方式
/etc/systemd/system/ /etc/init.d/
第一种即是在 /etc/systemd/system/ 文件夹下创建服务文件
第二种即是在 /etc/init.d/ 文件夹下创建服务文件
详情如下
/etc/systemd/system/
以创建 nginx 系统服务为例子
进入 /etc/systemd/system/ 文件夹
cd /etc/systemd/system/
创建 nginx.service 文件
vim nginx.service
写入以下内容
注意路径需要替换成自己的安装路径 [Unit] DescriptionNginx HTTP Server Afternetwork.target [Service] Typeforking ExecStart/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload/usr/local/nginx/sbin/nginx -s reload -c /usr/local/nginx/conf/nginx.conf ExecStop/usr/local/nginx/sbin/nginx -s stop PrivateTmptrue [Install] WantedBymulti-user.target 重新加载 systemd 配置文件
systemctl daemon-reload
重新加载后就能通过 service 或者 systemctl 来启动服务了
#启动服务
systemctl start nginx
#停止服务
systemctl stop nginx
#重启服务
systemctl restart nginx
#查看服务状态
systemctl status nginx 配置开机自启
#开启开机自启动
systemctl enable nginx
#关闭开机自启动
systemctl disable nginx
/etc/init.d/
以创建 mysql 系统服务为例子
进入 /etc/init.d/ 文件夹
cd /etc/init.d/
创建 mysql 文件
vim mysql
编写脚本内容
脚本可以自己写在此例中用的mysqlmysql 有提供一个启动脚本直接复制即可
官方下载 mysql 包下安装目录下的 support-files 下有一个 mysql.server 脚本 复制 mysql.server 内容到 /etc/init.d/mysql
cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
授权
chmod x /etc/init.d/mysql
添加/删除系统服务
#添加系统服务
chkconfig --add mysql
#删除系统服务
chkconfig --del mysql
添加服务后就能通过 service 或者 systemctl 名来来启动服务了
配置开机自启
#开启开机自启动
chkconfig mysql on
#关闭开机自启动
chkconfig mysql off
查看 chkconfig 下的开机自启动服务
chkconfig --list 总结
两种方式都可以配置服务
第一种配置方式相对简单只需在 /etc/systemd/system/ 文件夹下创建文件复制模板后修改下命令写入文件后重新加载下就行了
文件模板 [Unit] Description服务名称 Afternetwork.target [Service] Typeforking ExecStart启动命令 ExecReload重启命令 ExecStop停止命令 PrivateTmptrue [Install] WantedBymulti-user.target 第二种配置方式大同小异在 /etc/init.d/ 文件夹下创建脚本需要自己写启动脚本更加地灵活如果启动服务需要用到动态的启动参数那么就可以用第二种方式来配置
脚本模板
#!/bin/bashcase $1 instart)#启动命令echo service is started;;stop)#关闭命令echo service is stop;;restart)#重启命令echo service is started;;*)echo start|stop|restart;;
esac
exit 0