简单一言

bchat自启动脚本及手动启动检查命令

自动启动脚本
## 解决方案
### 步骤 1:重新创建启动脚本
“`
# 删除现有脚本
rm /etc/init.d/bchat

# 创建新的脚本
vi /etc/init.d/bchat
“`
### 步骤 2:使用正确的脚本格式
在打开的文件中粘贴以下内容(确保格式完全正确):

“`
#!/bin/sh
#
# bchat          Start/Stop BChat 
Server
#
# chkconfig: 2345 90 10
# description: BChat Server for chat 
room service
# processname: bluesky
# config: /www/wwwroot/chat.shimen8.
info/bchat/2000.cfg

### BEGIN INIT INFO
# Provides: bchat
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: BChat Server for chat 
room service
### END INIT INFO

# 服务路径
BChat_PATH=”/www/wwwroot/chat.
shimen8.info/bchat”
BChat_BIN=”./bluesky”
BChat_PORT=”2000″
BChat_CFG=”2000.cfg”
BChat_LOG=”start.log”

# 启动函数
start() {
    echo “Starting BChat Server…”
    su – www -s /bin/bash -c “cd 
    $BChat_PATH && $BChat_BIN 
    $BChat_PORT < $BChat_CFG > 
    $BChat_LOG 2>&1 &”
    sleep 2
    PID=$(ps -ef | grep “$BChat_BIN 
    $BChat_PORT” | grep -v grep | 
    awk ‘{print $2}’)
    if [ ! -z “$PID” ]; then
        echo “BChat Server started 
        successfully! PID: $PID”
    else
        echo “BChat Server start 
        failed!”
        return 1
    fi
}

# 停止函数
stop() {
    echo “Stopping BChat Server…”
    PID=$(ps -ef | grep “$BChat_BIN 
    $BChat_PORT” | grep -v grep | 
    awk ‘{print $2}’)
    if [ ! -z “$PID” ]; then
        kill $PID
        sleep 1
        PID=$(ps -ef | grep 
        “$BChat_BIN $BChat_PORT” | 
        grep -v grep | awk ‘{print 
        $2}’)
        if [ ! -z “$PID” ]; then
            echo “BChat Server stop 
            failed!”
            return 1
        else
            echo “BChat Server 
            stopped successfully!”
        fi
    else
        echo “BChat Server is not 
        running!”
    fi
}

# 重启函数
restart() {
    stop
    start
}

# 状态函数
status() {
    PID=$(ps -ef | grep “$BChat_BIN 
    $BChat_PORT” | grep -v grep | 
    awk ‘{print $2}’)
    if [ ! -z “$PID” ]; then
        echo “BChat Server is 
        running! PID: $PID”
        netstat -tlnp | grep “$PID”
    else
        echo “BChat Server is not 
        running!”
    fi
}

# 处理命令参数
case “$1” in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    status)
        status
        ;;
    *)
        echo “Usage: $0 {start|stop|
        restart|status}”
        exit 1
        ;;
esac

exit 0
“`
### 步骤 3:设置脚本执行权限
“`
# 设置执行权限
chmod +x /etc/init.d/bchat
“`
### 步骤 4:添加到开机自启
“`
# 添加到系统服务
chkconfig –add bchat

# 设置开机自启
chkconfig bchat on

# 查看自启状态
chkconfig –list bchat
“`
### 步骤 5:使用 systemctl 命令(推荐)
在 CentOS 7 中,推荐使用 systemctl 命令管理服务:

“`
# 启用服务(开机自启)
systemctl enable bchat

# 启动服务
systemctl start bchat

# 查看服务状态
systemctl status bchat

# 停止服务
systemctl stop bchat

# 重启服务
systemctl restart bchat
“`
## 技术说明
### 正确的 init.d 脚本格式
1. 头部注释 :必须包含 chkconfig 和 description 等字段
2. INIT INFO :CentOS 7 推荐添加 ### BEGIN INIT INFO 部分
3. 函数结构 :必须包含 start、stop、restart、status 等函数
4. 退出状态 :每个函数必须返回正确的退出状态
### 服务管理命令对比
操作 SysV 命令 Systemd 命令 启动 service bchat start systemctl start bchat 停止 service bchat stop systemctl stop bchat 重启 service bchat restart systemctl restart bchat 状态 service bchat status systemctl status bchat 启用 chkconfig bchat on systemctl enable bchat 禁用 chkconfig bchat off systemctl disable bchat

## 验证步骤
“`
# 测试服务启动
service bchat start

# 查看服务状态
service bchat status

# 检查自启设置
chkconfig –list bchat

# 检查 systemd 状态
systemctl list-unit-files | grep 
bchat

————————————————————分界线——————————————————————

一、检查数据库结构
# 检查数据库表是否存在
su – www -s /bin/bash -c “mysql -u chat_shimen8 -pxp5392026 -e ‘SHOW DATABASES;'”
su – www -s /bin/bash -c “mysql -u chat_shimen8 -pxp5392026 -e ‘USE chat_shimen8; SHOW TABLES;'”

二、检查 2000.cfg 配置文件
# 查看配置文件中的数据库连接参数
cat /www/wwwroot/chat.shimen8.info/bchat/2000.cfg

# 确保数据库连接参数与实际一致

三、停止现有服务并重新启动
# 停止现有服务
kill $(ps -ef | grep bluesky | grep -v grep | awk ‘{print $2}’)

# 以 www 用户身份重新启动服务
su – www -s /bin/bash -c “cd /www/wwwroot/chat.shimen8.info/bchat && ./bluesky 2000 < 2000.cfg > start.log 2>&1 &”

# 检查服务状态
ps -ef | grep bluesky

# 检查端口监听情况
netstat -tlnp | grep 2000
netstat -ulnp | grep 2000

四、检查服务日志
# 查看服务启动日志
cat /www/wwwroot/chat.shimen8.info/bchat/start.log

© 版权声明
THE END
喜欢就支持一下吧
点赞5赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片快捷回复

    暂无评论内容