---
title: "设置 OBServer 节点开机自启动 - OceanBase 数据库 V4.3.2 | OceanBase 文档中心"
description: 设置 OBServer 节点开机自启动 本文根据 OBServer 节点是否可以连接公网环境，分为 在线设置 和 离线设置 两种方法介绍如何设置 OBServer 节点开机自启动。 说明 设置 OBServer 节点开机自启动时，OceanBase 集群的每一个 OBServer 节点都需参考本文内容进行设置。 适用…
---
切换语言

- 中文站 - 简体中文
- International - English
- 日本站 - 日本語

文档反馈![](https://mdn.alipayobjects.com/huamei_22khvb/afts/img/A*P8CuR4UJ_FkAAAAAAAAAAAAADiGDAQ/original) OceanBase 数据库分布式版 - V 4.3.2

# 设置 OBServer 节点开机自启动

更新时间：2025-01-20 15:07:33

[编辑](https://github.com/oceanbase/oceanbase-doc/edit/V4.3.2/zh-CN/700.reference/1500.Components-and-Tools/100.manage/100.obshell/210.use-case/750.set-observer-self-starting.md)  

本文根据 OBServer 节点是否可以连接公网环境，分为 **在线设置** 和 **离线设置** 两种方法介绍如何设置 OBServer 节点开机自启动。

#### 说明

设置 OBServer 节点开机自启动时，OceanBase 集群的每一个 OBServer 节点都需参考本文内容进行设置。

## 适用场景

该文档中介绍的设置方法仅适用于满足如下条件的场景：

- 当前 OceanBase 集群已部署完成，并且该集群不被 obshell 运维管理。
 - 当前 OceanBase 数据库为 V4.2.1.4 或之后版本。

## 在线设置

当节点可以连接公网时，您可登录 OBServer 节点后直接执行如下命令设置 OBServer 节点开机自启动。

```shell
[admin@test001 ~]$ sudo bash -c "$(curl -s https://obbusiness-private.oss-cn-shanghai.aliyuncs.com/download-center/opensource/obshell/service_installer.sh)"

```

## 离线设置

当节点无法连接公网时，您可登录 OBServer 节点后参照如下步骤设置 OBServer 节点开机自启动。

1. 参考如下内容创建脚本文件

   此处以脚本文件名为 `self_start.sh` 为例，您可自定义脚本文件名。

   ```shell
   [admin@test001 ~]$ vim self_start.sh

   ```

   脚本内容如下：

   ```shell
   #!/bin/bash

   # Prompt the user for input and retrieve it
   prompt_for_input() {
       local prompt_message="$1"
       local user_input
       read -p "$prompt_message" user_input
       echo "$user_input"
   }

   # Check if the homepath argument was provided via the command line
   expected_arg_name="homepath"
   if [ $# -gt 0 ]; then
       arg_value="$1"
   else
       # No arguments provided, ask the user for the homepath
       arg_value=$(prompt_for_input "Please enter the $expected_arg_name: ")
   fi
   echo "Using $expected_arg_name: $arg_value"

   # Check for the existence of the 'obshell' file in the provided homepath
   homepath=$arg_value
   obshell="$homepath/bin/obshell"
   if [ ! -f "$obshell" ]; then
       echo -e "\033[31m[ERROR]\033[0m Sorry, [$obshell] does not exist."
       echo "You may need to upgrade the obcluster. Please consult the developers for assistance."
       exit 1
   else
       echo "[$obshell] has been found in the work directory."
   fi

   # Check if the oceanbase.service file exists
   system_dir="/etc/systemd/system"
   oceanbase=$system_dir/oceanbase.service
   # Check if the service file exists
   if [ -f "$oceanbase" ]; then
       echo -e "\033[33m[WARN]\033[0m oceanbase.service has been installed."
       echo "Please use 'systemctl status oceanbase.service' to check it."
       exit 0
   else
       echo "Service file $oceanbase does not exist."
   fi

   # Retrieve the owner of the 'observer' process configuration files
   owner=$(stat -c '%U' $homepath/etc)
   echo "Owner of the observer configuration: $owner"

   # Construct and output the content for the systemd service unit file
   name=observer.service
   mkdir -p $homepath/tmp
   file=$homepath/tmp/$name

   start_cmd="${homepath}/bin/obshell cluster start"
   V4231="4.2.3.1"
   version=`${homepath}/bin/obshell version`
   if printf '%s\n' "$V4231" "$version" | sort -CV; then
       start_cmd="${homepath}/bin/obshell admin start --takeover 0 --ob"
   fi

   echo "Creating the service unit file at $file..."
   # Write the service content to the file
   cat << EOF > ${file}
   [Unit]
   Description=observer
   After=network.target
   [Service]
   User=${owner}
   Type=forking
   KillSignal=SIGKILL
   ExecStartPre=${homepath}/bin/obshell admin stop
   ExecStart=${start_cmd}
   ExecStartPost=${homepath}/bin/obshell admin stop
   ExecStop=${homepath}/bin/obshell admin stop
   PIDFile=${homepath}/run/observer.pid
   Restart=no
   SuccessExitStatus=SIGKILL
   [Install]
   WantedBy=multi-user.target
   EOF
   echo "The content of the Service unit file is:"
   sed 's/^/  /' "$file"

   # Deploy the unit configuration file to the system directory
   echo "Deploying the service unit file to the system directory"
   cp -f $file $system_dir/$name
   echo "Updating permissions for the service unit file..."
   chmod 644 $system_dir/$name
   echo "Reloading the systemd daemon to recognize the new service"
   systemctl daemon-reload
   systemctl enable observer.service
   echo -e "\033[32m[SUCCEED]\033[0m observer.service has been installed."

   ```
 2. 执行如下命令启动脚本

   此处以 OceanBase 集群的安装目录为 `/home/admin/oceanbase` 为例，您需根据实际情况进行替换。

   ```shell
   [admin@test001 ~]$ sudo sh self_start.sh /home/admin/oceanbase

   ```

 上一篇 下一篇 ![有帮助](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*y6ocSqN8cqsAAAAAAAAAAAAAARQnAQ)![无帮助](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*BG9IQJyLHF8AAAAAAAAAAAAAARQnAQ)![反馈](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*eTWdQKCRKHwAAAAAAAAAAAAAARQnAQ)[AI](https://www.oceanbase.com/obi) 咨询热线
