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

prramesh

macrumors newbie
Original poster
Jul 16, 2008
28
0
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....
 
There is a shell command for doing shutdown. Unsurprisingly, it's called 'shutdown'. In Terminal its man page is:
Code:
man shutdown
You could also use AppleScript to initiate shutdown, using the osascript command:
Code:
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.
 
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.

example:
scp name@host:~/filepath localpath
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
osascript -e 'tell app "System Events"
does the job, but sometime it is problematic that it won't force the shutdown (asking for save document, etc).
 
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.

example:
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.
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.


Instead, using osascript does the job, but sometime it is problematic that it won't force the shutdown (asking for save document, etc).
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.
 
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.
 
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.
 
SCP shutdown

I don't understand what you're expecting it to do.

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
become a superuser by entering
sudo -s
now we can use
scp name@server.com:~/filepath localpath; sudo shutdown -h now
or
sudo shutdown -h `scp name@server.com:~/filepath localpath > /dev/null ; echo "now" `

also the following works (but it won't force shutdown)
scp name@server.com:~/filepath localpath; osascript -e 'tell app "System Events" to shut down'

I try to work around with "<my_username> LOCAL= NOPASSWD: /sbin/shutdown" but no luck.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.