SCP shutdown system

Discussion in 'Mac Programming' started by prramesh, Oct 7, 2011.

  1. prramesh macrumors newbie

    Jul 16, 2008
    Is there any way to automatically shutdown the system after the completion of file transfer over ssh (SCP)? I use Terminal to SCP file transfer, which takes few hours to complete and I want the system to go down after the job done.

    Hope there must some way....
  2. chown33 macrumors 604

    Aug 9, 2009
    There is a shell command for doing shutdown. Unsurprisingly, it's called 'shutdown'. In Terminal its man page is:
    man shutdown
    You could also use AppleScript to initiate shutdown, using the osascript command:
    osascript -e 'tell app "System Events" to shut down'
    Use AppleScript Editor to open the scripting dictionary of System Events, and look at the Power Suite of commands.
  3. prramesh thread starter macrumors newbie

    Jul 16, 2008
    SCP shutdown

    I'm not familiar to command lines, still I knew the shutdown commands, but I don't know how to execute them while the file transfer in progress.

    this command line transfers the file, but if I subsequently give "sudo shutdown -h now" in the same terminal window then doesn't ask for password (sudo used in shutdown). It asks password after the completion of file transfer.

    Instead, using
    does the job, but sometime it is problematic that it won't force the shutdown (asking for save document, etc).
  4. chown33 macrumors 604

    Aug 9, 2009
    I don't understand what you're expecting it to do.

    If the file transfer is in progress, then a shutdown will abort that transfer without completing it. That makes no sense. I would think you want the transfer to complete.

    If the shutdown command is executed after the file transfer, then of course it's going to ask for the 'sudo' password then.

    Again, I don't know what you're expecting it to do.

    If you have an application open with unsaved data, then of course you should expect a shutdown to ask you to save the data. If you don't want that to happen, then save the data before starting the transfer and shutdown.

    Perhaps you should think about building yourself a Workflow in Automator that performs the file transfer in Terminal, waits for it to complete, then quits Terminal. Only then do the script that tells System Events to shut down.

    If the file transfer is a single shell command, then you can script that as a single action in Automator, too. That might make more sense than telling Terminal to do it. Hard to say, since you haven't explained exactly what you're trying to accomplish.

    I suggest using Automator because Automator workflows are more clearly a sequence of action steps, which you don't seem to realize is also how command-lines work.
  5. willieva macrumors 6502

    Mar 12, 2010
    If you're doing the scp from the command line already, then it's quite easy. Just run the command like:
    sudo shutdown -h `scp file remotemachine:/path > /dev/null ; echo "now" `

    This will first run the scp command, then the shutdown command when it is done.

    You'll also have to add a line to /etc/sudoers so you won't have to type your password . It should look like:
    prramesh localhost = NOPASSWD: shutdown -h now

    No guarantees on the correctness of the sudoers line.
  6. willieva macrumors 6502

    Mar 12, 2010
    Ok, the correct line for the sudoers file is:
    prramesh localhost = NOPASSWD: /sbin/shutdown
    where localhost is the actual name of your computer, and prramesh is the username you use on your computer.
  7. prramesh thread starter macrumors newbie

    Jul 16, 2008
    SCP shutdown

    Well, I want to copy a file from remote system to local system via ssh. After the 100% transfer, the local system should get shutdown.

    Thanks "willieva" for your tips, I looked around and found some solution, but still not the best.

    following works well
    I try to work around with "<my_username> LOCAL= NOPASSWD: /sbin/shutdown" but no luck.

Share This Page