Linux服务开机自启动使用示例
本文以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 INFO
case "$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 defaults
update-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=Redis
After=network.target
[Service]
ExecStart=/usr/bin/redis-server /etc/redis.conf --daemonize no
ExecStop=/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 redis
systemctl disable redis
3)手动启动关闭redis
systemctl start redis
systemctl stop redis
systemctl restart redis
4.service和systemctl比较
systemd兼容service,更为的强大
编写服务启动脚本也更简洁,service需要编写完整的shell脚本,systemd只需要配置执行文件,pid文件路径,剩下的交给systemd去做就可以了
Linux服务开机自启动使用示例的更多相关文章
- linux 服务开机自启动systemd方式 (Centos7)
linux 服务开机自启动systemd方式 (Centos7) 1.编写一个 /opt/hello.sh 脚本 [root@jws-ftp prometheus]# cat /opt/hello.s ...
- linux服务开机自启动&注册系统服务
首先先看下linux系统开机启动顺序,如下图 对,要解决Linux CentOS 和 Red Hat Linux 系统中设置服务自启动有两种方式,就是从图中圈出的两个步骤下手. 一.修改 /etc/r ...
- Linux服务开机自启动设置
Linux中也有类似于Window中的开机自启动服务,主要是通过chkconfig命令来设置.它主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服 ...
- linux添加开机自启动脚本示例详解
linux下(以RedHat为范本)添加开机自启动脚本有两种方法,先来简单的; 一.在/etc/rc.local中添加如果不想将脚本粘来粘去,或创建链接什么的,则:step1. 先修改好脚本,使其所有 ...
- linux -- 服务开机自启动
好吧,最近需要用到开机启动服务,百度了一下,几乎都是一个版本,然后之间各种传递.我也抄个 ******************************************************* ...
- Linux(centos7)设置docker服务开机自启动以及容器自启动
docker服务开机自启动 systemctl enable docker 设置容器自启动 可以在运行的时候通过设置--restart 参数 docker run --restart always - ...
- 如何把一个c语言程序做成windows服务开机自启动
原文:如何把一个c语言程序做成windows服务开机自启动 目前写的程序是一个用c语言实现socket侦听的,那么如何把这个程序做成开机自启动呢? 我们是通过vs6.0,编译后生成了.exe文件,然后 ...
- 红帽linux系统开机自启动脚本。
其实很多东西在最后完成以后会觉得也就那样,有意思的是探究的过程. 前段时间老板要求把一个程序做成linux系统开机自启动脚本的模式. 首先你需要写一个脚本. 我这边建立了一个.sh的脚本,就是用脚本启 ...
- Linux(Debian) 上安装tomcat并注册服务开机自启动
1.准备工作 a.下载tomcat linux的包,地址:http://tomcat.apache.org/download-80.cgi,我们下载的版本是8.0,下载方式如图: b ...
随机推荐
- Ubuntu16.04下的英文词典Artha
地址: http://artha.sourceforge.net http://artha.sourceforge.net/wiki/index.php/Download 在Ubuntu下可以直接安 ...
- 通过socks tunnel设置http代理
1. 在服务器上使用ssh创建tunnel -f -C -q -N milton@112.34.113.35 # -D: 让ssh在指定的端口(-)上建立一个 SOCKS tunnel # -f: 让 ...
- 在子线程中发送短信,静态注册SentMsgReceiver。
1. 应该在子线程中执行发送短信的操作. 如果没有在子线程中发送短信会出现错误:点击发送短信之后,立即跳转到其他界面,那么这次发送短信可能就会失败! 请注意往子线程方法中传入外部的实参必须由final ...
- spring+mybatis+atomikos 实现JTA事务
1. 选择哪种transaction manager? 在单数据源情况下,JDBC,Hibernate,ibatis等自带的 transaction manager已能用于处理事务. ...
- Deep Introduction to Go Interfaces.
Standard Interface Intro Go’s interfaces are one of it’s best features, but they’re also one of the ...
- Swift3 根据秒数获取视频时长(转换成00:00:00时间格式)以及将时长转换成秒
直接代码了: /// 秒转换成00:00:00格式 /// /// - Parameter secounds: <#secounds description#> /// - Returns ...
- Weex学习与实践
Weex学习与实践(一):Weex,你需要知道的事 本文主要介绍包括Weex基本介绍.Weex源码结构.初始化工程.we代码结构.Weex的生命周期.Weex的工作原理.页面间通信.boxmodel ...
- linux达人养成计划学习笔记(三)—— 帮助命令
一.帮助命令man 1.基本使用方法: man 命令 #获取指定命令的帮助选项: -f 查看命令拥有的帮助级别 相当于whatis,也可以使用whereis来查询 -num 调用对应等级的帮助文件 - ...
- Rust hello world !
特点: 安全,速度,并发 文件:hello_world.rs 代码: fn main() { println!("hello world!"); } 执行:rustc hello_ ...
- 【C#】Skip和Tack方法实现分页
int pageIndex = SearchModel.PageIndex <= 0 ? 1 : SearchModel.PageIndex; return BatchInfoList.Skip ...