centos ubuntu设置开机自启动systemctl托管概览:
ubuntu设置开机自启
centos设置开机自启
ubuntu centos设置systemctl托管
开机自启测试脚本
1 2 3 #!/bin/bash time=`date +%F%H%M` echo "开启自启动配置成功 time: ${time}" >> /root/2.log
一. centos开机自启 https://blog.csdn.net/zhanhjxxx/article/details/122811638
方法一:更改/etc/rc.d/rc.local(常用)
环境: centos7
1 2 3 4 5 6 7 8 以下三种里面配置都可以实现开机自启动,其实差不多,只是服务器启动的时候执行的顺序先后的问题。一般都是用rc.local。 1、/etc/init.d (少用) 目录存放开机初始化启动脚本,将脚本放入如:网络,环境变量等 2、/etc/fstab(少用) 系统初始化后,程序启动前加载(如磁盘挂载,mount命令等) 3、/etc/rc.local (常用) 系统完成之后,执行。(如:应用服务启动,nginx启动等) (常用) nfs放stab里面会在程序启动前加载上NFS文件系统,放到rc.local里如有程序依赖会造成程序启动加载时找不到路径。
rc.local文件,是个软链接实际文件不具备执行权限,设置开机启动,需要给文件授权
1 2 3 4 # 给文件授权加入测试开机自启脚本 ll /etc/rc.local chmod +x /etc/rc.d/rc.local vim /etc/rc.d/rc.local
验证:成功
二. ubuntu配置开机自启 https://blog.csdn.net/qq_43685040/article/details/111574332
概览:
查看系统中的自动启动脚本 修改 rc-local.service 文件的权限 修改 rc-local.service 文件 修改 /etc/rc.local 文件 创建软链接 重启 ubuntu后,去/root下看看测试脚本有没有生效
1.给rc-local.service文件授权1 2 3 4 # 查看系统中的自动启动脚本 可以看到有 rc-local.service 这个文件 ll /lib/systemd/system/rc-local.service # 修改 rc-local.service 文件的权限 chmod 777 /lib/systemd/system/rc-local.service
2.修改rc-local.service文件1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 # SPDX-License-Identifier: LGPL-2.1+ # # This file is part of systemd. # # systemd is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or # (at your option) any later version. # This unit gets pulled automatically into multi-user.target by # systemd-rc-local-generator if /etc/rc.local is executable. [Unit] Description=/etc/rc.local Compatibility Documentation=man:systemd-rc-local-generator(8) ConditionFileIsExecutable=/etc/rc.local After=network.target [Service] Type=forking ExecStart=/etc/rc.local start TimeoutSec=0 RemainAfterExit=yes GuessMainPID=no #增加以下内容修改部分如下: [Install] WantedBy=multi-user.target Alias=rc-local.service
3.修改/etc/rc.local文件** 查看系统中有无**/etc/rc.local这个文件,没有则自己创建一个。** ** 写入以下内容(清空文件中原有所有内容):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 # 查看有没有rc.local文件没有则新建一个rc.local文件 vim /etc/rc.local # 写入下面内容 #!/bin/bash # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES # # It is highly advisable to create own systemd services or udev rules # to run scripts during boot instead of using this file. # # In contrast to previous versions due to parallel execution during boot # this script will NOT be run after all other services. # # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure # that this script will be executed during boot.
1 2 3 4 # 赋予权限 chmod +x /etc/rc.local # 创建软链接 ln -s /lib/systemd/system/rc-local.service /etc/systemd/system/
配置测试开机自启脚本 后重启服务器验证
1 2 3 4 # 重启:reboot 验证:成功 root@gegewu-virtual-machine:~# cat 2.log 开启自启动配置成功 time: 2023-06-291658 开启自启动配置成功 time: 2023-06-291700
三. systemctl方式设置开机自启 前言:
https://blog.csdn.net/michaelwoshi/article/details/104078664
托管文件目录可选:
centos: /usr/lib/systemd/system/nginx.service ubuntu: /etc/systemd/system/nginx.service
$MAINPID介绍:
托管文件中的信号描述ubuntu与centos通用:
1. centos (此种通过MAINPID的centos与ubuntu通用)以 redis为例参考如下配置即可:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 # 建立托管文件 cat > /usr/lib/systemd/system/redis.service << eric [Unit] Description=redis [Service] Type=simple #Type=forking # 根据情况指定类型 #EnvironmentFile=/etc/systemd/test.conf ExecStart=/opt/redis-5.0.5/src/redis-server /opt/redis-5.0.5/redis.conf ExecReload=/bin/kill -SIGHUP $MAINPID # 通过MAINPID变量自动获取当前程序的pid达到终止进程的效果 ExecStop=/bin/kill -SIGINT $MAINPID #Restart=always #RestartSec=5 #StartLimitInterval=0 [Install] WantedBy=multi-user.target eric systemctl daemon-reload systemctl start redis.service systemctl enable redis.service systemctl restart redis.service systemctl status redis.service
2. ubuntu参考如下配置即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 # GEGEWU n9eserver vim /etc/systemd/system/n9eserver.service cat <<EOF >/etc/systemd/system/n9eserver.service [Unit] Description="n9eserver" After=network.target [Service] Type=simple ExecStart=/home/gegewu/TPG-Nightinagle/docker-composeTPG_2n9e/n9e-5.6/n9e server > /home/gegewu/TPG-Nightinagle/docker-composeTPG_2n9e/n9e-5.6/server.log WorkingDirectory=/home/gegewu/TPG-Nightinagle/docker-composeTPG_2n9e/n9e-5.6 Restart=always [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl enable n9eserver systemctl restart n9eserver systemctl status n9eserver # nginx cat <<EOF >/etc/systemd/system/nginx.service [Unit] Description=nginx server Documentation=http://nginx.org/en/docs/ After=network.target [Service] Type=forking #Type=simple #PIDFile=/usr/local/nginx/logs/nginx.pid ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/bin/kill -SIGHUP \$MAINPID ExecStop=/bin/kill -SIGINT \$MAINPID Restart=always RestartSec=5 #StartLimitInterval=0 [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl start nginx.service systemctl status nginx.service
四. 扩展 结语 fighting!