0

I have modified the sudoers file with sudo visudo and added 2 lines to it:

tiamo ALL = NOPASSWD: /home/tiamo/myscript.sh
tiamo ALL = NOPASSWD: /usr/bin/command

I have then added my script to "Startup Applications" like so: bash -c "sleep 1m;gksu /home/tiamo/myscript.sh"

myscript.sh looks like this:

do some non sudo stuff
do sudo command
while true
do
launch a program
sleep 15m
kill the program
done

After I reboot Ubuntu I open a terminal and use pgrep myscript, it reveals that it is running and its pid. However, the program it is supposed to launch is not active, so I assume that it gets stuck on the sudo command which I have added to my sudoers file.

Any clue why it gets stuck?

SOLVED like so:

Added the sudo commands to my /etc/rc.local. Launch my script in startup applications like gnome-terminal -e ./myscript.sh. The output of the script is now redirected to a new terminal, and it executes successfully.

  • I suggest logging output to some file to debug it. Replace in that command: gksu /home/tiamo/myscript.sh with: gksu /home/tiamo/myscript.sh > /home/tiamo/myscript.output 2>&1". Bonus: can you gksu /home/tiamo/myscript.sh without inserting password? – Ctrl-C May 8 '18 at 17:32
  • Trying to launch my script with gksu indeed prompts me for my password, that was not intentional and I had not tested that yet. I am trying to launch a graphical application with sudo rights on startup automatically, with no additional input required of me, so this is something I want to avoid. – iTiamo May 8 '18 at 18:49
  • I wonder if replacing gksu with sudo will fix the problem. – Ctrl-C May 8 '18 at 18:50
0

The sudoers file looks okay.

The script is probably okay as long as none of the commands are blocking it. However, if any of the commands are also scripts, you will probably want to use . script.sh.

I'm not so sure about whether the bash command is necessary. Normally, I would think it is enough for the script to be executable with a proper #!.

There is also a potential issue with running graphical programs as root. You may need to create a file ~/.xinitrc with the following contents:

#! /usr/bin/env bash
xhost +local:

This page has some info you will probably find helpful:

  • Oops! I launch the program in the background like: program [some options] & I am trying to launch a graphical application with sudo rights on startup automatically, with no additional input required of me. The program should always be running, and the program never exits automatically. – iTiamo May 8 '18 at 18:47

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.