Linux clear log file

Is there a proper way to clear logs?

I was wondering if there was a proper way to clear logs in general? I’m new to Ubuntu and I’m trying to set up Postfix. The log in question is /var/log/mail.log . I was wondering if there was a correct way to clear it, rather than me going in it and deleting all the lines and saving it. I find that sometimes errors don’t get written to it immediately after I clear the log and save it. Side note: I’m having trouble setting up Postfix and am trying to make it easier for me to read the logs hoping it can help me out, instead of having to scroll all the way down.

if you want to just see the end of the file then tail is your friend. tail /var/log/mail.log to display the last 5 lines. tail -f /var/log/mail.log to see all lines written to the end of the file.

9 Answers 9

That will truncate the log without you having to edit the file. It’s also a reliable way of getting the space back.

In general it’s a bad thing to use rm on the log then recreating the filename, if another process has the file open then you don’t get the space back until that process closes it’s handle on it and you can damage it’s permissions in ways that are not immediately obvious but cause more problems later on.

Yasar has a nice answer using truncate

Also if you are watching the contents of the log you might like to use the tail command:

Ctrl-C will break off the tailing.

/bin/csh (common for FreeBSD) would bail out this with «Invalid null command», meanwhile zsh (popular replacement for bash ) would wait EOF. See serverfault.com/a/381380/67675

how can I schedule it ? putting > syntax in crontab is not running as it may not be recognising it as a syntax

Rather than a proper way, I’d call it a when-can’t-free-space way 🙂 And not sure if it matters, but you might want to service syslog restart . See also this answer.

truncate /opt/package/logs/*.log --size 0 

Here all log files in the /opt/package/logs will become empty..

This is actually a very good answer, and really the only one that directly answers the question of if there’s a proper way to truncate logfiles. HOW it’s better than the other answers is that is doesn’t DELETE the logfile, it zeros the contents properly, thus permission errors and missing logfiles which cause some daemons to panic will not occur in this case.

Yes, there’s a proper way: You don’t clear logs at all. You rotate them. Rotation involves switching log output to a new file, under the same name, with the previous N log files kept under a set of N related filenames.

How one rotates logs depends from how one is writing them in the first place. This is an oft-overlooked point. Some of the answers here touch upon it at least, mentioning that some logging programs keep an open file descriptor for the log file, so just deleting the file won’t free up the space, or indeed even switch output to a fresh log file.

Читайте также:  Print binary file linux

If the program writing the log file is multilog from the daemontools package, for example, then you don’t do anything to rotate the logs at all — no manual scripts, no cron jobs. Simply tell multilog that log output is to a directory, and it will itself maintain an automatically rotated and size-capped set of N log files in that directory.

If the program writing the log files is svlogd from the runit package, for another example, then much the same applies. You don’t do anything at all apart from point the tool at a directory. It will itself maintain an automatically rotated and size-capped set of N log files in that directory.

If you are using rsyslog to write log files, then the logging program can be told to stop after the log file reaches a certain size and run a script. You have to write the meat of the script, to actually rename the log file and delete old log files based upon total size constraints, but at least the logging program has closed the file and paused log writing whilst this is happening.

The old syslogd way of rotating logs, still expected by logging programs such as syslog-ng and as exemplified by tools such as logrotate mentioned by djangofan in another answer here, is somewhat more haphazard. One runs a cron job that periodically renames the log files, and restarts the logging daemon (using whatever daemon supervisor it is running under). The problem with this, of course is that it doesn’t enforce an overall size cap. On slow weeks one can get N very small daily log files, whereas on busy days one can get 1 very big log file that’s well over the size limit.

This is why later and better tools like multilog and svlogd have file size configuration options and actually check the log file sizes themselves, of course. The world has learned that polling the logs on a schedule with cron jobs, or even a logrotate daemon, leaves windows for the size to be wrong, and that the proper place to have these checks, and so rigourously enforce administrator-defined size caps so that one’s log files don’t ever swallow the partition that they are on, is in the program that is actually writing the files out in the first place.

Источник

How to Empty System Log Files in Linux

Logging is a normal operation that the Linux operating system performs constantly to maintain different types of messages in various log files.

If you’re maintaining a Linux server, it’s most likely that you might have come across an issue of running out of disk space. In such a situation, emptying huge log files mainly resolve the problem.

Using the rm command to directly delete log files is what you should avoid as it can leave you in a messed up situation. In this article, we’ll see various methods to clean up log files in Linux without deleting the actual file entirely.

Читайте также:  Удаленная установка linux сети

Create A Sample Log File

Before we jump to the main topic, let’s first create a sample log file on which we’re going to perform operations. The same steps you can follow for your desired log files by using the sudo privileges.

To make a sample log file, you can use the fallocate utility using the below command:

It will give us a file with a 5MB size, which you can verify using the ls command.

List Files by Size in Linux

Clear Logs in Linux Using Cat Command

Concatenating the popular cat command with the /dev/null device file in Linux, you can easily empty the content of a log file.

In case you don’t know, /dev/null is a special file in Linux that helps in disappearing anything written or streamed to it returning the empty output.

To clear or empty any log file, just issue the following command.

$ cat /dev/null > app.log $ ls -lh app.log

Clear Log File in Linux

As you can see, instead of completely deleting the file, the command only removed the file content making its size zero.

Use Redirection Operator To Clear Log In Linux

Redirection Operator (>) is one of the easiest ways to empty the log files in the Linux operating system. Just using the redirection operator with the log filename on the right side and nothing on the left side redirects Null to the file by making it blank.

Empty Log File in Linux

Empty Log File Using ‘true’ Command In Linux

Attaching the colon (:) symbol to the left of the redirection operator makes another built-in true command that also does the same work as the redirection operator.

You can use it as given below:

Likewise, you can add true in place of :> symbol to perform the same task.

Clear Logs in Linux Using Truncate Command

As the meaning of the name says “removing part of something”, truncate is also yet another Linux utility that helps to free up space by shrinking the size of the file without deleting the file entirely.

You can utilize the truncate Linux command with the -s option that defines the file size to empty a file content. Giving a size of zero (0) is equivalent to making file content NULL or adjusting the file size to 0 bytes.

Clear Logs Using Truncate Command

As you can see in the above screenshot, we create a file app.log with a size of 5MB. Then, using a truncate command, we readjusted its size to zero without deleting the file itself.

Clear Log In Linux Using dd Command

I’m sure you must have used the dd (disk/data duplicator) command line utility to create a bootable USB without destroying your disk. The way you copy an image file to a USB boot drive, likewise, you can write blank off to your log file by just changing the input and output file.

Clear Logs Using dd Command

Here, “if” denotes the input file that you want to write to the output file as denoted by “of” .

Truncate Logs In Linux Using Echo Command

The echo command is mainly used to print or send messages in the terminal. The same functionality of the echo command can utilize to send a null output to the log file.

Читайте также:  Hosts files on linux

Simply run the below command to redirect the empty to the file:

$ echo "" > app.log Or $ echo > app.log

Truncate Logs In Linux

However, if you see in the above screenshot, the file size is still not zero meaning the file is not completely empty. This is because we redirected an empty string which is not the same as NULL.

So, to send a null output to the file and make file size zero, you also need to use the -n flag with the echo command that restricts any trailing newline or leaving any empty line as happened in the above case.

Now the file size becomes zero and there is no content in the log file.

Clear Logs In Linux Using Logrotate Tool

Coming to the last and considered one of the best-automated methods, you can also use a logrotate tool that is built specifically to manage logs. It helps in the automatic rotation, compression, and removal of log files.

Check out the separate article on how to rotate logs with Logrotate in Linux for more information.

Finally, we learned to use different command line utilities to clear logs without deleting the files entirely in the Linux operating system. You can explore each command separately to use it along with Cronjob to automate the clean-up of logs at regular intervals of time.

Источник

How to clean log file? [duplicate]

Is there a better way to clean the log file? I usually delete the old logfile and create a new logfile and I am looking for a shorter type/bash/command program. How can I use an alias?

1 Answer 1

(fell free to substitute false or any other command that produces no output, like e.g. : does in bash ) if you want to be more eloquent, will all empty logfile (actually they will truncate it to zero size).

If you want to know how long it «takes», you may use

(which is the same as dd if=/dev/null > logfile , by the way)

(or truncate -s 0 logfile ) to be perfectly explicit or, if you don’t want to,

(in which case you are relying on the common behaviour that applications usually do recreate a logfile if it doesn’t exist already).

However, since logfiles are usually useful, you might want to compress and save a copy. While you could do that with your own script, it is a good idea to at least try using an existing working solution, in this case logrotate , which can do exactly that and is reasonably configurable.

Should you need to do it for several files, the safe way is

Some shells ( zsh ) also allow one to specify several redirection targets.

This works (at least in bash ) since it creates all the redirections required although only the last one will catch any input (or none in this case). The tee example with several files should work in any case (given your tee does know how to handle several output files)

Of course, the good old shell loop would work as well:

for f in file1 file2 . ; do # pick your favourite emptying method done 

although it will be much slower due to the command being run separately for each file. That may be helped by using find :

Источник

Оцените статью
Adblock
detector