linux设置开机服务自动启动(linux下的三种服务开机自启)

在ubuntu系统中,如果你使用的apt方式安装的软件,可以使用如下方式直接添加服务的开机自启,,现在小编就来说说关于linux设置开机服务自动启动?下面内容希望能帮助到你,我们来一起看看吧!

linux设置开机服务自动启动(linux下的三种服务开机自启)

linux设置开机服务自动启动

方式一、二、三适用于ubuntu,centos推荐使用方式二、方式三方式一

在ubuntu系统中,如果你使用的apt方式安装的软件,可以使用如下方式直接添加服务的开机自启,

如果你是手动解压缩官网下载的安装包安装的建议使用方式二、方式三。

直接使用apt进行的软件安装,一般情况下系统会默认生成开机自启的脚本文件,你只需要执行固定的命令,即可进行服务的开机自启。

mysql的开机自启

sudo update-rc.d mysql default

nginx的开机自启

sudo update-rc.d nginx default

其他软件的服务自启同理。

如需移除软件的服务自启

sudo update-rc.d -f '移除的软件' remove

方式二

简称为service方式

下面以自启mongo服务为例

1.创建脚本

sudo touch /etc/systemd/system/mongodb.service 修改文件权限: sudo chmod 777 /etc/systemd/system/mongodb.service

2.在文件中添加下面内容

[Unit] Description=High-performance, schema-free document-oriented database After=network.target [Service] User=mongodb ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf [Install] WantedBy=multi-user.target

注意修改你的mongo安装目录及配置文件路径

保存退出

3.使用命令,开机自启

sudo systemctl enable mongodb

sudo systemctl start mongodb # 启动mongo sudo systemctl status mongodb # 查看mongo状态

方式三

简称init.d方式

cd /etc/init.d touch mongodb chmod 777 /etc/init.d/mongodb update-rc.d mongodb defaults

脚本内容:

#!/bin/sh ### BEGIN INIT INFO # Provides: mongodb # Required-Start: # Required-Stop: # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: mongodb # Description: mongo db server ### END INIT INFO EXE_FILE=/home/mongo/mongodb-linux-aarch64-ubuntu1804-4.2.20/bin CONFIG_FILE=/data/MongoDB/mongodb.conf . /lib/lsb/init-functions MONGOPID=`ps -ef| grep mongod| grep -v grep| awk '{print $2}'` test -x $EXE_FILE || exit 0 case "$1" in start) ulimit -n 3000 log_begin_msg "Starting MongoDB server" $EXE_FILE --config $CONFIG_FILE log_end_msg 0 ;; stop) log_begin_msg "Stopping MongoDB server" if [ ! -z "$MONGOPID" ]; then kill -15 $MONGOPID fi log_end_msg 0 ;; status) ps -aux| grep mongod ;; *) log_success_msg "Usage: /etc/init.d/mongodb {start|stop|status}" exit 1 esac exit 0

注意修改

EXE_FILE=/home/mongo/mongodb-linux-aarch64-ubuntu1804-4.2.20/bin CONFIG_FILE=/data/MongoDB/mongodb.conf

换成你自己的目录位置

项目jar包的服务自启

这种方式,可以控制你服务的启动顺序

1.在/etc/init.d目录下先创建脚本文件,如super.sh

2.在脚本文件中添加以下内容

#!/bin/sh ### BEGIN INIT INFO # Provides: autoJar.sh # Required-start: $local_fs $remote_fs $network $syslog # Required-Stop: $local_fs $remote_fs $network $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: starts the svnd.sh daemon # Description: starts svnd.sh using start-stop-daemon ### END INIT INFO # 防止jdk加载慢 export JAVA_HOME=/usr/local/java/jdk1.8.0_131 export PATH=${JAVA_HOME}/bin:$PATH cd /usr/local/install/ nohup java -jar super-1.0-SNAPSHOT.jar > nohup.out 2>&1 &

3.增加文件权限

sudo chmod 777 /etc/init.d/super.sh

4.将脚本使用命令添加到启动脚本中去

cd /etc/init.d sudo update-rc.d -f super.sh defaults 50

5.移除启动脚本

cd /etc/init.d sudo update-rc.d -f wgkq.sh remove

原文链接:https://www.cnblogs.com/l12138h/p/16293372.html

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页