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
Radu Rădeanu
  • 169,590

1 Answers1

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
    
Radu Rădeanu
  • 169,590