Bash – sudo command after long sleep call without re-prompting sudo password

bashbashrcsudo

I'm trying to write a simple bash function that sleeps X-number of mins and then runs a system sleep command (for me it's sudo pm-suspend since I'm on Ubuntu) right now the function looks like this:

function sleepin {
  sleep $(bc <<< $1*60); sudo pm-suspend
}

The first argument is after how many mins the sleep is supposed to happen. The problem is that after the sleep the script prompts me for the sudo password.

How should I re-write the script so that it doesn't prompt me for the sudo password after the sleep?

(I've tried to call sudo sleepin 30 but it still prompts me for the password. Note that if the sleep call is small enough bash doesn't prompts for password, but for a longer sleep it will..)

Best Answer

As Mat suggested, you can allow the user to run pm-suspend without a password.

Run sudo visudo

Add youruser ALL=(root) NOPASSWD: /path/to/pm-suspend

Related Question