2

I am trying to get a simple bash script to run when I click on it. It runs perfectly from the cli but starting motion with sudo fails to start. I have that script setup with sudoers already for nopasswd, it starts up correctly and does not ask for password when run from cli. It also closes everything correctly when ran from cli, but it is almost like the sudo lines get skipped when I just double click on the file and tell it to run.

Here is my script:

#!/bin/bash

check_process() {
  [ "$1" = "" ]  && return 0
  [ `pgrep -n $1` ] && return 1 || return 0
}

check_process "motion"
if [ $? -eq 0 ]
then
  sudo /etc/init.d/motion start
  firefox http://localhost:8081/ > /dev/null &
else
  sudo /etc/init.d/motion stop
  killall firefox > /dev/null &
fi
  • 1
    try replacing 'sudo' with the 'gksu'. – Sudheer Aug 2 '14 at 4:58
2

In Nautilus you have two options to run a script when you double click on it:

Run a script from Nautilus

1. Run in Terminal

  • Using this option, your script will run correctly (as you said).

2. Run

  • Using this option you have to use pkexec (or gksu if you have it installed) instead of sudo. That is because you can't run graphical commands which use sudo without to use an X terminal emulator, even if you set sudo to not ask for password.

  • So, in this case, your script should look like:

    #!/bin/bash
    
    check_process() {
      [ "$1" = "" ]  && return 0
      [ `pgrep -n $1` ] && return 1 || return 0
    }
    
    check_process "motion"
    if [ $? -eq 0 ]
    then
      pkexec /etc/init.d/motion start
      firefox http://localhost:8081/ > /dev/null &
    else
      pkexec /etc/init.d/motion stop
      killall firefox > /dev/null &
    fi
    
  • switching to pkexec did work but still have to put in password each time. Not horrible but I was hoping to make a launcher that I just clicked to turn motion on and off. Is there a way to make it so I don't have to put in my password each time? – user311472 Aug 2 '14 at 14:44
  • @user311472 You don't need to put in password each time if you follow the following answer: How to configure pkexec to not ask for password? :) – Radu Rădeanu Aug 2 '14 at 16:55

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.