[Linux]创建server服务器并将数据存入log文件并只保留最后10000行

————————–baseport.c————————————-
#include
#include
#include
#include
#include
#include

#define PORT 8080 // 服务器端口
#define MAX_CLIENTS 5 // 最大连接数
#define BUFFER_SIZE 1024 // 数据缓冲区大小

int client_sockets[MAX_CLIENTS]; // 存储客户端套接字

// 结构体用于传递客户端套接字和地址信息
typedef struct {
int socket;
struct sockaddr_in address;
} client_info;

void *handle_client(void *arg) {
client_info *c_info = (client_info *)arg;
int client_socket = c_info->socket;
struct sockaddr_in client_address = c_info->address;
char buffer[BUFFER_SIZE];

// 获取并打印客户端IP地址
char client_ip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &client_address.sin_addr, client_ip, sizeof(client_ip));
printf(“Client connected: %s\n”, client_ip);

while (1) {
memset(buffer, 0, BUFFER_SIZE);

int bytes_received = recv(client_socket, buffer, BUFFER_SIZE, 0);
if (bytes_received <= 0) { printf("Client disconnected or error occurred.\n"); break; // 连接关闭 } printf("Received from %s: %s\n", client_ip, buffer); send(client_socket, buffer, bytes_received, 0); } close(client_socket); printf("Closed connection for socket %d (%s)\n", client_socket, client_ip); free(c_info); // 释放分配的内存 return NULL; } void manage_connections(int new_socket, struct sockaddr_in client_address) { int i; // 将变量声明移至循环外 // 关闭最早的连接 for (i = 0; i < MAX_CLIENTS; i++) { if (client_sockets[i] == 0) { // 找到空闲位置 client_sockets[i] = new_socket; return; } } // 如果达到最大连接数,关闭最早的连接 close(client_sockets[0]); // 将新的连接插入到数组中 for (i = 0; i < MAX_CLIENTS - 1; i++) { client_sockets[i] = client_sockets[i + 1]; } client_sockets[MAX_CLIENTS - 1] = new_socket; } int main() { int server_fd, new_socket; struct sockaddr_in address, client_address; int opt = 1; socklen_t addrlen = sizeof(client_address); // 初始化客户端套接字数组 memset(client_sockets, 0, sizeof(client_sockets)); // 创建套接字 if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) { perror("Socket failed"); exit(EXIT_FAILURE); } // 绑定套接字 if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) { perror("Setsockopt failed"); exit(EXIT_FAILURE); } address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; address.sin_port = htons(PORT); if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) { perror("Bind failed"); exit(EXIT_FAILURE); } // 开始监听 if (listen(server_fd, 3) < 0) { perror("Listen failed"); exit(EXIT_FAILURE); } printf("Server listening on port %d\n", PORT); while (1) { // 等待客户端连接 if ((new_socket = accept(server_fd, (struct sockaddr *)&client_address, &addrlen)) < 0) { perror("Accept failed"); exit(EXIT_FAILURE); } printf("New connection: socket fd is %d\n", new_socket); // 管理连接 manage_connections(new_socket, client_address); // 创建线程处理客户端 client_info *c_info = malloc(sizeof(client_info)); // 分配内存以存储客户端信息 c_info->socket = new_socket;
c_info->address = client_address;
pthread_t thread_id;
pthread_create(&thread_id, NULL, handle_client, (void *)c_info);
}

// 关闭服务器套接字
close(server_fd);
return 0;
}

—————————————–编译——————————————-
gcc -pthread -o baseport.o server.c

—————————在阿里云服务器运行、将结果打印到server.out文件,禁止缓存 执行—-
nohup stdbuf -oL ./baseport.o> baseport.log 2>&1 &

———————在/root/baseport/ 新建 baseport_last_10000_lines.sh —–并添加可执行属性—-

#!/bin/bash

# 设置日志文件路径
LOG_FILE=” /root/baseport/baseport.log”

# 仅保留最后 1000 行
tail -n 1000 “$LOG_FILE” > “$LOG_FILE.tmp” && mv “$LOG_FILE.tmp” “$LOG_FILE”

————————-执行—————————–
crontab -e

发表评论

邮箱地址不会被公开。 必填项已用*标注