【Xubuntu】多种方法设置xubuntu-18.04开机启动

写在前面的话:

  1. 版权声明:本文为博主原创文章,转载请注明出处!
  2. 博主是一个小菜鸟,并且非常玻璃心!如果文中有什么问题,请友好地指出来,博主查证后会进行更正,啾咪~~
  3. 每篇文章都是博主现阶段的理解,如果理解的更深入的话,博主会不定时更新文章。
  4. 本文初次更新时间:2020.12.04,最后更新时间:2020.12.04

正文开始

出于一些原因,需要自己写一些命令和脚本,在开机时可以自己执行,在网上找到一些方法,这里做一个总结记录。

环境:xubuntu 18.04

PS:为了方便,我个人使用root登陆,所以就不加sudo了。

注意:脚本一定要有可执行权限!!!

1. 常规设置(rc.local)

ubuntu 16.10 开始不再使用initd管理系统,改用systemd管理系统。

systemd管理系统默认读取/etc/systemd/system目录下的配置文件,该目录下的文件会链接/lib/systemd/system/下的文件。

1.1 修改 rc.local.service

执行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
#  (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

正常的启动文件主要分成以下三部分:

  • [Unit]段: 启动顺序与依赖关系。
  • [Service]段: 启动行为,如何启动,启动类型。
  • [Install]段: 定义如何安装这个配置文件,即怎样做到开机启动。

可以看出/etc/rc.local文件的启动顺序是在网络后面,但是它少了[Install]段,也就没有定义如何做到开机启动,所以这样配置是无效的。我们需要在后面加上[Install]段:

[Install]
WantedBy=multi-user.target
Alias=rc-local.service

放一下完整的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
#  (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

1.2 创建并修改 rc.local

因为Ubuntu 18.04默认没有/etc/rc.local文件,需要自己创建,然后将需要开机启动的命令或脚本写入该文件。

#!/bin/bash

chmod +x /root/my_startup.sh
/bin/bash /root/my_startup.sh

exit 0

记得 chmod +x /etc/rc.local

注意 :脚本或者开机执行的命令要在exit 0之前。

1.3 链接

/etc/systemd/system目录下创建rc.local.service服务的软链接:

ln -s /lib/systemd/system/rc.local.service /etc/systemd/system/
# 或
systemctl enable rc-local.service

然后重启即可。

2. UI设置

打开【UI 左上角】->【Settings】->【Session and Startup】:
在这里插入图片描述
选择【Application Autostart】,点击【Add】:
在这里插入图片描述
填写内容,Name是自己起的名字,Description是对该应用的描述,Command是启动的命令,填完点击【OK】。
在这里插入图片描述
确定自己设置的已经打上【√】了,再【Close】:
在这里插入图片描述

3. UI改脚本设置

效果同 UI 设置,只不过这里使用脚本进行设置。

mkdir -p /root/.config/autostart/
vim /root/.config/autostart/csc_test.desktop

写入如下内容:

[Desktop Entry]
Encoding=UTF-8
Version=0.9.4
Type=Application
Name=my_startup
Comment=startup tool
Exec=/root/mystartup.sh
OnlyShowIn=XFCE;
StartupNotify=false
Terminal=false
Hidden=false

参考

ubuntu 18.04 如何设置开机自动启动脚本
How can I edit/create new launcher items in Unity by hand?

相关文章

Debian设置开机自启动脚本的两种方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值