convert upstart to systemd – run command as root, spawns command off as another user

Dubl1n asked:

I need help with converting an upstart .conf into a systemd .service. Essentially what my .conf file does is this:
exec su -l foo -c 'cd /var/www/path/to/file ; bundle exec puma -C config/puma.rb'

As far as I can tell, we are running the command as root, and then creating a login shell using our “user”:

root 1 0.0 0.0 5 7 ? Ss Date 0:00 su -l foo -c cd /var/www/path/to/file ; bundle exec puma -C config/puma.rb
foo 2 0.0 0.0 6 8 ? Ss Date 0:00 -su -c cd /var/www/path/to/file ; bundle exec puma -C config/puma.rb

This is what I have so far with my systemd .service file:

[Unit]
Description=simple boot on start
After=network.target

[Service]
Type=simple
User=foo
WorkingDirectory=/var/www/path/to/file
ExecStart=/usr/bin/sudo /bin/bash -lc 'bundle exec puma -C /var/www/path/to/file/config/puma.rb'
Restart=always

[Install]
WantedBy=multi-user.target

I know this is wrong, but it works…sort of. In the sense that I get the process to run, but as root. Not running the command as root to run the process as the “user”:

root 3 0.0 0.0 4 5 ? Ss Date 0:00 /usr/bin/sudo /bin/bash -lc bundle exec puma -C /var/www/path/to/file/config/puma.rb

This is pretty new to me at a new job, so trying to find may around this system. Any help is appreciated.

Thanks!
D

My answer:


You’re not meant to just copy things literally into ExecStart=, especially those which are just for setup, like changing the working directory or user. Those have different directives (which you already used!) so you should just start your program.

For Puma in particular, you should just start with the sample systemd unit, and customize it to add the user, working directory, path to puma, and (if applicable) your TLS certificate and key.


View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.