Centos使用systemctl管理服务,实现在服务的自动启动

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lianshaohua/article/details/88679860

把自定义的服务添加到系统服务,并通过Systemctl管理

    1.写服务文件

[Unit]:服务说明

Description:服务描述

After:描述服务类别

 

[Service]服务运行参数的设置

Type=forking是后台运行的形式

ExecStart服务的具体运行命令

ExecReload重启命令

ExecStop停止命令

PrivateTmp=True是否给服务分配独立的temp空间

注:启动、重启、停止命令全部要求使用绝对路径

[Install]服务安装的相关设置,可设置为多用户

    示例:reas.service

使用vim创建一个reas.service,然后输入以下内容:

[Unit]

Description=reas web service

After=network.target remote-fs.target nss-lookup.target



[Service]

Type=forking  #或simple

ExecStart=/usr/bin/java -jar /web/reas/reas.jar start

ExecReload=/usr/bin/java -jar /web/reas/reas.jar restart

ExecStop=/usr/bin/java -jar /web/reas/reas.jar stop

[Install]

WantedBy=multi-user.target

保存文件

2.保存目录

# 修改权限为754

chmod 754 reas.service

sudo copy reas.service /usr/lib/systemd/system 

3.设置开机自启动

# 任意目录下执行

systemctl enable reas.service 

4.其他命令

# 启动reas服务

systemctl start reas.service

# 设置开机自启动

systemctl enable reas.service

# 停止开机自启动

systemctl disable reas.service

# 查看服务当前状态

systemctl status reas.service

# 重新启动服务

systemctl restart reas.service

# 查看所有已启动的服务

systemctl list-units --type=service

猜你喜欢

转载自blog.csdn.net/lianshaohua/article/details/88679860
今日推荐