SHUTDOWN script format?

Discussion in 'macOS' started by Toby Goodbar, Sep 16, 2009.

  1. Toby Goodbar macrumors 6502

    Sep 8, 2006
    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?
  2. ChrisA macrumors G4

    Jan 5, 2006
    Redondo Beach, California
    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
    #! /bin/sh

    But why make a one line "script"?

    Experiemt with a script that contains a more safe command like
    echo "Hello World"
  3. Toby Goodbar thread starter macrumors 6502

    Sep 8, 2006
    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?
  4. sidewinder macrumors 68020


    Dec 10, 2008
    Northern California
    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 "", you need to do this:

    $ chmod +x

    Then, to run it from the shell, type:

    $ ./

    That's it....

  5. Toby Goodbar thread starter macrumors 6502

    Sep 8, 2006
    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?
  6. tarl macrumors newbie

    Oct 27, 2007
    The command to rename a file is mv-- short for move.
    mv filename.rtf
    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.
  7. mysterytramp macrumors 65816


    Jul 17, 2008
    You can create an AppleScript to do it:

    tell application "Finder"
    	shut down
    end tell
  8. calderone macrumors 68040


    Aug 28, 2009
    Another AppleScript option would be:

    do shell script "shutdown -r now"
    Although you would have to add the user to sudoers.
  9. Toby Goodbar thread starter macrumors 6502

    Sep 8, 2006
    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
  10. tarl macrumors newbie

    Oct 27, 2007
    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:
    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.
  11. electroshock macrumors 6502a


    Sep 7, 2009
    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:

    tell application "Finder" to shut down
  12. tarl macrumors newbie

    Oct 27, 2007
    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?
  13. Toby Goodbar thread starter macrumors 6502

    Sep 8, 2006
    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
  14. Toby Goodbar thread starter macrumors 6502

    Sep 8, 2006
    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?
  15. electroshock macrumors 6502a


    Sep 7, 2009
    OK, try this via 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.
  16. dyn macrumors 68030

    Aug 8, 2009
    -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.
  17. electroshock macrumors 6502a


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

    -s will do a shutdown rather than reboot.
  18. Toby Goodbar thread starter macrumors 6502

    Sep 8, 2006
    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
  19. TXbug macrumors member


    Aug 24, 2009
    Austin, Texas
    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.
  20. J the Ninja macrumors 68000

    Jul 14, 2008
    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.

Share This Page