You are not logged in.

#1 2013-06-26 16:46:32

orschiro
Member
Registered: 2009-06-04
Posts: 2,136
Website

[solved] sudo command in a Bash function?

Hello there,

I need to use sudo inside of a Bash function in order to allow my laptop to hibernate after a certain time using "systemctl hibernate".

However, I cannot figure out what's the correct way to include sudo inside of my function snippet:

function hibernate {
    echo "System is going to hibernate in $1 minute(s)..." 
    sleep $1m ; systemctl hibernate
}

alias hibernate_timer=sudo hibernate # sudo misplaced here

Last edited by orschiro (2013-06-26 20:33:47)

Offline

#2 2013-06-26 16:55:44

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: [solved] sudo command in a Bash function?

I think you just put "sudo" before the "systemctl" command, and setup sudo to not require a password for "systemctl".

Otherwise, you could just move the "hibernate" code into a Bash script and call "sudo hibernate". hmm

Offline

#3 2013-06-26 16:58:17

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: [solved] sudo command in a Bash function?

I would just put sudo in the systemctl line in the function.

If you want to use the alias method, put quotes around the command.

alias foo="sudo bar"

Are you sure a function can be aliased, though?


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#4 2013-06-26 17:22:59

orschiro
Member
Registered: 2009-06-04
Posts: 2,136
Website

Re: [solved] sudo command in a Bash function?

Yes, functions can be aliased.

However, alphaniner's suggestion does not work:

[orschiro@thinkpad ~]$ hibernate_timer 1
[sudo] password for orschiro: 
sudo: hibernate: command not found
function hibernate {
    echo "System is going to hibernate in $1 minute(s)..." 
    sleep $1m ; systemctl hibernate
}

Offline

#5 2013-06-26 17:48:26

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: [solved] sudo command in a Bash function?

you'd need sudo's -E parameter for that, otherwise that function is not defined in the su environment.

EDIT: -E is not sufficient.

man sudoers wrote:

In all cases, environment variables with a value beginning with () are
     removed as they could be interpreted as bash functions.  The list of environ‐
     ment variables that sudo allows or denies is contained in the output of “sudo
     -V” when run as root.

You cannot pass functions to sudo - the alias is not relevant, you couldn't do this directly: `sudo hibernate` would fail.

I'd also recommend putting the sudo as "deep" as possible: right on the systemctl command.  There is a reason bash functions are not allowed to be 'sudo'ed - and running sudo on a function that includes a sleep command seem particularly bad to me.

Last edited by Trilby (2013-06-26 17:59:18)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#6 2013-06-26 17:57:56

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: [solved] sudo command in a Bash function?

I really think putting sudo in the function is the best way to go. It's no big deal here, but if you run a whole function or script with sudo, every command therein runs as root. Using sudo 'discretely' means only the specific commands run as root.


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#7 2013-06-26 18:02:35

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: [solved] sudo command in a Bash function?

If you just want to be prompted for your password first, then have it sleep then hibernate (without needing the password at that time) then put the sudo in the function as suggested, but make the alias as `sudo -v && hibernate`

EDIT: or much easier, ditch the alias, but the `sudo -v` at the start of the funtion, then sudo before systemctl.

Last edited by Trilby (2013-06-26 18:03:31)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#8 2013-06-26 20:21:14

orschiro
Member
Registered: 2009-06-04
Posts: 2,136
Website

Re: [solved] sudo command in a Bash function?

Trilby wrote:

If you just want to be prompted for your password first, then have it sleep then hibernate (without needing the password at that time) then put the sudo in the function as suggested, but make the alias as `sudo -v && hibernate`

EDIT: or much easier, ditch the alias, but the `sudo -v` at the start of the funtion, then sudo before systemctl.

Yes, that is exactly what I want. Being asked for the password first, then sleep, then the hibernate command.

I am afraid I do not completely understand your suggestion. Do you mean something like the following?

function hibernate {
    sudo -v
    echo "System is going to hibernate in $1 minute(s)..." 
    sleep $1m ; sudo systemctl hibernate
}

alias hibernate_timer=hibernate

Offline

#9 2013-06-26 20:27:05

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: [solved] sudo command in a Bash function?

Yes, that will work as long as the sleep time is not greater than the sudo timeout.  I don't know off hand where the timeout is set (probably sudoers), but I think the default is 5 minutes.

If you need it longer than you would want to make the sudo timeout, then you'll need to use a separate script - or put systemctl hibernate in the sudoers as not requiring a password.

Last edited by Trilby (2013-06-26 20:27:45)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#10 2013-06-26 20:33:23

orschiro
Member
Registered: 2009-06-04
Posts: 2,136
Website

Re: [solved] sudo command in a Bash function?

Well, if that is the case then I will use a separate script since I may want to wait for longer than 5 minutes.

Thanks anyway!

Offline

Board footer

Powered by FluxBB