# Granting Permission to Hibernate [SOLVED]

## pandaxiongmao

I want to use 

```
echo disk > /sys/power/state
```

 in order to put my PC to hibernate mode, but how do I do it as non-root? I tried to modify /etc/sudoers, but it didn't work.

```
%wheel          ALL=NOPASSWD:   /sys/power/state

%wheel          ALL=NOPASSWD:   echo disk > /sys/power/state

%wheel          ALL=NOPASSWD:   /usr/bin/echo disk > /sys/power/state
```

All 3 of them refused to grant permission to non-root.

BTW, I tried suspend2, and it failed miserably, so I'm not going to redo it again.Last edited by pandaxiongmao on Mon Jun 04, 2007 12:55 pm; edited 1 time in total

----------

## Hu

Can you paste the failed command and its output?  I suspect you were attempting to sudo an echo, with the output of the sudo redirected by the non-root shell to /sys/power/state.  That would obviously fail, as the entire point of the sudo is that the non-root shell lacks permission to do that redirection.

As a cheesy hack, save the below code block as /usr/local/bin/suspend-to-disk.sh and tell sudo to let anyone run it.

```

#!/bin/sh

echo disk > /sys/power/state

```

This should work since it moves the redirection into the spawned process, making it unambiguous when you issue your command that you are sudo'ing a command with nothing special, and unknown to sudo or your shell, that command happens to perform some redirected output.

----------

## pandaxiongmao

It failed because of the lack of permission, but thanks for letting me know that I can turn the command into bash script.

Next, how do I tell sudo to allow me to execute the script?

I added

```
%wheel          ALL=NOPASSWD:   /usr/local/bin/hibernate.sh
```

into /etc/sudoers, is that the correct way? 

Next, I ran

```
sudo . /usr/local/bin/sh
```

But the sudo couldn't find the period "." command, so I tried to change the ownership of hibernate.sh to my regular account, but I got denied again whenever I tried to run it. 

Does this below command exist? 

```
sudo .
```

----------

## Hu

sudo /usr/local/bin/hibernate.sh should work to run it, once you configure sudo to allow it.  I do not use sudo, so I cannot advise whether you have configured it right.

----------

## pandaxiongmao

It didn't work, sudo still complained that it couldn't find /usr/local/bin/hibernate.sh.

Thanks for the reply, btw.

----------

## Hu

What did you name the file that holds the shell script I posted?  I thought from your /etc/sudoers file that it was /usr/local/bin/hibernate.sh.

----------

## pandaxiongmao

It is.

Sudo complains

```
sudo: /usr/local/bin/hibernate.sh: command not found
```

I tried several different ways to execute the command (look at the example I provided underneath,) but they didn't work.

```
sudo sudo /usr/local/bin/hibernate.sh

sudo . /usr/local/bin/hibernate.sh
```

----------

## Monkeh

You.. don't want to try and source it. Is the file executable?

----------

## pandaxiongmao

Yes, the script works flawlessly if I execute it as root. In fact, I just attempted it before I posted this message.

----------

## pandaxiongmao

Okay, I found the solution.

1. Create a file called hibernate at /usr/bin

```
su -

cd /usr/bin

vim hibernate
```

2. Fill in the file with these lines

```
#!/bin/bash

echo mem > /sys/power/state
```

3. Make it executable by typing

```
chmod 755 hibernate
```

4. Add the new file to the wheel group or any other group by using

```
chgrp wheel hibernate
```

5. Finally, add this line to /etc/sudoers

```
%wheel          ALL=(ALL)       NOPASSWD: /usr/bin/hibernate
```

6. In order to run the file, you must type

```
sudo hibernate
```

7. Voila

----------

