I've created a script in /etc/init/mms-agent.conf :

start on runlevel [2345]
stop on runlevel [06]

exec /usr/bin/env python /home/mms/mms-agent/agent.py >> /home/mms/agent.log 2>&1

service mms-agent start/stop work fine, but i'd like to start this service with the user "mms"

When I try to change the script as following :

exec su mms -c "/usr/bin/env python /home/mms/mms-agent/agent.py >> /home/mms/agent.log 2>&1"

I get 3 process running instead of 1 expected (su, bash + my python script):

mms       8864  0.0  0.2  37816  1332 ?        Ss   22:30   0:00 su mms -c /usr/bin/env python /home/mms/mms-agent/agent.py >> /home/mms/agent.log 2>&1
mms       8865  0.0  0.2  11452  1196 ?        S    22:30   0:00 bash -c /usr/bin/env python /home/mms/mms-agent/agent.py >> /home/mms/agent.log 2>&1
mms       8866  4.0  1.8  54672 10640 ?        Sl   22:30   0:00 python /home/mms/mms-agent/agent.py

What does that mean ?

What is the best way to start a script with a user different than root ?


PS :

exec start-stop-daemon --start -u mms --exec "/usr/bin/env python /home/mms/mms-agent/agent.py"

doesn't work, I got no error but the process isn't started.


When you exec su, the shell is replaced with the su process. su then forks and execs bash. bash sets up the output redirection and then forks and execs env. env searches PATH, finds python and execs it. So you're left with three processes, 1) su, waiting for bash to exit, 2) bash, waiting for python (previously env) to exit, and 3) python, which is busily running your script.

There is nothing wrong with this. su is the Unix time-honored way to temporarily switch users, so that's what you should be using. Similarly, the way to run a command line is to use a shell, so su runs bash and lets bash handle it. Again, this follows the general Unix toolbox approach, letting the shell do what the shell is good at, and not reinventing shell command line and PATH search functionality in another application.

Also, since bash is already doing the PATH search, you can drop the use of env and invoke python directly.


See Run a Job as a Different User in the Upstart manual. Using start-stop-daemon is the recommended method.

You just made a small mistake: you passed the whole command line where start-stop-daemon expects the path to the executable. Pass the arguments separately.

Another problem with your invocation is that you're telling start-stop-daemon to consider any Python process to be an instance of your service. That might result in it killing other unrelated Python processes. To ensure only one instance of your service runs, use a pidfile.

exec start-stop-daemon --start -c mms -u mms -m -p /var/run/mms-agent.pid \
                       --startas /usr/bin/env -- python /home/mms/mms-agent/agent.py
  • humm can't get it launch, even with exec start-stop-daemon --start --pidfile /var/run/mms-agent.pid -m -c mms --exec python /home/mms/mms-agent/agent.py -- run > /home/mms/agent.log 2>&1 – Quentin Feb 28 '12 at 1:09
  • Your path to the executable files need to come after the --. Also, you should use the --exec flag i.e. start-stop-daemon --start -u mms -m -p /var/run/mms-agent.pid --exec python -- /home/mms/mms-agent/agent.py – mills013 Feb 28 '12 at 5:12
  • @Quentin Does anything appear in a log file in /var/log? If the launch fail, you should have a log message. – Gilles 'SO- stop being evil' Feb 28 '12 at 9:16
  • right, the path to python was missing, I added /usr/bin/env (or /usr/bin/python2.7), but now the following lauch the script as root, no with my mms user... Any idea ? exec start-stop-daemon --start -u mms -m -p /var/run/mms-agent.pid --exec /usr/bin/env python -- /home/mms/mms-agent/agent.py – Quentin Feb 28 '12 at 11:42
  • ok, adding -c mms make it start with the mms user. Not sure to understand the difference between -c and -u. – Quentin Feb 28 '12 at 11:55

You get three processes because you're running su, which starts a shell to run the python process.

There are several ways to convince the "extra" two processes that they can exit, for example you can have the python script fork and exec, or you can have the command you execute with su background itself.

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.