CentOS开机自启动服务内容设置
- 1. 开机后自动配置时钟同步
- 2. 开机自启动服务脚本
- 3. 配置开机自动添加路由
1. 开机后自动配置时钟同步
# cat /etc/rc.local
/usr/sbin/ntpdate pool.ntp.org >> /var/log/ntpdate.log
需要设置/etc/rc.local
的一个权限:
# ll /etc/rc.local
lrwxrwxrwx. 1 root root 13 2月 2 2018 /etc/rc.local -> rc.d/rc.local
其实/etc/rc.local
是/etc/rc.d/rc.local
的一个软连接
# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 528 11月 11 17:59 /etc/rc.d/rc.local
2. 开机自启动服务脚本
设置命令在开机时自动执行:
# cat /opt/start.sh
#!/bin/bash
nohup java -jar /opt/service-SNAPSHOT.jar --server.port=8090 > /dev/null 2>&1 &# chmod +x /opt/start.sh
# cat /etc/rc.local
/opt/start.sh# chmod +x /etc/rc.d/rc.local
# cat /etc/systemd/system/my-service.service
[Unit]
Description=My Custom Service[Service]
ExecStart=/opt/start.sh[Install]
WantedBy=multi-user.target
# systemctl enable my-service.service
# systemctl start my-service.service
# systemctl status my-service.service
● my-service.service - My Custom ServiceLoaded: loaded (/etc/systemd/system/my-service.service; enabled; vendor preset: disabled)Active: inactive (dead) since Thu 2024-11-07 15:27:54 CST; 18h agoMain PID: 491 (code=exited, status=0/SUCCESS)Nov 07 15:27:54 ecs-d091 systemd[1]: Started My Custom Service.
在系统启动后执行任务,而不是在启动过程中,您可以使用cron
:
# crontab -e
@reboot /opt/start.sh
3. 配置开机自动添加路由
# cat /etc/sysconfig/network-scripts/route-bond1
192.168.0.0/16 via 172.25.2.1 dev bond1
172.26.255.9 via 172.25.2.1 dev bond1
172.25.5.0/24 via 172.25.2.1 dev bond1