一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

linuxcentos7 开机自启实现过程

时间:2026-06-22 09:57:48 编辑:袖梨 来源:一聚教程网

第一种

vim /etc/rc.local

linuxcentos7开机自启实现过程

#!/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 ApplicationAfter=network.target[Service]ExecStart=/path/to/your/applicationRestart=alwaysUser=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

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持本站。

您可能感兴趣的文章:
  • Centos7设置postgresql数据库开机自启动方式
  • 阿里云Centos7下安装Redis及tomcat设置自启动的方法
  • 详解Centos7中Nginx开机自启动的解决办法

热门栏目