/home/mystart.txtexit 02.查看当前运行级别root@dev:~# runlevelN 53.在当前运行级别的目录下建立软连接ln -s /etc/init.d/mystart /etc/rc5.d/S99mystart完成以上三步就设置了开机自启动,系统重启时会看到如下信息: Starti_support for opti">
赞
踩
方式一 在/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的配置文件
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。