linux centos7开机自启实现过程
作者:只有一个途径
本文详细介绍了五种Linux系统自启动方法,包括vim编辑/etc/rc.local文件、在/etc/profile.d下写.sh脚本文件、切换root账户创建启动脚本、配置systemd服务单元文件以及使用crontab计划任务设置自启动;适合系统管理员参考
第一种
vim /etc/rc.local
#!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don't # want to do the full Sys V style init stuff. touch /var/lock/subsys/local # 使用 bitlink 用户及 用户环境 后台执行 start.sh &后台运行 su - bitlink -c "/home/bitlink/service/dc/start.sh &"
第二种
也是可以的 但是有一个弊端是重启之后需要root用户登录一次 才能执行。原因不详。
- 在/etc/profile.d/下写.sh文件
- 在/etc/profile.d/下写.sh文件,reboot即可
- /etc/profile会遍历/etc/profile.d/*.sh
第三种
- 先切换root账户
- 切换目录
/etc/rc.d/init.d # cd /etc/rc.d/init.d
- 创建脚本文件
# touch bitlink.sh
- 脚本文件填入如下内容(可参考部署包-脚本里的bitlink.sh文件)
#!/bin/sh #chkconfig: 2345 20 80 ##普通用户启动服务 /bin/su - carbon -c "sh /home/carbon/service/start_jar.sh" /bin/su - carbon -c "docker-compose -f /home/carbon/software/middleware.yml up -d"
- 设置权限
# chmod +x bitlink.sh
- 添加服务
# chkconfig --add bitlink.sh
- 开机自启动
# chkconfig bitlink.sh on
第四种
CentOS 7采用systemd作为初始化系统,这是配置进程自启动的推荐方法:
- 创建服务单元文件:
sudo vim /etc/systemd/system/myapp.service
- 编辑文件内容:
[Unit] Description=My Application After=network.target [Service] ExecStart=/path/to/your/application Restart=always User=youruser [Install] WantedBy=multi-user.target
- 重载systemd配置:
sudo systemctl daemon-reload
- 启用服务:
sudo systemctl enable myapp.service
第五种
使用crontab计划任务
- 编辑crontab:
crontab -e
- 添加以下内容:
@reboot /path/to/your/application
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
