Linux:Ubuntu 20.04 —添加开机启动(服务/脚本)

这篇具有很好参考价值的文章主要介绍了Linux:Ubuntu 20.04 —添加开机启动(服务/脚本)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

本文章向大家介绍Linux Ubuntu 20.04 —添加开机启动(服务/脚本),主要包括Linux Ubuntu 20.04 —添加开机启动(服务/脚本)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。


系统启动时需要加载的配置文件

/etc/profile、/root/.bash_profile
/etc/bashrc、/root/.bashrc
/etc/profile.d/*.sh、/etc/profile.d/lang.sh
/etc/sysconfig/i18n、/etc/rc.local(/etc/rc.d/rc.local)

一、修改开机启动文件:/etc/rc.local(或者/etc/rc.d/rc.local)

# 1.编辑rc.local文件
[root@localhost ~]# vi /etc/rc.local

# 2.修改rc.local文件,在 exit 0 前面加入以下命令。保存并退出。
/etc/init.d/mysqld start                                        # mysql开机启动
/etc/init.d/nginx start                                          # nginx开机启动
supervisord -c /etc/supervisor/supervisord.conf                #supervisord开机启动
/bin/bash /server/scripts/test.sh >/dev/null 2>/dev/null

# 3.最后修改rc.local文件的执行权限
[root@localhost ~]# chmod +x  /etc/rc.local
[root@localhost ~]# chmod 755 /etc/rc.local

二、自己写一个shell脚本
将写好的脚本(.sh文件)放到目录 /etc/profile.d/ 下,系统启动后就会自动执行该目录下的所有shell脚本。
三、通过chkconfig命令设置

# 1.将(脚本)启动文件移动到 /etc/init.d/或者/etc/rc.d/init.d/目录下。(前者是后者的软连接)
mv  /www/wwwroot/test.sh /etc/rc.d/init.d

# 2.启动文件前面务必添加如下三行代码,否侧会提示chkconfig不支持。
#!/bin/sh                          告诉系统使用的shell,所以的shell脚本都是这样
#chkconfig: 35 20 80              分别代表运行级别,启动优先权,关闭优先权,此行代码必须
#description: http server          自己随便发挥!!!,此行代码必须
/bin/echo $(/bin/date +%F_%T) >> /tmp/test.log

# 3.增加脚本的可执行权限
chmod +x  /etc/rc.d/init.d/test.sh

# 4.添加脚本到开机自动启动项目中。添加到chkconfig,开机自启动。
[root@localhost ~]# cd /etc/rc.d/init.d
[root@localhost ~]# chkconfig --add test.sh
[root@localhost ~]# chkconfig test.sh on

# 5.关闭开机启动
[root@localhost ~]# chkconfig test.sh off

# 6.从chkconfig管理中删除test.sh
[root@localhost ~]# chkconfig --del test.sh

# 7.查看chkconfig管理
[root@localhost ~]# chkconfig --list test.sh

四、自定义服务文件,添加到系统服务,通过Systemctl管理
1.写服务文件:如nginx.service、redis.service、supervisord.service

[Unit]:服务的说明
Description:描述服务
After:描述服务类别

[Service]服务运行参数的设置
Type=forking            是后台运行的形式
ExecStart              为服务的具体运行命令
ExecReload              为服务的重启命令
ExecStop                为服务的停止命令
PrivateTmp=True        表示给服务分配独立的临时空间
注意:启动、重启、停止命令全部要求使用绝对路径

[Install]              服务安装的相关设置,可设置为多用户
WantedBy=multi-user.target

2.文件保存在目录下:以754的权限。目录路径:/usr/lib/systemd/system。如上面的supervisord.service文件放在这个目录下面。

[root@localhost ~]# cat /usr/lib/systemd/system/nginx.service
[root@localhost ~]# cat /usr/lib/systemd/system/supervisord.service

3.设置开机自启动(任意目录下执行)。如果执行启动命令报错,则执行:systemctl daemon-reload

# 设置开机自启动
[root@localhost ~]# systemctl enable nginx.service     
[root@localhost ~]# systemctl enable supervisord

# 停止开机自启动
[root@localhost ~]# systemctl disable nginx.service
[root@localhost ~]# systemctl disable supervisord

# 验证一下是否为开机启动
[root@localhost ~]# systemctl is-enabled nginx
[root@localhost ~]# systemctl is-enabled supervisord

4.其他命令

# 启动nginx服务
[root@localhost ~]# systemctl start nginx.service

# 停止nginx服务
[root@localhost ~]# systemctl stop nginx.service

# 重启nginx服务
[root@localhost ~]# systemctl restart nginx.service

# 查看nginx服务当前状态
[root@localhost ~]# systemctl status nginx.service

# 查看所有已启动的服务
[root@localhost ~]# systemctl list-units --type=service

5.服务文件示例:

# supervisord.service进程管理服务文件
[Unit]
Description=Process Monitoring and Control Daemon  # 内容自己定义:Description=Supervisor daemon
After=rc-local.service nss-user-lookup.target

[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
ExecStop= /usr/bin/supervisorctl shutdown
ExecReload=/usr/bin/supervisorctl reload
Restart=on-failure
RestartSec=42s
KillMode=process

[Install]
WantedBy=multi-user.target
# 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
# 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.target

Linux Ubuntu 20.04 —添加开机启动(服务/脚本) - 简书文章来源地址https://www.toymoban.com/news/detail-498465.html

到了这里,关于Linux:Ubuntu 20.04 —添加开机启动(服务/脚本)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 虚拟机ubuntu20.04扩容时遇到的问题及解决方法(包含fdisk打不开、开机黑屏无法启动及一种扩容办法)

    在创建ubuntu虚拟机的前期,默认选择了ubuntu的内存为20G,但是用了没多长时间就经常提示我内存不足,我也没怎么在意。直到某一天我发现代码都保存不了了。ubuntu扩容迫在眉睫。、 扩容的具体过程我这一次没有记录下来,着重讲一下遇到的问题,相信我之后还会扩容,后续

    2024年02月08日
    浏览(92)
  • ubuntu20.04开机黑屏只有光标闪烁

    前情介绍          最初遇到这个问题我一直以为开机黑屏,然而就那一瞥让我发现了事情没那么简单,原来还有一个小小的光标闪烁,ok,活来了! 原因分析         1、硬件可能连接不正确         2、驱动安装的有问题(我遇到的是这个问题)         3、安装

    2024年04月27日
    浏览(25)
  • ubuntu20.04开机界面黑屏,只有一个光标闪烁

    接下来我就把我的解决方法完整的发出来,因为我也是非常的绝望,终于在不断尝试中解决了问题 首先开机界面就是这个东西,一直卡在这不动了,原因就是,内存被用完了,无法加载出图形化界面 解决方法: 1.重启虚拟机,注意在重启之后,要不停的去按ctrl+alt+f3,因为鼠

    2023年04月27日
    浏览(41)
  • 27. Ubuntu 20.04 开机自动挂载文件/etc/fstab

    不同于热插拔的设备,对于硬盘可能需要长期挂载在系统下,所以如果每次开机都去手动mount是非常痛苦的,当然Ubuntu系统的GNOME桌面自带的gvfsd也会帮你自动挂载,但是指向的路径却是按照uuid命名的,这是极其痛苦的,所以希望开机就可以自动挂载硬盘到指定路径。 系统开

    2024年02月06日
    浏览(46)
  • Ubuntu20.04开机闪光标进不去图形界面

    (一)实验室电脑         默认进入系统的方式黑屏闪烁光标,重启后进入最新的recover模式中进行修复,发现还是进不去图形界面,再重启进入低版本的内核可以正常进入,说明是内核版本太高,与NVIDIA驱动不匹配导致的问题。这里的解决方案选择的是通过较低版本的内

    2024年02月15日
    浏览(35)
  • 虚拟机Ubuntu20.04 网络连接器图标开机不显示怎么办

    执行以下指令:

    2024年02月10日
    浏览(49)
  • Ubuntu 20.04 中安装docker一键安装脚本

    直接上脚本,依次执行如下命令即可 install docker operation system Ubuntu 18.04+ ways1 : wget https://github.com/grant-tt/docker/blob/main/docker_install.sh bash docker_install.sh ways2: wget http://apollo-pkg-beta.bj.bcebos.com/docker_install.sh bash docker_install.sh

    2024年02月13日
    浏览(39)
  • Ubuntu Server 20.04 网卡启动及配置

    由于网络环境问题,联网安装会导致报错,故在安装期间disable了所有网卡,下面记录装好之后打开的过程。 得到本机的所有网卡信息,例如我这边网卡为eth0 将上述网卡名称填入 把up换成down是关掉 ubuntu server 20.04 采用读yaml配置文件的方式修改网卡配置,文件在/etc/netplan/下,

    2024年02月11日
    浏览(50)
  • Linux | Ubuntu20.04系统使用命令从移动硬盘/U盘拷贝文件到服务器上

    *确认自己移动硬盘、U盘的格式,本文为exfat格式 查看disk默认位置 查看最后的位置,我的显示为 Device, 位置为 /dev/sdb1 ,2048, (后面省略) *注意:此时无法直接查看硬盘内容 进入Linux系统主界面,如果是user的话,获取管理员权限 挂载移动硬盘/U盘 命令如下,其中/dev/sdb1为disk默

    2024年02月14日
    浏览(36)
  • ubuntu20.04以及更高版本下docker添加国内镜像

    ubuntu20.04下默认是snap安装的docker。安装位置和apt安装的不一样。所以daemon.json的位置也不一样。国内网上说的都是往/ect/docker/daemon.json里添加 \\\"registry-mirrors\\\": [         \\\"http://hub-mirror.c.163.com\\\",         \\\"https://docker.mirrors.ustc.edu.cn\\\",         \\\"https://registry.docker-cn.com\\\"     ] 统统都

    2024年02月11日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包