Search code examples
bashshellpermissionsstdoutsudo

How to execute and log as specific user?


I need to run a command/shell-script as on other user. The stdout shall be written to a logfile.

I tried it like this:

export LOGDIR=foo/bar
sudo -u www command /home/www > /home/www/$LOGDIR/command.log

But I get always this error:

-bash: /home/www/foo/bar/command.log: Permission denied

You can try this easily with this little stupid example:

sudo -u edeviser ls /home/edeviser > /home/edeviser/$LOGDIR/ls.log

I see the problem is, that the redirection with > is not done as the user specified by the ´-u´ option.

How to execute the command and log as the same specific user?


Solution

  • You can try something like

    sudo -u www bash -c "command /home/www > /home/www/$LOGDIR/command.log"