Shell Command and LogFiles

I have different bash scripts. I have created Shell_commands in configuration.yaml but I would like to create a logfile with the output of that shell_command… So in configuration yaml I have the following lines:

   optimize_database: /bin/bash /home/homeassistant/.homeassistant/optimize_database.sh > /home/homeassistant/.homeassistant/optimize_database.log
   myfox: /bin/bash /home/homeassistant/.homeassistant/myfox.sh "{{ arguments }}" > /home/homeassistant/.homeassistant/myfox.log

My problem: the first shell_command is running correctly and create an output file called optimize_database.log (I have three shell_commands with the same format and they are running correctly with logfiles created), the second one above is running correctly as well but the logfile is not created (I have 6 of those with the same format and the same behaviour)… So it seems that when there is an argument what is behind is not considered… or my approach is not correct for what I aim to do and should be changed… Any recommendations ?

Check the docs

The commands can be dynamic, using templates to insert values for arguments. When using templates, shell_command runs in a more secure environment which doesn’t allow any shell helpers like automatically expanding the home dir ~ or using pipe symbols to run multiple commands. Similarly, only content after the first space can be generated by a template. This means the command name itself cannot be generated by a template, but it must be literally provided.

Sure enough, looking at the code:

 # Template used. Break into list and use create_subprocess_exec
 # (which uses shell=False) for security

So you can’t do the ‘>’ character with templates. If you want, create a wrapper script (or modify myfox.sh if that’s your own script) to do the logging inside there.

That’s a work around I thought about… Create an “ini” script with just one line where I can launch the script with the redirection to the logfile… Thanks !

1 Like