Ubuntu 18.04设置开机自动启动

Ubuntu 18.04从16.10开始采用systemd管理系统,开机自启动服务需通过systemctl命令操作。文章介绍了如何编辑启动脚本的[Unit]、[Service]和[Install]段,并创建软链接到/etc/systemd/system目录,以实现开机启动。同时,还讲解了如何创建和设置/etc/rc.local文件,赋予其可执行权限,并测试脚本确保启动成功。

Ubuntu 16.10开始不再使用initd管理系统,改用systemd,包括用systemctl命令来替换了service和chkconfig的功能。比如以前启动mysql服务用sudo service mysql start,现在用sudo systemctl start mysqld.service。systemd 默认读取 /etc/systemd/system 下的配置文件,该目录下的文件会链接/lib/systemd/system/下的文件。

执行命令ls /lib/systemd/system可以看到很多启动脚本,其中就有我们需要的rc.local.service,打开脚本内容如下

#  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
### 配置 Ubuntu 18.04 应用程序开机自启 #### 使用 `rc.local` 文件实现开机自启动服务 对于希望以传统方式设置开机自启动的应用,在 `/etc/systemd/system/` 下创建名为 `rc-local.service` 的文件来定义服务行为[^2]: ```bash sudo vim /etc/systemd/system/rc-local.service ``` 编辑后的文件内容应类似于下面这样,确保 `[Install]` 和 `[Service]` 节点下的路径指向实际存在的脚本或命令。 接着修改并保存位于 `/etc/rc.local` 中的内容,该位置用于放置具体的执行指令列表。注意保持最后一行为空白以便正常结束进程: ```bash #!/bin/bash # rc.local # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don't # want to do the full Sys V style init stuff. touch /var/lock/subsys/local gnome-terminal -x sh -c "/home/user/script.sh; exec bash" ``` 赋予此文件可执行权限,并启用及启动对应的服务单元: ```bash sudo chmod +x /etc/rc.local sudo systemctl enable rc-local sudo systemctl start rc-local.service ``` 验证服务状态以确认一切运作良好: ```bash sudo systemctl status rc-local.service ``` #### 利用图形界面工具设定应用开机运行 另一种更为直观的方式是在桌面环境中通过 GUI 完成相同目标。打开“启动应用程序首选项”,点击添加按钮后输入名称与描述信息;最重要的是指定要随系统一同加载的命令字符串,例如让终端模拟器按照特定参数调用 shell 脚本[^3]: ```bash gnome-terminal -x /home/xavier/result.sh ``` 这种方法适用于那些不需要超级用户权限即可独立工作的个人化小程序或是快捷操作集合。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱清清

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值