ubuntu自启动程序管理

一、简介

在ubuntu下,系统管理程序是systemd,原先的initd管理系统已被放弃,rc.local文件常被用于配置程序自启,如今已经默认没有这个文件。但是在/etc/init.d下,还存在一些旧的服务启动脚本来管理一些程序的运行。

二、使用systemd配置.sh脚本自启动

1、创建需要启动的脚本,并写入指令,给与脚本权限

touch ~/test.sh
chmod 777 ~/test.sh #重要

#在test.sh中添加以下内容
vim ~/test.sh
#!/bin/bash
touch ~/test_auto_start.txt

2、进入/etc/systemd/system目录,创建配置文件

cd /etc/systemd/system
sudo touch test.service
sudo chmod 777 test.service
sudo vim test.service
#test.service添加以下内容
[Unit]
Description=Startup Example

#指定了在systemd在执行完那些target之后再启动该服务
After=network.target syslog.target

[Service]
#绝对路径
ExecStart=/home/chenjian/test.sh 

[Install]
WantedBy=multi-user.target

3、设置开机自启

sudo systemctl enable test.service

4、查看系统启动项

systemctl list-unit-files --type=service|grep enabled

5、关闭/开启程序自启

sudo systemctl disable test.service #关闭服务自启
sudo systemctl enable test.service #打开服务自启

注:这种方法的缺点是无法启动带界面的程序。

三、使用系统工具

如果找不到,就使用如下命令启动

gnome-session-properties

gnome-session-properties实际是在 /home/user/.config/autostart 下写入一个desktop启动脚本,也可以直接将脚本写成desktop的内容形式,放在/home/user/.config/autostart目录下。
内容格式如下:

注意:这种方式的缺点是需要登录桌面,才能自动启动。所以需要开启自动登录功能。

开启方法1:

开启方法2:

cd /etc/gdm3/
sudo vim custom.conf

将下面的自动登录打开,并修改用户名,重启系统,测试程序是否自启

posted @ 2021-12-03 10:45  chenjian688  阅读(409)  评论(0编辑  收藏  举报