博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux服务开机自启动使用示例
阅读量:7231 次
发布时间:2019-06-29

本文共 2720 字,大约阅读时间需要 9 分钟。

本文以redis服务为例,介绍了两种服务自启动的方法service,systemctl使用示例

1.修改redis.conf,允许后台运行

daemonize no 改为 daemonize yes

2.使用service命令

1)编写启动shell脚本,命名为redis
redis文件代码如下:

#!/bin/sh#Configurations injected by install_server below....EXEC=/home/shijingjing/redis-4.0.2/src/redis-server   # redis-server的路径CLIEXEC=/home/shijingjing/redis-4.0.2/src/redis-cli   # redis-cli的路径PIDFILE=/var/run/redis_6379.pid  # redis.conf里有这个参数,将其复制过来CONF="/home/shijingjing/redis-4.0.2/redis.conf"  # redis.conf的路径REDISPORT="6379"  # 端口################ SysV Init Information# chkconfig: - 58 74# description: redis is the redis daemon.### BEGIN INIT INFO# Provides: redis# Required-Start: $network $local_fs $remote_fs# Required-Stop: $network $local_fs $remote_fs# Default-Start: 2 3 4 5# Default-Stop: 0 1 6# Should-Start: $syslog $named# Should-Stop: $syslog $named# Short-Description: start and stop redis# Description: Redis daemon### END INIT INFOcase "$1" in    start)        if [ -f $PIDFILE ]        then            echo "$PIDFILE exists, process is already running or crashed"        else            echo "Starting Redis server..."            $EXEC $CONF        fi        ;;    stop)        if [ ! -f $PIDFILE ]        then            echo "$PIDFILE does not exist, process is not running"        else            PID=$(cat $PIDFILE)            echo "Stopping ..."            $CLIEXEC -p $REDISPORT shutdown            while [ -x /proc/${PID} ]            do                echo "Waiting for Redis to shutdown ..."                sleep 1            done            echo "Redis stopped"        fi        ;;    status)        PID=$(cat $PIDFILE)        if [ ! -x /proc/${PID} ]        then            echo 'Redis is not running'        else            echo "Redis is running ($PID)"        fi        ;;    restart)        $0 stop        $0 start        ;;    *)        echo "Please use start, stop, restart or status as first argument"        ;;esac

将redis文件放入/etc/init.d文件夹中,也可以直接将安装目录下utils/redis_init_script文件拷贝到该目录下

更改文件为可执行

chmod +x redis

2)将执行文件放入,移除开机自启动

update-rc.d redis defaultsupdate-rc.d redis remove

3)手动启动关闭redis

/etc/init.d/redis start/etc/init.d/redis stop/etc/init.d/redis restart

3.使用systemctl命令

1)编写.service文件,redis.service

[Unit]Description=RedisAfter=network.target[Service]ExecStart=/usr/bin/redis-server /etc/redis.conf  --daemonize noExecStop=/usr/bin/redis-cli -h 127.0.0.1 -p 6355 shutdown[Install]WantedBy=multi-user.target

将redis.service放入/etc/systemd/system文件夹下

2)将服务加入,移除开机自启动

systemctl enable redissystemctl disable redis

3)手动启动关闭redis

systemctl start redissystemctl stop redissystemctl restart redis

4.service和systemctl比较

systemd兼容service,更为的强大
编写服务启动脚本也更简洁,service需要编写完整的shell脚本,systemd只需要配置执行文件,pid文件路径,剩下的交给systemd去做就可以了

转载地址:http://zapfm.baihongyu.com/

你可能感兴趣的文章
英特尔® Software Guard Extensions 教程系列:第一部分,英特尔® SGX 基础
查看>>
ASP.NET WebApi OWIN 实现 OAuth 2.0(自定义获取 Token)
查看>>
Docker基本使用命令
查看>>
surfaceview组件的surfaceCreated()不被调用的解决方案
查看>>
2018 eclipse安装反编译插件
查看>>
Swift - AnyClass,元类型和 .self
查看>>
Swift实时画箭头的实现
查看>>
maven报错
查看>>
为什么我没有在Paragon NTFS上找到卸载的地方
查看>>
2018.7.13vue知识小结
查看>>
使用git工具上传自己的程序到github上
查看>>
(转)git checkout 撤销修改
查看>>
Android Retrofit2 数据解析
查看>>
如何实现‘请在微信客户端打开链接’
查看>>
解决Jira和Confluence访问打开越来越缓慢问题
查看>>
000 Security的计划
查看>>
html+css+js实现科学计算器
查看>>
文件上传的三种模式-Java
查看>>
【IntellJ IDEA】idea上所有代码都报错了
查看>>
xdebug php 运行效率分析工具
查看>>