Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Restarting a remote Mac via AppleScript Apps
When logging into a Mac remotely, sometimes it's necessary to restart the computer (for example, after performing an update using softwareupdate). Although one could use sudo reboot, this doesn't allow the Aqua apps to exit gracefully. So if there are any unsaved changes in an open document, you will lose those changes. Instead one should use AppleScript:
% sudo osascript -e 'tell application "Finder" to restart'
Notes:
  1. If your user isn't the currently logged in user, you might get some errors in the command line, but it will work.
  2. sudo might not be required.
  3. If there is an open application with unsaved changes, the restart will time out. This is the intended behaviour. You could try to use applescript to save and exit that application (if said application is scriptable).
[robg adds: If the remote machine absolutely positively must be restarted, and you don't want to lose any unsaved changes (and the apps aren't scriptable), you'll have to use something like Timbuktu or OSXvnc to connect to the GUI on the remote Mac. Once you've taken that effort, though, you might as well just choose Restart in the GUI as well!]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[15,065 views]  

Restarting a remote Mac via AppleScript | 11 comments | Create New Account
Click here to return to the 'Restarting a remote Mac via AppleScript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Restarting a remote Mac via AppleScript
Authored by: ewheet on Aug 01, '03 11:22:05AM

You can also use "sudo shutdown -r now" I believe this will shut down the machine gracefully.



[ Reply to This | # ]
Restarting a remote Mac via AppleScript
Authored by: osax on Aug 01, '03 03:55:38PM

"shutdown -r now" will not quit GUI apps gracefully, it simply kills them. The script method is really the way to go if you rely on GUI apps. You could even go one step further and send running apps something like "quit with saving" or "save to desktop; quit" before telling the Finder to restart. Somebody should really write this into the "perfect" AppleScript...

In addition, you should check if you have to stop some services by hand before restarting. FileMaker Server comes to mind, but don't get me started on this subject...



[ Reply to This | # ]
Restarting a remote Mac via AppleScript
Authored by: john_e on Aug 02, '03 07:32:48AM

Yes exactly, if you restart a machine via the shell you should use shutdown -r now (or some value). To shut it down invoke shutdown with -h instead of -r.
The reboot command should only be used if the machine won't restart normally. It's basically the equivalent of pushing the power button, i.e not very graceful.



[ Reply to This | # ]
Restarting a remote Mac via AppleScript
Authored by: ynolo on Aug 02, '03 03:54:03PM

since sudo is already being used why not just use

sudo reboot or sudo shutdown

less typing and efficient

You cannot have everything with applescript, :)

---
i don\'t have one



[ Reply to This | # ]
Restarting a remote Mac via AppleScript
Authored by: osxpounder on Aug 03, '03 04:17:17PM

Why not, since you're logging in remotely, simply start a VNC server and use the GUI to shut down any apps you see? If you're accessing it via a Windows client, you can get TightVNC, which offers a range of compression options to help you get optimal performance in any given VNC session.

Whatever you use, you can easily use VNC if you have a computer with a broadband connection to the Internet.

Here's a few tips for using VNC I've picked up, mainly from this site. I gather them here for your convenience:

- use a shell script to start the vnc server whenever you want, from a command prompt. That way, you can use the next tip:

- only run the VNC server for the time you need it. When you aren't logged in remotely to the server, kill the VNC server. Why leave a point of attack availalble for hackers if you're not using it?

- When you're done with your VNC session and you want to stop the OSXVNC-server program, type "ps -ax|more" at the command prompt and hit ENTER. This will list the programs running on your OSX box at the moment. Look for the OSXVNC-server program.

Note its process id number, and type "kill [paste its process id number here]. Press ENTER, and the server is killed.

- Since you know you might be using VNC to access your machine, leave its Display preferences and similar settings in a state that works best with VNC and keeps the display lag to a minimum:

-- Turn off Dock hiding, and turn off magnification in the Dock preferences, too.

-- If you have two monitors, go to Display preferences and click the Arrange tab. Check the box to "Mirror Displays".

-- Get rid of the desktop picture; go with a plain color.

-- Clean up your desktop, if you have a really slow connection. Give the VNC system less graphical detail than necessary, and your screen updates will happen more quickly, reducing the lag in the interface.

-- You can even hide drivers and servers on your desktop. You can always see them by creating a new Finder window; no sense lagging the display if you don't need to see them.

-- Use keyboard shortcuts; that way you don't have to wait for the VNC system to update your screen, and you essentially sidestep most of the lag you'd experience if you used the mouse. For example, on an Windows TightVNC client, connected to an OSX box that's running a VNC server:

Alt-N creates a new window in the finder Alt-Shift-N creates a new folder in the finder Alt-W closes a window in any app Alt-Q quits a program

You get the picture. My client maps the Alt key in Windows to the Apple key on the OSX machine. Using the keys means you don't have to deal with the display's lag issue at all.

- the most beautiful tip of all: if you forget to do any of these things, you can do them remotely via the VNC server; you'll just be dealing with a laggy display [and hence, interface response will be slower] long enough to make a few quick changes, such as mirroring displays, and then you're done. In other words, if you forget to leave your OSX display and dock preferences set nicely for VNC, you can make those changes after you log in with VNC. Not a problem.

---
--
osxpounder

[ Reply to This | # ]

Restarting a remote Mac via AppleScript
Authored by: derekhed on Nov 10, '04 01:56:51PM

Nice list, osxpounder.

Allow me to add one more thing that I have found useful. I allow only ports 80 and 22 through my router, and have VNCServer running at startup (boooot). (That's not a typo, my computer just takes a long time to start up :-).

Bad jokes aside, if I tunnel my VNC connection over SSH, I can also use the compression flag -C and get about 3X the speed of my broadband connection. Very nice. And of course, all the normal security advantages are there. This has made the entire procedure useful, where before it was just possible.

ssh -2 user_name@home_IP_number -L 5901:127.0.0.1:5901 -C



[ Reply to This | # ]
Restarting a remote Mac via AppleScript
Authored by: newbish on Aug 04, '03 09:07:21AM
You can also tell your Mac to sleep by replacing the word 'restart' with the word 'sleep.'

I can confirm that it works quite nicely, as I now cannot SSH into my Mac at home because it is now sleeping! :)

Be careful with the shutdown command unless you are certain there is nothing you need to save in the Aqua layer. Shutdown, as a UNIX-layer command, will pull the rug out from everything else. The only thing it does gracefully is offer you the opportunity to delay the actual shutdown to give other users on your Mac the chance to save their work before it actually goes down. You can also have it broadcast a message to other terminals to warn other users.

Bear in mind, the only warnings users will receive are broadcast to the Terminal, not to the GUI. If someone is sitting at your Mac at home when you send the shutdown command via SSH to the terminal, that person will be in for a very unpleasant surprise. Makes for a really nasty April Fool's prank on someone...!

[ Reply to This | # ]
Restarting a remote Mac via AppleScript
Authored by: Tom Robinson on Aug 04, '03 03:46:59PM

I'm not trying to be picky, but under AppleScript and OS X you should be talking to System Events rather than the Finder about global commands like shut down, sleep, and restart.

If you look in the Finder's dictionary these now appear under the Legacy suite, and the Finder just forwards them to the appropriate app.

Cheers



[ Reply to This | # ]
Restarting a remote Mac via AppleScript
Authored by: vonleigh on Aug 04, '03 08:12:35PM

I thought so too, it's not a task for the finder, but I wasn't able to find the proper syntax. Can you provide code?

v



[ Reply to This | # ]
Restarting a remote Mac via AppleScript
Authored by: Tom Robinson on Aug 05, '03 05:17:39AM

Just replace 'Finder' with 'System Events':

tell application "System Events" to sleep

Cheers



[ Reply to This | # ]
Restarting a remote Mac via AppleScript
Authored by: msk on Nov 09, '04 09:33:59AM

"When logging into a Mac remotely, sometimes it's necessary to restart the computer (for example, after performing an update using softwareupdate)." and that AppleScript does not work if the Finder is not running since you logged in remotely via ssh and noone else is logged into the machine directly, the error message is "24:34: excution error: Application isn't running. (-60)".

Following the logic of the other replies I tried:

osascript -e 'tell application "System Events" to restart'

and no go, tried it both using sudo and after using sudo -s (certain commands won't work using sudo, for example "nvram "skip-netboot?=true").

I should also note I just finished a softwareupdate which included QuickTime and I had to walk over to the machine to press continue even though I had issued the command "setenv COMMAND_LINE_INSTALL 1" beforehand as recommended by an Apple Engineer.



[ Reply to This | # ]