linux添加开机启动任务

linxu添加开启启动任务

本机为fedora 26

先创建rc.local文件,编辑rc.local

1
2
sudo touch /etc/rc.d/rc.local
sudo vim /etc/rc.d/rc.local

写入执行脚本

1
2
#!/bin/bash
cd /home/CaseZheng/CaseZhengBlog && nohup hexo serve &

给予执行权限

1
sudo chmod +x /etc/rc.d/rc.local

执行下面的命令

1
sudo systemctl enable rc-local.service

如果报错,编辑rc-local.service

1
sudo vim /usr/lib/systemd/system/rc-local.service

在最后面加入下列内容

1
2
[Install]
WantedBy=multi-user.target

再执行命令

1
sudo systemctl enable rc-local.service

然后重启

1
reboot

使用systemctl查看rc-local.service状态

1
2
3
4
5
6
7
8
[CaseZheng@localhost CaseZhengBlog]$ sudo 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 (running) since Sat 2017-12-30 23:45:57 CST; 1h 3min ago
Tasks: 11 (limit: 4915)
CGroup: /system.slice/rc-local.service
├─852 /bin/bash /etc/rc.d/rc.local start
└─859 hexo

如有错误会有报错信息