2

I am trying to change user in order to execute a specific command during startup. It fails silently and apparently the userchange isn't carried out as I can tell that the command isn't executed.

What am I doing wrong in my below shown initscript?

respawn
console none

start on startup
stop on shutdown

script
  su -u anotheruser -c "myCommand" >> "myLogfile.log"
end script
5

I'm assuming you are running recent version of ubuntu or a distribution based on upstart. You can check /var/log/daemon.log for errors.

The standard su takes the syntax su [options] [username]. Checkout man 1 su. You might want to try :

su -c "myCommand" anotheruser >> "myLogfile.log"

Also, a couple of things would happen (mostly not desirable)

  1. myLogfile.log would be owned by root.
  2. myLogfile.log would be created on / (root directory) if you don't use an absolute path like /tmp/myLogfile.log (because upstart runs with pwd set to /).

If you want the file to be owned by anotheruser you might switch the command to.

su -c "myCommand >> /tmp/myLogfile.log" anotheruser

This might cause problems if you have leftover myLogfile.log owned by root from earlier runs or if have not changed myLogfile.log to something like /tmp/myLogfile.log (normally, regular users can't create files on root dir /).

  • The ownership or permissions of myLogfile.log might be the issue. Be aware that root might have a stricter umask, and the permissions of myLogfile.log would then disallow writes. – Bruce Ediger Feb 24 '12 at 13:22
  • How to provide the password for another user while changing the user in script? – user3548033 Feb 28 '18 at 13:41
2

It looks like you've mixed up the syntax of su and sudo.

su -c 'mycommand' anotheruser
sudo -u anotheruser 'mycommand'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.