Fedora 22(15以上版本)开机自启动脚本
前段时间做了一个网站btdog磁力与btdog电视直播。DHT爬虫需要消耗比较多的资源,原来的服务器不够用了,于是自己使用电脑搭了一台服务器,使用Fedora22系统。在Fedora22中自动写了些开机自启动脚本,但始终找不到放在哪里。折腾了下,发现原来Fedora 从15开始,系统初始化软件开始由initscript转向了systemd方式,原来要写开机启动脚本一般写在rc.local里面,但现在rc.local已经不存在了,不过systemd仍然有rc-local服务。
编辑/usr/lib/systemd/system/rc-local.service
# 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.d/rc.local is executable. [Unit] Description=/etc/rc.d/rc.local Compatibility ConditionFileIsExecutable=/etc/rc.d/rc.local After=network.target [Service] Type=forking ExecStart=/etc/rc.d/rc.local start TimeoutSec=0 RemainAfterExit=yes
可见我们只要新建文件/etc/rc.d/rc.local 然后加入自己的脚本就可以了
新建rc.local
touch /etc/rc.d/rc.local
vim /etc/rc.d/rc.local
#!/bin/bash
#下面是你需要执行的脚本
#####################################
赋以可执行权限
chmod +x /etc/rc.d/rc.local
然后使用systemd开机自启动
systemctl enable rc-local.service
提示错误如下:
The unit files have no [Install] section. They are not meant to be enabled
using systemctl.
Possible reasons for having this kind of units are:
1) A unit may be statically enabled by being symlinked from another unit's
.wants/ or .requires/ directory.
2) A unit's purpose may be to act as a helper for some other unit which has
a requirement dependency on it.
3) A unit may be started when needed via activation (socket, path, timer,
D-Bus, udev, scripted systemctl call, ...).
这个时候,我们需要在/usr/lib/systemd/system/rc-local.service 中加入[Install]区域,用于告诉systemd需要在多用户启动时还是图形启动时自启动这个脚本
编辑/usr/lib/systemd/system/rc-local.service
# 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.d/rc.local is executable. [Unit] Description=/etc/rc.d/rc.local Compatibility ConditionFileIsExecutable=/etc/rc.d/rc.local After=network.target [Service] Type=forking ExecStart=/etc/rc.d/rc.local start TimeoutSec=0 RemainAfterExit=yes SysVStartPriority=99 [Install] WantedBy=multi-user.target
好了,然后在使用systemd开机自启动
systemctl enable rc-local.service
然后查看启动状态
#systemctl status rc-local.service ● rc-local.service - /etc/rc.d/rc.local Compatibility Loaded: loaded (/usr/lib/systemd/system/rc-local.service; enabled; vendor preset: disabled) Active: active (exited) since 一 2015-06-22 20:23:23 CST; 28min ago 6月 22 20:23:23 btdog systemd[1]: Starting /etc/rc.d/rc.local Compatibility... 6月 22 20:23:23 btdog rc.local[3973]: /etc/rc.d/rc.local:行4: [Install]: 未找到命令 6月 22 20:23:23 btdog systemd[1]: Started /etc/rc.d/rc.local Compatibility. 6月 22 20:27:28 btdog systemd[1]: Started /etc/rc.d/rc.local Compatibility. 6月 22 20:37:05 btdog systemd[1]: [/usr/lib/systemd/system/rc-local.service:20] Support for option SysVStartPriority=...ignored Hint: Some lines were ellipsized, use -l to show in full.
启动成功~~