Option to purge the log

I am getting somewhere, but not there yet.
So made the error message when doing a config check go away by using my own defined text “system_log”.

dokscript

clearHA

Restarted Hassio (0.71) but no “Clear Log HA” appeard in my system. And I am not getting button “Activate”?
Do I have to bind the script/shell comands to a switch.And if so, please give an example of the code?

Create sensor:

6

Add to Automation:

7

1 Like

@Vlad
The shell commands:
clear_log_ha: rm home-assistant.log
create_log_ha: touch home-assistant.log
set_perm_ha: chmod 777 home-assistant.log

Your shell commands does delete and creates a new file. But HA (Hassio 0.71.0) cannot write log entries into the new file. The log file stays empty after activating the script. Have monitored it for days. And the log files are still empty.

Any ideas?

1 Like

rm is remove command.
Try to use truncate instead.
Like:
truncate home-assistant.log --size 0
or
truncate -s 0 home-assistant.log

1 Like

Thats a winner. The truncate worked fine!

ha_log_truncate: truncate -s 0 home-assistant.log

Reading this thread I’d like to point out that the myriad “answers” make a strong case for what was the original point made by the OP…

This shouldn’t be so stupid and difficult.

2 Likes

OP requested a button to clear the logs in the info panel.
Now there is a button for that.
image
and a service system_log.clear

Haha, that button doesn’t actually clear the log. :stuck_out_tongue:

1 Like

It does clear the log on that page :wink:
Just not the log file.

Just for full understanding, the truncate command makes the shell_commands
create_log and
set_perm
obsolete. Truncate keeps the file in tact and deletes the contents of the logfile.

I have opposite problem. My log is being cleared each time Hassio spontaneously restarts. I would like to keep the log to diagnose the reason.

I find this useful but as a conclusion what are the steps/code for this to work?
Because I see truncate and from there I cannot understand anything.

If you’d like to clear the log just create a script called empty_log in the scripts.yaml file. The script calls the shell command. I have commented out the shell commands that are not needed if you use the truncate command:

# Empty the log file
empty_log:
  alias: Clear log HA
  sequence: 
    - service: shell_command.clear_log
#    - service: shell_command.create_log
#    - service: shell_command.set_perm

In fact, if you only have the one shell command, you can just use that in an automation, if you prefer.

Thank you.
I am only intrested in clearing the log, not delete the file (there is no use to delete it as long as the contents are emptied)

So this is the only entry to clear the logs? No other code in another yaml file?
(I see that clear_log.yaml with a command inside it)

That “service: shell_command.clear_log” is already embedded in HA?

# Empty the log file
empty_log:
  alias: Clear log HA
  sequence: 
    - service: shell_command.clear_log

edit:
I executed the script… no effect, the log file still has text in it.

2018-12-24 01:06:58 WARNING (MainThread) [homeassistant.core] Unable to find service shell_command/clear_log

edit 2:
For those noobs struggling like me, the full instructions:

  1. create a folder “shell” (in the same dir as configuration.yaml)

  2. inside folder “shell” create a file “clear_log.yaml” and inside it put this line:
    truncate -s 0 home-assistant.log

  3. in “configuration.yaml” put this line
    shell_command: !include_dir_named shell/

  4. in “scripts.yaml” put this line (if you split the configuration.yaml) :

    empty_log:
      alias: Clear log HA
      sequence: 
        - service: shell_command.clear_log
    

The script calls the clear_log.yaml (actualy the command inside it) from the folder “shell” .

See the new entry in the Scripts section of the UI. Click EXECUTE and will empty the log file (it takes 2-3 seconds).

ps.
Now if I could find instructions to do the same for database db or to find out what the command shell would be :wink:

8 Likes

Edit: Stupid reaction, recorder has nothing to do with the log.

whatever I try, i doesn’t touch the database (or so it seems), which keeps growing…

No idea how the purging actually works. Might need a restart to purge the db.
The suggestion of mine has nothing to do with the log, so it’s not helpful at all.
No idea why I posted that :slight_smile:

Using the same logic in script, actually I managed to clear the database by deleting it using this command written in “clear_db.yaml” in the “shell” folder.
rm home-assistant_v2.db

It deletes the DB but for unknown reason HA recreates one back but no more than 4k in size and the log is pouring with errors because cannot write in the db any more.
A reboot fixes this and deleting the 4k db file.

I wonder id truncate command works on db file, since is not a text file like the log.

ps.
It is safe to delete db file while HA is On, it will recreate another fresh one.

great, thank you