Managing /media with shell scripts

I download files to Home Assistant’s /media directory and have some shell scripts that organise and delete files.

I’m currently running the supervised installation of Home Assistant within Ubuntu on an Intel NUC. Home Assistant has a shell command that calls a script on the host (the NUC) via SSH:

  tidy_media: ssh my-intel-nuc.local -F /config/.ssh/config 'bash ~/scripts/'

The shell script then accesses the exposed /media folder from within Home Assistant to perform the commands:


cd "/usr/share/hassio/media"
echo "I am in the /media directory"

# more stuff, tidying media etc.
# rm downloads/file_name.txt
# mv downloads/file_name.txt /files/file_name.txt

This works, but it’s a bit crazy to be SSHing out to the host to run a script that just calls back in to Home Assistant. I’d like to switch to a Home Assistant OS setup and remove Ubuntu from the equation, so I need to run these scripts entirely within Home Assistant.

To do this, I’ve moved the script in to Home Assistant’s config and as-per the docs setup my shell command like this:

  tidy_media: bash /config/shell/

If I run bash /config/shell/ from the terminal in the VSCode addon (which looks to run as root) the script executes correctly.

However if I call the command from the Home Assistant UI I get this error in my logs:

Error running command: `bash /config/shell/`, return code: 1

return code: 1 looks to be an authorisation error. So when the script is called from Home Assistant it’s not running as root but some other user, that doesn’t have permission to modify files within the /media directory.

Does anyone have any ideas on a fix for this? I want to run a script from within Home Assistant’s config directory that modifies files in the nearby official Home Assistant /media directory.

I’ve tried changing permissions from the terminal in the VSCode addon but it seems to make no difference. Besides such permissions are likely to be reset, and shouldn’t be necessary given what I’m trying to do?

Interestingly if I run echo $(whoami) > whoami.txt in this script, root is saved regardless of whether it was run via the terminal in the VSCode addon or via the Home Assistant UI. So when a script command runs it runs as the root user it seems, but with a limited subset of commands available.

This “root but not root” user is not able to execute commands I need such as chmod, rm, sudo etc. and has no write permissions in /media even after I separately chmod 777 -R /media.

I thought this might be a case of “add the homeassistant user to sudoers” but it seems the homeassistant user doesn’t exist and there’s no /etc/sudoers file anyway.

Pretty much at a loss here on how to run a shell script within Home Assistant against the /media directory.

Maybe the script’s PATH is incorrect / incomplete. Try specifying the whole path to the program you are trying to execute. You can find the correct path to a program in an interactive shell like this:

$ which ls

(Although I don’t think this would result in return code 1.)

It would help if we could see the exact error messages you are getting. Try redirecting the script’s output (stdout and stderr) to a file.

1 Like

@ondras12345 thank you for your reply! I stupidly assumed return code: 1 was all I could get, I hadn’t tried outputting the error to a file.

Being able to see the full error using command &> error.txt revealed some issues with my script, for example find doesn’t seem to have the -execdir option when the script is called from the Home Assistant UI, but it does have -exec which avoided these errors and even the rm command seems to work.

Another solution I was considering before your reply was passing the script to the “SSH & Web Terminal” add-on which runs with more permissions, like this. Thankfully this hasn’t been necessary though.

In the end, very simple working setup:


  tidy_media: bash shell/



rm /media/useless_file.txt

Thanks again!

1 Like