Detect if Macbook is closed?

Hi.
Is there a way to detect if the macbook is getting closed while the program is running? Cause i have a script setting up an ssh connection and every time i close the macbook to get it with me, the ssh connection is shut down. Now I want the programm to reconnect automatically after open the mac.

Is this possible?

I would rather change your ssh settings than creating a script for it. You can setup ssh to reconnect after you’ve closed your macbook for a certain period of time.

This is what the ssh manual has to say about it:

So when you close your macbook for max 4 hours you can put this in you ssh config file:
[format]ServerAliveInterval 10
ServerAliveCountMax 1440 [/format]

update: to answer the question of the title of this topic:


set plistData to do shell script "ioreg -r -a -k AppleClamshellState"
tell application "System Events"
	set plistItem to make new property list item with properties {text:plistData}
	set plistRecord to value of plistItem
end tell
return AppleClamshellState of item 1 of plistRecord