Linux

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > Linux > linux centos7开机自启

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/rc.d/init.d
# cd /etc/rc.d/init.d
# touch 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
sudo systemctl daemon-reload
sudo systemctl enable myapp.service

第五种

使用crontab计划任务

crontab -e
@reboot /path/to/your/application

总结

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

您可能感兴趣的文章:
阅读全文