# Execute command after ACPI resume



## Acacio (Aug 24, 2015)

Hello, I'm trying to lock my notebook whenever the screen lid is closed. I tried to combine `/usr/bin/lock -vpn` with ACPI control, by adding the lock command to /etc/rc.resume. 

ACPI sleep/resume is working fine, but when the notebook is resumed, lock is not executed, and I'm back at the shell prompt. I'm using 10.2.

Is there a way to execute lock (or any other command) after an ACPI resume?


----------



## protocelt (Aug 24, 2015)

Hi.

Since suspend and resume is working for your notebook, you could try adding a devd(8) event to run a command when the lid is opened and the notebook resumes.

Create a devd(8) configuration file in /usr/local/etc/devd/: `# touch /usr/local/etc/devd/acpi_lock.conf`
Open the file in $EDITOR and add the following:

```
notify 10 {
## Opening lid : Lock the terminal
match "system" "ACPI";
match "subsystem" "Lid";
match "notify" "0x01";
action "/usr/bin/lock -vpn";
};
```

Save the file, restart the devd(8) service(`# service devd restart`), and try it out.
Note I have not tested this myself so it may or may not work for you.


----------



## Acacio (Aug 25, 2015)

Thanks for your reply. I've seen that the devd(8) rule is executed, but lock(1) is not. Actually even /etc/rc.resume is being executed, but lock seems to be exiting with status 1. I've found this out by adding the following to /etc/rc.resume:


```
...
/usr/bin/lock -pvn
/usr/bin/logger -t $subsystem "lock ret code $?"
```

So I'm assuming that lock needs some driver/resource that is not yet available when it's executed from /etc/rc.resume. I'm trying to find lock's source code to see what it could be. I'll update the post should I find something out.

As a workaround, I've created an alias for lock(1):


```
alias k="clear; /usr/bin/lock -vpn"
```

And call it before closing the lid. This way I get the desired functionality.


----------



## protocelt (Aug 25, 2015)

Hmm, it's possible the console just isn't completely up yet when the devd(8) action is executed. You could try adding a sleep(1) command to the rule and see if that helps, otherwise, if your happy with your workaround, great. 

```
action "/bin/sleep 4; /usr/bin/lock -vpn";
```
If you have the FreeBSD source code installed, the source code for lock(1) is in /usr/src/usr.bin/lock


----------



## SirDice (Aug 25, 2015)

Instead of locking it when the lid opens you may want to lock it when the lid closes (just before it goes to sleep). On the lid open "event" there may be a small time delay before the lock actually executes, potentially providing an opening. It'll also resolve any ordering issues due to initialization when it's powering up.


----------



## protocelt (Aug 25, 2015)

That probably is the more reasonable approach if it works.


----------

