Restore session (reopen windows) after a **CLI** shutdown on Big Sur

I'm automating some tasks, like brew upgrade then shutdown, or Duplicati backup (no Time Machine), wait for cloud sync then shutdown, but I can't seem to find a CLI command which WILL reopen my windows/restore session on the next boot.

I've tried one of these, also added them to NOPASSWD sudoers:

    (ALL) NOPASSWD: /sbin/shutdown, /sbin/reboot, /sbin/halt

but I can't seem to find one which actually restores the session too.

Is this feature implemented and available from CLI? (Usually when I shut my Macbook down, I use the Apple menu, but a script can't click for me, can it?)

I have a 2018 15" MacBook Pro with the latest Big Sur (11.6.2 (20G314)).

Thank you!


Solution 1:

In your, e.g., bash/zsh shell script you can use e.g.:

osascript -e 'tell app "Finder" to restart'

Or:

osascript -e 'tell app "Finder" to shut down'

Then any open applications and their window will be restored, assuming you have not unchecked [√] Reopen windows when logging back in on the standard restart and shut down dialog box, e.g.:

enter image description here

Note that you will not see this dialog box when using the commands shown above.