ubuntu设置自启动方式整理

本文详细介绍了在Linux系统中设置开机自启动服务的三种常见方法:通过在/etc/init.d/目录添加服务,编辑rc-local.service以及使用Systemd服务化自定义脚本。每种方法都包括了具体的步骤,如创建服务文件、设置执行权限和配置自启动等,适合系统管理员参考使用。
摘要由CSDN通过智能技术生成

方式一 在/etc/init.d/目录添加服务
1.在/etc/init.d目录下建立一个文件mystart,文件内容如下:

#! /bin/sh

echo "hello world!" > /home/mystart.txt
exit 0

2.查看当前运行级别

root@dev:~# runlevel
N 5

3.在当前运行级别的目录下建立软连接

ln -s /etc/init.d/mystart /etc/rc5.d/S99mystart

完成以上三步就设置了开机自启动,系统重启时会看到如下信息:

         Starting mystart.service...
[  OK  ] Started mystart.service.

可见你已经成功添加一个mystart.service 服务

方式二.在/etc/systemd/system目录添加rc-local服务
1.创建一个rc-local.service

sudo vi /etc/systemd/system/rc-local.service
[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

将上面这段内容写到rc-local.service。
2.创建/etc/rc.local

sudo vi /etc/rc.local
#!/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.
/home/et1000/lib/startjob.sh # 启动自己的程序
exit 0

接下来是给rc.local 执行的权限

sudo chmod +x /etc/rc.local

3.启动服务

sudo systemctl start rc-local.service
sudo systemctl status rc-local.service

完成以上三步就设置了开机自启动,系统重启时会看到启动rc-local.service的信息

方式三 使用Systemd把自作脚本服务化(加入开机启动)
1.在/etc/systemd/system/目录建立startjob.service

root@dev:/etc/systemd/system# cat startjob.service 
[Unit]
Description=startjob deamon
ConditionPathExists=/home/et1000/lib
 
[Service]
Type=simple
ExecStart=/home/et1000/lib/startjob.sh start #服务执行的自定义脚本
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
 
[Install]
WantedBy=multi-user.target

2.配置服务为自启动

root@dev:~# systemctl disable startjob #禁止开机启动
Removed /etc/systemd/system/multi-user.target.wants/startjob.service.
root@dev:~# systemctl list-unit-files --type=service | grep startjob #查看服务是否配置成功
startjob.service                           disabled 
root@dev:~# systemctl enable startjob #使能开机自启动
Created symlink /etc/systemd/system/multi-user.target.wants/startjob.service 鈫/etc/systemd/system/startjob.service.
root@dev:~# systemctl list-unit-files --type=service | grep startjob
startjob.service                           enabled  

3.查看服务状态

root@dev:~# systemctl status startjob[0m startjob.service - startjob deamon
   Loaded: loaded (/etc/systemd/system/startjob.service; enabled; vendor preset: enabled)
   Active: active (exited) since Thu 2016-11-03 17:16:46 UTC; 18min ago
 Main PID: 2376 (code=exited, status=0/SUCCESS)
   Memory: 5.0M
      CPU: 3min 15.590s
   CGroup: /system.slice/startjob.service
           鈹溾攢2385 /home/et1000/watchdog
           鈹溾攢2431 proparse --locale Standard05
           鈹溾攢2432 readmeter
           鈹溾攢2433 irSev
           鈹溾攢2434 watchgprs already_power_on
           鈹斺攢2506 gprs 30

Nov 03 17:16:46 dev systemd[1]: Started startjob deamon.
Nov 03 17:30:53 dev systemd[1]: [/etc/systemd/system/startjob.service:11] Support for option SysVStartPriority= has been removed and it is ignored
Nov 03 17:31:31 dev systemd[1]: [/etc/systemd/system/startjob.service:11] Support for option SysVStartPriority= has been removed and it is ignored

注意:debian8开始支持systemd作为默认init

参考:
linux中的unit的配置文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值