ubuntu18.04设置开机自启动

本文详细介绍如何在Ubuntu 18.04中通过配置rc.local实现开机自启动脚本,包括创建rc-local.service文件、设置执行权限及启用服务等步骤。

ubuntu-18.04不能像ubuntu14一样通过编辑rc.local来设置开机启动脚本,通过下列简单设置后,可以使rc.local重新发挥作用。

1、建立rc-local.service文件

1

sudo vi /etc/systemd/system/rc-local.service

2、将下列内容复制进rc-local.service文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

[Unit]

Description=/etc/rc.local Compatibility

ConditionPathExists=/etc/rc.local

 

[Service]

Type=forking

ExecStart=/etc/rc.local start

TimeoutSec=0

StandardOutput=tty

RemainAfterExit=yes

SysVStartPriority=99

 

[Install]

WantedBy=multi-user.target

3、创建文件rc.local  

1

sudo vi /etc/rc.local

4、将下列内容复制进rc.local文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

#!/bin/sh -e

#

# rc.local

#

# This script is executed at the end of each multiuser runlevel.

# Make sure that the script will "exit 0" on success or any other

# value on error.

#

# In order to enable or disable this script just change the execution

# bits.

#

# By default this script does nothing.

echo "看到这行字,说明添加自启动脚本成功。" > /usr/local/test.log

exit 0

5、给rc.local加上权限

1

sudo chmod +x /etc/rc.local

6、启用服务

1

sudo systemctl enable rc-local

7、启动服务并检查状态

1

2

sudo systemctl start rc-local.service

sudo systemctl status rc-local.service

8、重启并检查test.log文件

1

cat /usr/local/test.log  

如果能看到内容,说明设置成功,你就可以通过编辑rc.local文件来设置启动脚本了

### 配置 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 ``` 这种方法适用于那些不需要超级用户权限即可独立工作的个人化小程序或是快捷操作集合。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值