Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Toby Goodbar

macrumors 6502
Original poster
Sep 8, 2006
400
0
i want to write a script using the command to shutdown as done in terminal

"shutdown -r now"

(please correct me if i'm wrong)

ive been using script editor to attempt saving a script but i'm doing something wrong. i am using the terminal version because i don't want it ask to close tabs on browsers, etc. i want it to shutdown the computer without any questions. force quit, whatever it takes.

what is the best way to accomplish this? or what format does the script need to be saved in?
 
...
what is the best way to accomplish this? or what format does the script need to be saved in?

Save it as a simple ascii text file.

However you will need to set "execute" permission on the file and it helps if the first line of the text file reads
Code:
#! /bin/sh
.

But why make a one line "script"?

Experiemt with a script that contains a more safe command like
Code:
echo "Hello World"
 
i want to script it because i can access my mac remotely from my iphone via ssh and then i can activate scripts to run.

so to clarify i would write the following in text editor

#! /bin/sh
shutdown -r now

then in what format would i save it? or with what extension rather?
 
i want to script it because i can access my mac remotely from my iphone via ssh and then i can activate scripts to run.

so to clarify i would write the following in text editor

#! /bin/sh
shutdown -r now

then in what format would i save it? or with what extension rather?

It doesn't matter what you call it. Some people like to save shell scripts with a ".sh" on the end just so they know what it is.

However, you need to make it executable. So, assuming the is called "sd.sh", you need to do this:

$ chmod +x sd.sh

Then, to run it from the shell, type:

$ ./sd.sh

That's it....

S-
 
this should be a 2 minute thing, i know.....

anyway i cant save it as .sh extension. it always adds the .rtf via finder. in terminal, it says the command "rename" can't be found.

i need to create something that is a clickable executable file that will initiate the shutdown command in terminal, or with something to the same effect.

even if its just a script that opens terminal, and enters in the commands. i figured out how to open terminal but NOT how to get it to enter the command lines. do you (anybody) know how?
 
this should be a 2 minute thing, i know.....

anyway i cant save it as .sh extension. it always adds the .rtf via finder. in terminal, it says the command "rename" can't be found.

i need to create something that is a clickable executable file that will initiate the shutdown command in terminal, or with something to the same effect.

The command to rename a file is mv-- short for move.
Code:
mv filename.rtf filename.sh

But that's not your only problem. From the .rtf extension I'm guessing that you wrote the script in TextEdit. That's not going work. Files ending in .rtf are Rich Text Files. They include invisible characters that tell the document what font, how big, etc. All that stuff will get in the way of the script running correctly.

You need to create the script in a text editor that can save pure text files. TextWrangler is a decent choice. Or if you feel up to it, use built-in applications called pico, nano, or even vi or emacs directly in Terminal. (You'll have to find tutorials for those elsewhere)

Once you create the correct kind of text file, then you can set its execute bit and run it from Terminal.
 
Another AppleScript option would be:

Code:
do shell script "shutdown -r now"

Although you would have to add the user to sudoers.
 
tarl: thanx for the info! ive been looking for that one. i really just need to find a book with the commands for terminal. some of the old xenix ones work and some don't. i need to update my knowledge!

Another AppleScript option would be:

Code:
do shell script "shutdown -r now"

Although you would have to add the user to sudoers.

i just figured that out. but what i couldn't figure, was how to add my account (the only one, and it is admin obviously) as a super user aka add me to the sudoers
 
i just figured that out. but what i couldn't figure, was how to add my account (the only one, and it is admin obviously) as a super user aka sudoers

Rather than opening up a possible security hole by creating more sudoers than necessary, just log into the Mac using an administrator username. They have sudo capability.

Then just:
Code:
sudo shutdown -h now

No script needed.

Also, your first post used the -r option. That will restart the Mac rather than shut it down. -h stands for halt.
 
tarl: thanx for the info! ive been looking for that one. i really just need to find a book with the commands for terminal. some of the old xenix ones work and some don't. i need to update my knowledge!

i just figured that out. but what i couldn't figure, was how to add my account (the only one, and it is admin obviously) as a super user aka add me to the sudoers

The first AppleScript example (with the tell) is much simpler because you don't have to deal with sudo, setting up sudoers, and stuff of that nature. Simple is good. :) Means less things to possibly goof up or break.

In fact, you can shorten the AppleScript to just one line:

Code:
tell application "Finder" to shut down
 
Why are you trying to do this?

I initially thought you were trying to shutdown a Mac remotely. After rereading you posts it seems like that is not the case.

Shutting down the Mac with any of these scripts will work, but it's quite likely to lead to problems down the road. Shutting down a Mac by always force quitting applications and the OS is going to lead to corrupt files.

Why are you not using the Shutdown command in the Apple menu?
 
The first AppleScript example (with the tell) is much simpler because you don't have to deal with sudo, setting up sudoers, and stuff of that nature. Simple is good. :) Means less things to possibly goof up or break.

In fact, you can shorten the AppleScript to just one line:

Code:
tell application "Finder" to shut down

I did that before, but it will want confirmation of any open apps. Safari closing tabs, or firefox, transmission with active transfers,iTunes wiu someone connected to shared library, etc
 
Why are you trying to do this?

I initially thought you were trying to shutdown a Mac remotely. After rereading you posts it seems like that is not the case.

Shutting down the Mac with any of these scripts will work, but it's quite likely to lead to problems down the road. Shutting down a Mac by always force quitting applications and the OS is going to lead to corrupt files.

Why are you not using the Shutdown command in the Apple menu?

I am doing it remotely. I'm using my iPhone safari which connects to a server on my mac (iPhone remote aka telekinesis) and allows me access to files like turning on a movie, or opening a picture, or even turning on an app. So if I want to shutdown the mac without all the "are you sure?" dialogs which I can't see all of them anyway because I have multiple screens, I need to shut it down with terminal. This won't be so often that I'm worried much about file corruption.

Does anyone know how to make my account a sudoer or some code I can put in with the shell script?
 
Does anyone know how to make my account a sudoer or some code I can put in with the shell script?

OK, try this via Terminal.app on the target Mac in question as a ONE-time-only setup step:

$ sudo visudo

and add a line that reads:

%admin ALL=(root) NOPASSWD: /sbin/shutdown -r now

Then anybody with an admin account on the Mac can do:

$ sudo /sbin/shutdown -r now

And it'll fire off the shutdown -- no prompting for a password or confirmation at all.
 
Code:
-r      The system is rebooted at the specified time.

You need to use something else since the command will only reboot the machine instead of doing a power down. Use the "poweroff" command as lenchnikin says or check out man shutdown for more options (like -h).
You also need to configure every program to not ask to save anything when shutting down. This might also be accomplished via some sort of applescript.
 
Code:
-r      The system is rebooted at the specified time.

You need to use something else since the command will only reboot the machine instead of doing a power down. Use the "poweroff" command as lenchnikin says or check out man shutdown for more options (like -h).
You also need to configure every program to not ask to save anything when shutting down. This might also be accomplished via some sort of applescript.

Actually, I goofed because I'm used to '-r' by default -- force of habit.

-s will do a shutdown rather than reboot.
 
thanx to all who replied. what i finally did to accomplish my desired result was to use my iphone to access terminal via mobile safari and then log in as root and use either the "shutdown -h now" since i have finally put a password on my single account, i plan on experimenting with "shutdown -r now" and see if it reboots to a login screen as it should.

thanx again! we have a great community here. i appreciate all the comments and input
 
To shutdown and halt the system now -
shutdown -h now​

To shutdown and halt the system now (immune to hangups) -
nohup shutdown -h now &​
This works if you break the ssh connection.
In a terminal session do man nohup for a detailed explanation.
In a terminal session do man shutdown for a detailed explanation of the shutdown command.
 
To the user who mentioned the -s flag, that puts the machine to sleep (whatever state the power management file has defined for that).


Also, you can too get extension-less plain text files out of TextEdit. Select Format > Make Plain Text, then when you save it, don't type an extension, and uncheck the box that says "If no extension provided, use .txt". There's also an option in preferences to not append the .txt unless the user actually types it.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.