Permission denied using sudo

aliaspermissionssudo

I have an alias in my .bashrc file that runs an executable python file like so:

alias my-command="sudo -u apache /path/to/file.py"

When I run my-command It prompts for my sudo password and then it runs the .py file.

However the python script writes to a Log file that only apache has permission to write to. When I run my-command it says Permission denied when it tries to write to that file.

When I run sudo -u apache /path/to/file.py directly the script runs fine.

What am I doing wrong here? Would it be better to change my alias to:

my-command="/path/to/file.py"

and then run:

sudo -u apache my-command

Best Answer

The issue had to do with relative paths. I was running the alias my-command from my home directory. My python script was writing to a file relative to the working directory. When I ran the command directly (without the alias) I was running it from the location of the python script, so that was the working directory, so the script had permission to write to the file.

So, when I run the alias command from the proper directory everything worked as expected.