서버 로그파일 php로 내용 지우기, 파일크기 확인

서버 로그파일 php로 내용 지우기, 파일크기 확인

QA

서버 로그파일 php로 내용 지우기, 파일크기 확인

답변 1

본문

안녕하세요.

리눅스 서버의 로그파일을 php로 내용만 비우기 가능 한지요?

 

<?php

 

`cp -f /dev/null /var/log/httpd/error_log`;

 

?>

 

예전엔

error_log 파일의 파일권한을 707로 해 놓고

위와 같이 해서 비워 졌었는데,

지금은 안됩니다.

 

서버에 들어가지 않고

php 로

error_log 파일의 크기 확인(출력)

내용 비우기 방법이 궁금합니다.

이 질문에 댓글 쓰기 :

답변 1

아래의 코드를 참고해 보시겠어요..

 

 

<?php
$logfile = '/var/log/httpd/error_log';

function checkLogFile($file) {
    if (!file_exists($file)) {
        return "로그 파일이 존재하지 않습니다.";
    }
    
    if (!is_readable($file)) {
        return "로그 파일을 읽을 수 없습니다.";
    }
    
    $size = filesize($file);
    return "현재 로그 파일 크기: " . number_format($size/1024, 2) . " KB";
}

function clearLogFile($file) {
    if (!file_exists($file)) {
        return "로그 파일이 존재하지 않습니다.";
    }
    
    if (!is_writable($file)) {
        return "로그 파일에 쓰기 권한이 없습니다.";
    }
    
    try {
        file_put_contents($file, '');
        return "로그 파일이 성공적으로 비워졌습니다.";
    } catch (Exception $e) {
        return "오류 발생: " . $e->getMessage();
    }
}

// 파일 크기 확인
echo checkLogFile($logfile) . "\n";

// 파일 비우기
echo clearLogFile($logfile);
?>

 

 

권한 문제가 계속 생기면 아래의 코드도 참고를 해보세요.

 

 

# 로그파일 권한 확인
ls -l /var/log/httpd/error_log

# Apache 사용자 확인
ps aux | grep apache

# 권한 설정 (root 권한 필요)
chown www-data:www-data /var/log/httpd/error_log
chmod 664 /var/log/httpd/error_log

 

 

 

 

답변을 작성하시기 전에 로그인 해주세요.
QA 내용 검색
질문등록
filter #php ×
전체 15,417
© SIRSOFT
현재 페이지 제일 처음으로