# Sleep & Wake Up



## fr33bsd (Aug 15, 2021)

Hi,

I am wondering, if it is possible to make a freebsd server fall asleep like when you close the display of a laptop making the machine switching into stand-by.

On the other hand, is it possible to resume a freebsd server using WoL (wake on lan)?

Does anyone have experience?


----------



## George (Aug 15, 2021)

Add this to /etc/devd.conf:


```
notify 10 {
match "system" "ACPI";
match "subsystem" "Lid";
action "/etc/myscript $notify";
};
```

According to devd.conf(), $notify=0x00 means closed, $notify=0x01 is open.

Don't forget to kill and restart devd, so that it reads the config file anew.

In your script, you can make your server fall asleep (zzz).

 Not sure how wake on lan is implemented.


----------



## fr33bsd (Aug 15, 2021)

Hey George,

thank you, but maybe I was comparing the stand-by state too visual: I meant: "What is the command to make the server to switch to stand-by?"
The answer would actually be the content of /etc/myscript. Sorry, my bad. 

All my servers are headless. So there is no lid to close.  
I mean it would be funny when you trigger your data center to shut down or switch into stand-by mode when you close the lid of the toilet in the office. Or: If you implement your boss' toilet lid to shut down the data center. Of course, opening the lid should send a WOL-ping to all systems ...

I was kiddin' - ok, no jokes, no off-topic jokes. 

Being more precisely:
The event that triggers the server to go in stand-by is supposed to be 

a certain number of specific processes (in this case: list of zero jails)
and 100% idle like 1 minute or so.
In other words: if no jail is running and the system is idling more than 60 seconds then switch to stand-by.
How to sense such event and what is the stand-by command?


----------



## fr33bsd (Aug 15, 2021)

Regarding the certain number of specific processes: It will be a `grep` on `ps` piped into `wc` (word count) comparing`eq 0` by using `if`.

What about the idle couter?

And how to run that check? 1-minute-cron-job?


----------



## fr33bsd (Aug 15, 2021)

Command: dead end #1 and dead end #2 . Both out dated.


----------



## fr33bsd (Aug 15, 2021)

Let's try `sysctl debug.acpi.suspend_bounce=1` followed by `acpiconf -s 3` (taken from here). 

But it seems to be broken in earlier versions of FreeBSD ...


----------



## George (Aug 15, 2021)

Ok, run a cron job.
I use zzz().


----------



## fr33bsd (Aug 16, 2021)

George said:


> Ok, run a cron job.
> I use zzz().


I apologize. I thought *you were making jokes *by saying:


> In your script, you can make your server fall asleep (zzz).


I didn't know zzz is an app.   I thought about this picture ""

How ever, George, thank you again.
Regarding executing `zzz` command there are still missing both mentioned conditions before running `zzz` as well as the sensing for that condition.

I mean I could do something like this:


```
#!what-ever-shell
// pseudo coded draft
idletime=0
while true do
  check if idle=100%-99% do idletime++ else idletime=0
  check if jails=0 and idletime>=60 do
    zzz // server go to bed
  done
  delay 1 second
done
```

But this is 

Rather run this draft by cron(?) every second :


```
#!what-ever-shell
// pseudo coded draft
check if idle=100%-99% do set env-idletime++ else set env-idletime=0
check if jails=0 and env-idletime>=60 do
    zzz // server go to bed
done
```


----------



## fr33bsd (Aug 18, 2021)

Is there any mechanism that senses for specific number of some specific process?

Or can I just tell devd to run my script after 60 seconds of being idle? Then the script could count the number auf jails. If the number auf jails is zero then run zzz.


----------



## Hakaba (Aug 18, 2021)

If you want to test each second, then the simplest way is to create two Cron job IMHO.

One put the last date if no idle in /tmp/lastNoIdleTime each second.
The second that run `zzz` if the file does not exist or if `now - date > 60`.
But there is maybe a simpler way.


----------



## fr33bsd (Aug 18, 2021)

Hakaba said:


> If you want to test each second, then the simplest way is to create two Cron job IMHO.
> 
> One put the last date if no idle in /tmp/lastNoIdleTime each second.
> The second that run `zzz` if the file does not exist or if `now - date > 60`.
> But there is maybe a simpler way.


What about exporting the value to environment variable?


----------



## Hakaba (Aug 19, 2021)

fr33bsd said:


> What about exporting the value to environment variable?


I am not sure that two cron jobs shares env vars. To be tested ?


----------



## fr33bsd (Aug 19, 2021)

Hakaba said:


> I am not sure that two cron jobs shares env vars. To be tested ?


Cron jobs are being executed as same user (root), so they should be able to read globally exported env vars of root, right? I didn't try it before, too. It might be a security issue if it is possible ...   On the other hand, there should be an initial export of some env var to set the value like on login by bash due to .bashrc, I think. So, if you just boot up a system there should be no env var available until root logs in, except you set export command in crontab, if there is no environmental isolation. Logical, but sounds like an stupid idea ... Let's give it a try ... 

but ...

on the other hand, what if you go

```
. script1.sh
. script2.sh
```
in crontab?


----------



## Hakaba (Aug 19, 2021)

The `uptime` command has the last minute load average. This is maybe sufficient and more elegant to know if the system is on charge or not ?


----------



## fr33bsd (Aug 19, 2021)

Ok, so the idle issue might be cleared by uptime with a load threshold *regarding sensing the load for last 60 seconds*. But how to combine uptime with devd? There is no check on load in devd ... 

time2sleep.sh

```
// pseudo code

on cpu.event "idle" do // event by devd?
  if 0 <= uptime.load <= 0.3 and numberOf.jails == 0 do
    zzz // good night
  done
done
```

Maybe it is possible to use powerd to execute the command, even though the purpose of powerd is different ... 

powerd():


> *-v*      Verbose mode.    Messages about power changes will be printed to stdout and powerd will operate in the foreground.



check.sh

```
// pseudo code

if (grep idle stdin) do
  if 0 <= uptime.load <= 0.3 and numberOf.jails == 0 do
    zzz // good night
  done
done
```

But this sounds like a stupid idea ... even if you somehow pipe powerd's output thru the check
`powerd -v | ./check.sh`

Or rather sense on logs, if powerd logs anything !?


----------



## ccammack (Aug 19, 2021)

You should probably test `zzz` and WOL by hand on your server to make sure they work before scripting anything.

After that, write the simplest possible script that might work, which will be something like the first script you wrote with the `sleep` in it. You will likely have to poll to get the jail count and CPU usage, and if you're polling anyway, there's nothing wrong with using `sleep`.

After you get the script working, start it using `cron` at boot time or run it as a service. Let it run for a while and then make improvements if needed.


----------



## ccammack (Aug 19, 2021)

fr33bsd said:


> What about the idle couter?



Maybe `top`, `vmstat` or `iostat`?

```
$ top -Sb | sed -Enr 's/^CPU: .+ (.+)% idle/\1/p'
70.5
$ vmstat | awk 'NR==3 {print $(NF)}'
71
$ iostat | awk 'NR==3 {print $(NF)}'
71
```


----------



## fr33bsd (Aug 20, 2021)

Neubert said:


> Maybe `top`, `vmstat` or `iostat`?
> 
> ```
> $ top -Sb | sed -Enr 's/^CPU: .+ (.+)% idle/\1/p'
> ...



Excellent idea, Neubert . This is a great approach to determine specific metrics to decide when to suspend the system.

Regarding strategy of development of the whole thing: I totally agree with you.


----------



## fr33bsd (Aug 20, 2021)

I will try this, soon.



> make sure WoL functionality is enabled in a machine’s / servers BIOS so that it can be used to power on machines from a remote system without having physical access to them. The syntax is:
> 
> wake mac-address-here
> wake link-layer-address-here
> ...


wake is being executed on linux ...   does FreeBSD have wake?
YES, it DOES.


----------

