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
2

2 Answers 2

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 /).

2
  • 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. Commented Feb 24, 2012 at 13:22
  • How to provide the password for another user while changing the user in script? Commented Feb 28, 2018 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'

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.