Remote Sleep Script

185 views
Skip to first unread message

Samuel Collier

unread,
Apr 28, 2021, 11:15:06 AM4/28/21
to QLab
Hi All,

I'm pretty green when it comes to creating my own scripts. 
I have created this script in Qlab to put a Mac on the local network to sleep at the end of the evening. The idea is that it opens the terminal, logs onto the mac remotely, uses the password, puts the mac to sleep, then quits the terminal.

tell application "Terminal" to activate
tell application "Terminal"
do script "ssh av2@192,168.0.3"
delay 3
do script "bov" in window 1
delay 3
do script "osascript - tell application "system events" to sleep" in window 1
delay 10
do shell script "pkill -9 -x Terminal"
end tell

However, I am having issues with the line; 
do script "osascript - tell application "system events" to sleep" in window 1

What I want is for it to do is write "osascript - tell application "system events" to sleep" in the terminal window and run it. What's happening instead is the error; Expected end of line, etc. but found identifier (-2741).

Is there a way to get the script to view the whole line as text rather than as a continuation of the script?

Thanks in advance.

Sam

Alexander (Mailing List) Taylor

unread,
Apr 28, 2021, 1:35:55 PM4/28/21
to ql...@googlegroups.com
Hi Sam,

It looks like the quotes need to be properly escaped.  Here’s the code with that change:

tell application "Terminal" to activate
tell application "Terminal"
do script "ssh av2@192,168.0.3"
delay 3
do script "bov" in window 1
delay 3
do script "osascript - tell application \"system Events\" to sleep" in window 1
delay 10
do shell script "pkill -9 -x Terminal"
end tell

I think you could use Remote Apple Events instead of opening Terminal, or do it via SSH with keys if you want the security there.

Alexander


On Apr 28, 2021, at 11:15 AM, Samuel Collier <samuel.c...@bathspa.ac.uk> wrote:

Caution - This email is from outside ORCSD. Do not click links or open attachments unless you recognize the sender and know the content is safe.
--
Contact support anytime: sup...@figure53.com
Follow QLab on Twitter: https://twitter.com/QLabApp
User Group Code of Conduct: https://qlab.app/code-of-conduct/
---
You received this message because you are subscribed to the Google Groups "QLab" group.
To unsubscribe from this group and stop receiving emails from it, send an email to qlab+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/qlab/8a0b93b0-1009-4eee-b273-776f15dc42adn%40googlegroups.com.

The Right-To-Know Law provides that most e-mail communications to or from School District employees regarding the business of the School District are government records available to the public upon request. Therefore, this e-mail communication may be subject to public disclosure.

Alexander (Mailing List) Taylor

unread,
Apr 28, 2021, 1:59:36 PM4/28/21
to ql...@googlegroups.com
Hi Again,

I wanted to follow up, note I am not a good AppleScript coder and the script below was untested, though did compile.  Expanding on my other thought, you can accomplish this via a single SSH command, with a few gotchas.

If you SSH as the user logged in (who may have to be an admin, untested), you can use the osascript command to sleep.

If you SSH in as root, you can use a more simple command.  There are many security implications allowing root SSH access, but it is an option.

Here are the examples, both SSH commands:

ssh ro...@hostname.tld "pmset sleepnow”

ssh us...@hostname.tld "osascript - tell application \"system Events\" to sleep”

In either case, you should set up SSH key pairs so you don’t have to worry about passwords.  Here’s a link on how to do that.  https://www.mrtechnique.com/how-to-set-up-ssh-keys-on-a-mac/

If you do go with the root route, you’ll need to enable root SSH login and ensure password login for root is disabled.

Alexander

Sam Kusnetz

unread,
Apr 28, 2021, 3:10:32 PM4/28/21
to ql...@googlegroups.com
Hello all

I do not want to invalidate anything said so far, but I would like to add that it is our recommendation that any computer being used for QLab be rebooted at least once per 24 hours.

Best
Sam

––
Sam Kusnetz [he/him/his] (what is this?)
Figure 53
https://qlab.app | https://figure53.com

Maik Waschfeld

unread,
Apr 28, 2021, 3:38:35 PM4/28/21
to ql...@googlegroups.com
Hi Sam,

First, you have a typo in the ip address.
It should be „192.168.0.3“, not „192,168.0.3“.
It should be period, not comma there.

Then:
Why do you use the actual application „Terminal“ on the local mac?
Do you have to see the execution of your script?
On second thought, as you don’t have any mechanisms to catch errors, that’s not a bad idea, at all.

Then:
why you use osascript on the remote mac?
you’re already in „his“ or „hers" shell, so you can use shell commands to put him or her to sleep.
See „sh_text“ down below.
The „osascript“ didn’t work at all on my machines.

Then:
please don’t just „kill“ a process, when you think, it’s done it’s job.

I hope, that „av2“ is a user’s name, not an administrator’s name.
Leaving a remote mac logged in as an administrator isn’t a good idea!
Not even in „normal user mode“, an unattended mac should be left alone, sleeping for a longer time as absolutely necessary.


I’d recommend to use something like:

set sh_text to ("pmset sleepnow")

tell application "Terminal" to activate
tell application "Terminal"
do script "ssh a...@192.168.0.3"
delay 3
do script "bov" in window 1
delay 3
do script sh_text in window 1
delay 3
do script "exit" in window 1 -- exit from remote host
delay 3
do script "exit" in window 1 -- exit from local host
delay 3
quit -- quitting the terminal the clean way
-- this way, you get at least a note from the terminal, if something's not right.
end tell

I didn’t check, if those long delays are necessary at all.
Depends on bandwidth of your local network, too.


With kindest regards…
…Maik Waschfeld

(sent from my MBPro16,2)
also at <mailto:Maik.Wa...@Staatstheater-Stuttgart.de>



Samuel Collier

unread,
Apr 30, 2021, 11:58:05 AM4/30/21
to QLab
Thank you all very much,

I ended up using the "pmset sleepnow" command as Maik and Alexander suggested and it worked like a charm.

This was all part of an upgrade to make our front-of-house projection and music system all operate from one Cue Cart. We wanted the sleep function so that the bar staff could shut down the remote mac without causing any issues if the button was pressed by mistake. Plus it stops the staff from worrying when they see a computer still on while they are locking up.

Thanks Sam, the Qlab computers now have a timed shut down at 1am, about 30 mins after the latest bar shift, and boot back up at 8am ready for morning shows (when we are all allowed to have them again). 

To answer your questions Maik:
I was using the terminal as I wanted confirmation that the scripts were working,  the Qlab is triggered from a remote tablet so I don't think that having the window popping up will cause any show-related issues. 
Weirdly the "Osascript" was working on mine when I was typing it manually into the terminal, hence why I wanted to use it in the script.
Don't worry AV2 isn't an admin, all that can be done from that account is run mapping software and the content video! Plus the Remote Mac, the main Qlab machine, and switch are securely locked away in their racks far from where the bar staff or public can access them! That said I'll talk to the rest of the department about getting better passwords. 

Thanks again everyone!

Sam C

Bristol Old Vic
Reply all
Reply to author
Forward
0 new messages