5

Quick question, is it possible to run as non-root for applications, automatically started when the pi is booted?

I'm having performance and memory issues when my pi starts a homebrewn spotify-client on boot.

Performance - I cannot explain why this is different than when running it from a terminal once logged in, but the music lags a second once every 15-30 seconds.

Memory - My offline tracks doesn't get saved from session to session.

This leads me to the question, can I 'fake' a user logging in and starting the app with user-previlegies?

Right now i'm using /etc/rc.local to run the command /home/pi/spotifyapp & Everything runs smooth and offline tracks are saved when i start the app by myself after the pi has booted

5 Answers 5

15

I am skeptical whether running this application under a different user will resolve your issue.

However that being said, you can use the su command to run a command as another user. Therefore to run /home/pi/spotifyapp you should use the following in your rc.local file:

su fakeuser -c '/home/pi/spotifyapp &'

You may want to redirect stdout and stderr to a file to allow you to see any messages generated. The following command will do that:

su fakeuser -c '/home/pi/spotifyapp >> /tmp/spotifyapp.log 2>&1 &'

Where fakeuser is the username that you want to run the process under.

1
  • 1
    Changing the current directory, i.e. cd /home/pi/app/ and then running it as a another user seems to solve the problem. Didn't know that trick, thanks a lot :-) Commented Jun 27, 2013 at 8:03
2

Usually, scripts in /etc/rc.d are using start-stop-daemon to launch, stop and control status of boot-time processus.

Combined with the optional -c, --chuid argument, you can specify under which user you want the process to run.

2

I use

sudo -u yourusername command -parameter
2

Much like OP (although 7 years later :)) I also wanted to start a service as a nonroot user at startup:

In my case I needed method run a script as the raspotify user at startup so I could automate control of alsamixer and alsaequal from the hardware I had built. However the other answers didn't work for me (in particular the rc.local failed to launch the script and left nothing in the log). (I have added the detail given due to similarity with OP and that this post was the closest search hit while I was working the problem)

Ultimately, I followed the tutorial here and made a systemd service that looked like this:

[Unit]
Description=raspotify_alsaequal_wrapper service.

[Service]
Type=simple
User=raspotify
Group=raspotify
ExecStart=/usr/bin/python /home/pi/alsa_wrap_script

[Install]
WantedBy=multi-user.target

I saved the file at /lib/systemd/system/alsawrap.service and /etc/systemd/system/alsawrap.service

set permissions to 644 and enabled it to start with sudo systemctl enable alsawrap

1
  • The "User" and "Group" options are what I was looking for, needed WorkingDirectory, too. Commented Sep 18, 2021 at 15:19
1

You could place your script at the bottom of /etc/profile file.

2
  • I don't think this would have solved my problem, since I had performance issues. Your answer is about starting a process once logged in. Also, you're 3 years late to the party :) Commented Nov 28, 2016 at 8:00
  • 3
    I've browsed around and just throwing my thoughts into it. Maybe helps someone in the future.
    – HelpNeeder
    Commented Nov 29, 2016 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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