Serv00 部署案例和常用命令

常用命令

Serv00重置系统命令a

  1. 更改权限:

    1
    2
    find ~ -type f -exec chmod 644 {} \; 2>/dev/null
    find ~ -type d -exec chmod 755 {} \; 2>/dev/null
  2. 删除文件:

    1
    find ~ -type f -exec rm -f {} \; 2>/dev/null
  3. 删除空目录:

    1
    find ~ -type d -empty -exec rmdir {} \; 2>/dev/null
  4. 再次尝试删除剩余的文件和目录:

    1
    find ~ -exec rm -rf {} \; 2>/dev/null

    Serv00重置系统命令b

    1
    bash <(curl -Ls https://raw.githubusercontent.com/k0baya/x-for-serv00/main/reset.sh)

Serv00重启命令

  1. 命令:

    1
    pkill -kill -u <username>

Serv00 PHP保活

  1. 打开Serv00域名的php exec

    V5GiWj

  2. 进入需要保活的Serv00终端,执行

    1
    bash <(curl -sL https://txt.0nz.de/serv00.sh)
  3. 将生成的网页链接加入收藏夹,手动点击,

    或者进入网站:https://console.cron-job.org添加定时任务

    WhbuNc

    4.感谢[email protected]大神的脚本

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    #!/bin/bash

    # 生成随机字符串函数
    generate_random_string() {
    local length=$1
    local result=""
    local chars="abcdefghijklmnopqrstuvwxyz0123456789"
    for i in $(seq 1 $length); do
    result+=${chars:$((RANDOM % ${#chars})):1}
    done
    echo "$result"
    }

    # 生成随机密码函数
    generate_random_password() {
    local length=$1
    local result=""
    local chars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    for i in $(seq 1 $length); do
    result+=${chars:$((RANDOM % ${#chars})):1}
    done
    echo "$result"
    }

    # 询问是否使用 serv00 自带域名
    read -p "是否使用 serv00 自带域名?[Y/n]: " use_serv00

    # 将回车或空值设为默认值 y
    use_serv00=${use_serv00:-y}
    # 转换为小写进行比较
    use_serv00=$(echo "$use_serv00" | tr '[:upper:]' '[:lower:]')

    if [ "$use_serv00" = "y" ] || [ "$use_serv00" = "yes" ]; then
    user="$(whoami)"
    domain="${user}.serv00.net"
    else
    read -p "请输入域名: " domain
    fi

    # 询问PHP文件名
    read -p "是否使用随机PHP文件名?[Y/n]: " use_random_filename
    use_random_filename=${use_random_filename:-y}
    use_random_filename=$(echo "$use_random_filename" | tr '[:upper:]' '[:lower:]')

    if [ "$use_random_filename" = "y" ] || [ "$use_random_filename" = "yes" ]; then
    php_filename=$(generate_random_string 6)
    echo "生成的随机PHP文件名: ${php_filename}.php"
    else
    read -p "请输入PHP文件名(不需要输入.php后缀): " php_filename
    fi

    # 询问密码设置
    read -p "是否使用自动生成的8位随机密码?[Y/n]: " use_random_pwd

    # 将回车或空值设为默认值 y
    use_random_pwd=${use_random_pwd:-y}
    # 转换为小写进行比较
    use_random_pwd=$(echo "$use_random_pwd" | tr '[:upper:]' '[:lower:]')

    if [ "$use_random_pwd" = "n" ] || [ "$use_random_pwd" = "no" ]; then
    read -p "请输入访问密码: " password
    else
    password=$(generate_random_password 8)
    echo "已生成随机密码: $password"
    fi

    # 设置安装路径
    installpath="$HOME"
    domainPath="$installpath/domains/$domain/public_html"

    # 创建必要的目录
    mkdir -p "$domainPath"

    # 生成 index.php 文件
    cat > "$domainPath/${php_filename}.php" << EOL
    <?php
    \$valid_password = "$password";
    $(cat << 'EOF'

    // 获取并验证参数
    $password = $_GET['pwd'] ?? '';
    $cronCommand = $_GET['cronCommand'] ?? '';
    $timeInterval = isset($_GET['timeInterval']) ? (int)$_GET['timeInterval'] : 15;
    $isApiCall = isset($_GET['api']); // 检查是否存在api参数

    // 验证密码和参数
    if ($password !== $valid_password) {
    if ($isApiCall) {
    header('Content-Type: application/json');
    die(json_encode(['success' => false, 'message' => 'Invalid password']));
    }
    die("hello world");
    }

    if (empty($cronCommand)) {
    if ($isApiCall) {
    header('Content-Type: application/json');
    die(json_encode(['success' => false, 'message' => 'cronCommand cannot be empty']));
    }
    die("cronCommand cannot be empty!");
    }

    // 设置默认的时间间隔为10分钟
    $timeInterval = isset($_REQUEST['timeInterval']) ? intval($_REQUEST['timeInterval']) : 10;

    // 验证时间间隔必须大于0
    if ($timeInterval < 1) {
    if ($isApiCall) {
    header('Content-Type: application/json');
    die(json_encode(['success' => false, 'message' => 'timeInterval must be greater than 0']));
    }
    die("timeInterval must be greater than 0!");
    }

    function checkCronExists($cronCommand) {
    $currentCron = shell_exec('crontab -l');
    return strpos($currentCron, $cronCommand) !== false;
    }

    function addCron($timeInterval, $cronCommand) {
    if (checkCronExists($cronCommand)) {
    return "Cron task already exists.";
    }

    try {
    // 获取当前的 crontab
    $currentCron = shell_exec('crontab -l') ?: '';

    // 添加新的 cron 任务
    $newCron = $currentCron;
    if (!empty($newCron) && substr($newCron, -1) !== "\n") {
    $newCron .= "\n";
    }
    $newCron .= "*/{$timeInterval} * * * * bash {$cronCommand} > /dev/null 2>&1\n";

    // 写入临时文件
    $tempFile = tempnam(sys_get_temp_dir(), 'cron');
    file_put_contents($tempFile, $newCron);

    // 导入新的 crontab
    exec("crontab $tempFile");

    // 删除临时文件
    unlink($tempFile);

    // 验证是否成功添加
    if (checkCronExists($cronCommand)) {
    return "Cron task added successfully.";
    } else {
    return "Failed to add cron task. Please check system permissions.";
    }
    } catch (Exception $e) {
    return "Error: " . $e->getMessage();
    }
    }

    // 执行添加操作
    $result = addCron($timeInterval, $cronCommand);

    // 如果存在api参数,返回JSON
    if ($isApiCall) {
    header('Content-Type: application/json');
    echo json_encode([
    'success' => (strpos($result, 'successfully') !== false),
    'message' => $result,
    'currentCrontab' => shell_exec('crontab -l')
    ]);
    exit;
    }

    // 否则显示HTML页面
    ?>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Add Cron Task</title>
    <style>
    :root {
    --primary-color: #2196F3;
    --bg-color: #f8f9fa;
    --text-color: #333;
    --card-bg: #ffffff;
    }

    * {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    }

    body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
    background: var(--bg-color);
    color: var(--text-color);
    line-height: 1.6;
    padding: 20px;
    max-width: 800px;
    margin: 0 auto;
    }

    .card {
    background: var(--card-bg);
    border-radius: 12px;
    padding: 20px;
    margin: 15px 0;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    transition: transform 0.2s;
    }

    .card:hover {
    transform: translateY(-2px);
    }

    .title {
    font-size: 1.5rem;
    color: var(--primary-color);
    margin-bottom: 20px;
    font-weight: 600;
    }

    .params {
    text-align: left;
    }

    .param-item {
    padding: 8px 0;
    border-bottom: 1px solid #eee;
    }

    .param-item:last-child {
    border-bottom: none;
    }

    .crontab {
    font-family: monospace;
    background: #f5f5f5;
    padding: 10px;
    border-radius: 6px;
    overflow-x: auto;
    white-space: pre-wrap;
    }

    @media (max-width: 600px) {
    body {
    padding: 10px;
    }

    .card {
    padding: 15px;
    margin: 10px 0;
    }

    .title {
    font-size: 1.2rem;
    }
    }
    </style>
    </head>
    <body>
    <div class="title">Cron Task Manager</div>

    <div class="card">
    <div class="params">
    <div class="param-item">
    <strong>Command:</strong> <?php echo htmlspecialchars($cronCommand); ?>
    </div>
    <div class="param-item">
    <strong>Interval:</strong> <?php echo htmlspecialchars($timeInterval); ?> minutes
    </div>
    </div>
    </div>

    <div class="card">
    <strong>Status:</strong>
    <div class="param-item">
    <?php echo $result; ?>
    </div>
    </div>

    <div class="card">
    <strong>Current Crontab:</strong>
    <div class="crontab">
    <?php echo nl2br(htmlspecialchars(shell_exec('crontab -l'))); ?>
    </div>
    </div>
    </body>
    </html>
    EOF
    )
    EOL

    # 询问是否需要新手指导
    read -p "是否需要新手指导?[Y/n]: " need_guide
    need_guide=${need_guide:-y}
    need_guide=$(echo "$need_guide" | tr '[:upper:]' '[:lower:]')

    if [ "$need_guide" = "y" ] || [ "$need_guide" = "yes" ]; then
    # 获取定时命令
    read -p "请输入需要定时执行的命令路径(例如: /www/wwwroot/script/keepalive.sh): " cronCommand

    # 获取时间间隔,默认10分钟
    read -p "请输入执行间隔时间(分钟)[默认10分钟]: " timeInterval
    timeInterval=${timeInterval:-10}

    echo "================================================================"
    echo "安装完成!"
    echo "根据您的输入,访问地址如下:"
    echo "网页访问: http://$domain/${php_filename}.php?pwd=$password&cronCommand=$cronCommand&timeInterval=$timeInterval"
    echo "JSON访问: http://$domain/${php_filename}.php?pwd=$password&cronCommand=$cronCommand&timeInterval=$timeInterval&api"
    else
    echo "================================================================"
    echo "安装完成!"
    echo "访问地址示例:"
    echo "网页访问: http://$domain/${php_filename}.php?pwd=$password&cronCommand=/path/to/your/script.sh&timeInterval=10"
    echo "JSON访问: http://$domain/${php_filename}.php?pwd=$password&cronCommand=/path/to/your/script.sh&timeInterval=10&api"
    fi
    echo "================================================================"

Serv00 部署案例和常用命令
http://example.com/2024/12/15/Serv00-部署案例和常用命令/
作者
Justin
发布于
2024年12月15日
许可协议