Is there a way to trigger the lid close action programmatically with a script or through terminal?
I am testing this by placing a small magnet near the SD slot on my MacBook Pro.
There is no Apple supported programming interface to simulate the lid being closed.
You can put your Mac to sleep using this command line:
osascript -e 'tell application "System Events" to sleep'
You may put your machine to sleep with a shell script:
P.S. If you intend to run this from a script, be prepared that machine won't sleep until script finishes, and if that's something like a daemon running in a loop, it won't ever work unless you run
pmset in a subshell:
(pmset sleepnow &)