CentOS 中注册服务并随系统启动
我们在使用Linux作为服务器的时候,可能想着有些服务在系统重启后会自动启动,在有些安装版的软件中, 安装的过程就已经注册了服务服务,但是有些非安装的就要自动注册。
向Linux注册服务大致有两种方案
- chkconfig
- systemd
1.chkconfig
为要执行的脚本建立连接,例如:
sudo ln -s $NEXUS_HOME/bin/nexus /etc/init.d/nexus
软连接一定要放到init.d目录下。
cd /etc/init.d
sudo chkconfig --add nexus
sudo chkconfig --levels 345 nexus on
sudo service nexus start
第二个命令将nexus添加为要使用该命令启动和停止的服务。 chkconfig Manager /etc/rc[0-6].d中的符号链接,它控制在操作系统重新启动或在运行级别之间转换时要启动和停止的服务。 第三个命令将nexus添加到运行级别3,4和5.然后服务命令启动资源库管理器。
2. systemd
创建一个名为xxx.service的文件,添加以下内容并将此文件保存(内容为示例)到/etc/systemd/system/目录中,注意文件的权限。
[Unit]
Description=nexus service
After=network.target
[Service]
Type=forking
ExecStart=/opt/nexus/bin/nexus start
ExecStop=/opt/nexus/bin/nexus stop
User=nexus
Restart=on-abort
[Install]
WantedBy=multi-user.target
通过面的命令激活此服务
sudo systemctl daemon-reload
sudo systemctl enable nexus.service
sudo systemctl start nexus.service
参考地址:https://help.sonatype.com/repomanager3/installation/run-as-a-service