最详细的CentOS7设置自定义开机启动服务教程 |
您所在的位置:网站首页 › 自定义开机启动 › 最详细的CentOS7设置自定义开机启动服务教程 |
CentOS 7 添加开机启动有两种方式: 一、添加开机启动服务Centos 系统服务脚本目录: /usr/lib/systemd/有系统(system)和用户(user)之分,如需要开机没有登陆情况下就能运行的程序,存在系统服务(system)里,即: /usr/lib/systemd/system/反之,用户登录后才能运行的程序,存在用户(user)里,服务以.service结尾。 /usr/lib/systemd/user/这边以nginx开机运行为例: 1.建立服务文件 vim /lib/systemd/system/nginx.service [Unit] Description=nginx After=network.target [Service] Type=forking User=nginx Group=nginx ExecStart=/etc/init.d/nginx start ExecReload=/etc/init.d/nginx restart ExecStop=/etc/init.d/nginx stop PrivateTmp=true [Install] WantedBy=multi-user.targetDescription:描述服务 After:描述服务类别 [Service]服务运行参数的设置 Type=forking是后台运行的形式 User服务启动用户 Group服务启动用户组 ExecStart为服务的具体运行命令 ExecReload为重启命令 ExecStop为停止命令 PrivateTmp=True表示给服务分配独立的临时空间 注意:[Service]的启动、重启、停止命令全部要求使用绝对路径 [Install]服务安装的相关设置,可设置为多用户 2.保存目录 以754的权限保存在目录: /lib/systemd/system3.设置开机自启动 systemctl enable nginx.service4.其他命令 任务 旧指令 新指令 使某服务自动启动 Chkconfig --level 3 httpd on systemctl enable httpd.service 使某服务不自动启动 chkconfig --level 3 httpd off systemctl disable httpd.service 检查服务状态 service httpd status systemctl status httpd.service (服务详细信息) systemctl is-active httpd.service(仅显示是否Active) 显示所有已启动的服务 chkconfig --list systemctl list-unit-files systemctl list-units --type=service 启动某服务 service httpd start systemctl start httpd.service 停止某服务 service httpd stop systemctl stop httpd.service 重启某服务 service httpd restart systemctl restart httpd.service启动nginx服务: systemctl start nginx.service设置开机自启动: systemctl enable nginx.service停止开机自启动: systemctl disable nginx.service查看服务当前状态: systemctl status nginx.service重新启动服务: systemctl restart nginx.service查看所有已启动的服务: systemctl list-units --type=service systemctl list-unit-files 常用服务文件1.nginx.service [Unit] Description=nginx - high performance web server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop[Install] WantedBy=multi-user.target 2.mysql.service [Unit] Description=mysql After=network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStart=/usr/local/mysql/support-files/mysql.server start #ExecReload=/usr/local/mysql/support-files/mysql.server restart #ExecStop=/usr/local/mysql/support-files/mysql.server stop #PrivateTmp=true [Install] WantedBy=multi-user.target3.php-fpm.service [Unit] Description=php After=network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStart=/usr/local/php/sbin/php-fpm [Install] WantedBy=multi-user.target4.redis.service [Unit] Description=Redis After=network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStart=/usr/local/bin/redis-server /etc/redis.conf ExecStop=kill -INT `cat /tmp/redis.pid` User=www Group=www [Install] WantedBy=multi-user.target5.supervisord.service [Unit] Description=Process Monitoring and Control Daemon After=rc-local.service [Service] Type=forking ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf SysVStartPriority=99 [Install] WantedBy=multi-user.target 二、添加开机启动脚本在centos7中增加脚本有两种常用的方法,以脚本autostart.sh为例: #!/bin/bash #description:开机自启脚本 /mnt/shell/autostart.sh #启动nginx 方法一1、赋予脚本可执行权限(/mnt/shell/autostart.sh是你的脚本路径) chmod +x /mnt/shell/autostart.sh2、打开/etc/rc.d/rc/local文件,在末尾增加如下内容 /mnt/shell/autostart.sh3、在centos7中,/etc/rc.d/rc.local的权限被降低了,所以需要执行如下命令赋予其可执行权限 chmod +x /etc/rc.d/rc.local 方法二1、将脚本移动到/etc/rc.d/init.d目录下 mv /mnt/shell/autostart.sh /etc/rc.d/init.d2、增加脚本的可执行权限 chmod +x /etc/rc.d/init.d/autostart.sh3、添加脚本到开机自动启动项目中 cd /etc/rc.d/init.d chkconfig --add autostart.sh chkconfig autostart.sh on |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |