首页 > 服务器    日期:2026-06-20 / 浏览

第一种

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

总结

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

觉得上面的内容有用吗?快来点个赞吧!

点赞() 我要打赏

温馨提示 : 本站内容来自会员投稿以及互联网,所有源码及教程均为作者总结编辑,请大家在使用过程中提前做好备份,以免发生无法预知的错误,源码类教程请勿直接用于生产环境!

 可能感兴趣的文章