Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question
    
You could also use cron, i.e. a crontab @reboot entry, I suppose. (Output will be sent via mail.) –  sr_ Feb 24 '12 at 10:05
    

2 Answers 2

up vote 3 down vote accepted

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

share|improve this answer
    
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

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

su -c 'mycommand' anotheruser
sudo -u anotheruser 'mycommand'
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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