1

I have a python script that reboots the device at the end of the script. I got it working fine on my user account by giving the user rights to /sbin/shutdown in sudoers file. But the problem is that when I try to automate it with the user's crontab, I get the following error:

sudo: no tty present and no askpass program specified

Any ideas how to solve the problem? I thought the user's crontab would have same rights as the user, but it doesn't seem that way.

I can't use root crontab because part of the python script uses Selenium with geckodriver and geckodriver cannot be ran as root.

Here's the reboot part of the python script:

def restart():
    command = "/usr/bin/sudo /sbin/shutdown -r now"
    import subprocess
    process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
    output = process.communicate()[0]
    print output

I'm using Ubuntu 18.10

Milkymilk
  • 21
  • 3
  • An alternative approach might be to have a `root` cron job which examines a file every minute, and then just create that file when you need a reboot. (You need write access to the location, of course.) – tripleee Feb 21 '19 at 08:15
  • Is your `sudo` set up with `NOPASSWD`? – tripleee Feb 21 '19 at 08:15
  • As an aside, don't use `Popen()` for trivial things that `check_call()` or `run()` can handle. See further https://stackoverflow.com/a/51950538/874188 – tripleee Feb 21 '19 at 08:17
  • @tripleee I was also thinking of splitting the reboot part as it's own script, if it's now possible to give cronjob specific rights. I have the following lines in the sudoers file: `user ALL=/sbin/shutdown` `user ALL=NOPASSWD: /sbin/shutdown` – Milkymilk Feb 21 '19 at 08:25
  • Those are conflicting, you just want the latter one (though unsure what the effect might be). – tripleee Feb 21 '19 at 08:26
  • Thanks for the Popen() tip. Actually my original script has just `os.system('systemctl reboot -i')`, but I tried to change to the subprocess one if it worked better (unfortunately not) – Milkymilk Feb 21 '19 at 08:27

1 Answers1

1

I managed to solve the issue. If someone is having similar problems, try editing the sudoers file so that your user ALL=NOPASSWD: /sbin/shutdown is the last line in the file so it takes priority over conflicting lines.

Milkymilk
  • 21
  • 3