2

I'm running a python script on my rPi. I have to run it in sudo to use the rpi.gpio library, but when I do, it won't let me run my bash script in non-sudo. This causes my ssh commands to prompt for passwords, even though the key-gen authentication is set.

I'm sure I'm missing something simple. Can anyone help?

Running python script:

sudo python ./runcam.py

In the py script I'm running this:

subprocess.call("./runit", shell=True)

And my ssh script:

#!/bin/bash

      FNAME=`date +'%H-%M-%S-%m%d%Y'`

      ssh [email protected] '
        mkdir $FNAME
      ' & 

      ssh [email protected] '
        mkdir $FNAME
      ' & 
1
  • Could you change your command to subprocess.call("sudo ./runit", shell=True)? Commented Dec 6, 2013 at 2:19

1 Answer 1

1

You can also drop your root permissions with sudo. You just need to check for user id 0, and the environment variable "SUDO_USER", then sudo -u "$SUDO_USER" command.

You can do it in your bash script like this:

# if we're root from sudo, run this script as the original user instead
[[ "$(id -u)" = "0" && -n "${SUDO_USER:-}" ]] && exec sudo -u "$SUDO_USER" "$0" "$@"

Full script example:

#!/bin/bash -u
[[ "$(id -u)" = "0" && -n "${SUDO_USER:-}" ]] && exec sudo -u "$SUDO_USER" "$0" "$@"
id -u

This script will print your user id, regardless of whether it's run with sudo.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.