服务自启动
systemctl 命令介绍
- Linux服务管理两种方式service和systemctl
- systemd是Linux系统最新的初始化系统(init),作用是提高系统的启动速度,尽可能启动较少的进程,尽可能更多进程并发启动。
- systemd对应的进程管理命令是systemctl
systemctl命令兼容了service
常用命令:
systemctl [command] [unit]
# 查看状态
systemctl status 服务名
# 启动
systemctl start 服务名
# 停止
systemctl stop 服务名
# 重启
systemctl restart 服务名
# 设置开机自启动
systemctl enable 服务名
# 设置开机不启动
systemctl disable 服务名
参考链接:
服务器启动jar包
首先进入到/etc/systemd/system
目录下
cd /etc/systemd/system
新建服务文件
vim yourProjectName.service
服务文件内容
[Unit]
Description=data hub service #描述
After=syslog.target #依赖
[Service]
ExecStart=/bin/sh -c "exec /usr/bin/java -jar /home/yunhe/service/yunhe-maquan.jar>>/home/yunhe/log.log 2>&1"
[Install]
WantedBy=multi-user.target
修改配置文件后需要通过命令systemctl daemon-reload
重新加载配置文件
systemctl enable 服务名
设置开机自启
systemctl start 服务名
启动服务
systemctl stop 服务名
停止服务
postgresql开机启动
通过rpm包安装的postgresql是有自动注册服务的。
以postgresql-11为例
查看状态
systemctl status postgresql-11
启动
systemctl start postgresql-11
重启
systemctl restart postgresql-11
通过查看状态发现postgresql-11是开机自启的;如果状态不对通过systemctl enable postgresql-11
设置即可
nginx开机自启
通过rpm安装的nginx
- 首先可通过
systemctl status nginx
查看安装nginx是否已注册服务 - 通过
systemctl start nginx
启动服务时报错 - 而我们通过
ps -ef|grep nginx
命令查到nginx是在启动的。 - 通过
pkill -9 nginx
杀死nginx进程 - 再次启动启动成功
- 再通过
systemctl enable nginx
设置开机自启
参考链接:
Redis 开机自启
通过rpm包进行安装redis
- 编辑 redis.conf配置文件,设置后台启动redis服务
vim /etc/redis.conf
把文件中的daemonize属性改为yes(表明需要在后台运行)
(如需设置密码和远程访问自行设置)
- 在根目录下查找通过
find -name 文件名
查找redis启动文件 - 再通过命令
./usr/bin/redis-server /etc/redis.conf
启动redis
可通过查看 redis 进程,或 redis-cli 连接 redis 确认是否启动成功 - rpm包安装redis已自动注册服务,通过
systemctl status redis
查看服务状态 - 停止 redis 进程再通过
systemctl start redis
启动 redis 服务,启动失败;通过journalctl -xe
查看服务日志 - 通过
chmod 777 /var/log/redis/redis.log
命令修改文件权限,再通过systemctl start redis
启动redis,启动成功 systemctl enable redis
设置开机自启。