I want to shut down another computer in my network when shutting down my own.

For that I have created a script that logs into that machine through ssh and powers it off. (SSH keys are set up, so no need for password.)

#! /bin/sh
ssh user@remote sudo halt

And placed it in /etc/init.d.

Then I created two symlinks pointing to this file under rc0.d and rc6.d called K10haltremote.sh.

If I call the script manually it works as expected, but when shutting down or rebooting my machine does not have any effect on the remote one.

The only thing I can figure out is that shutdown is too fast, and there is not time for the ssh connection, but it's only a guess.

Any ideas?

  • Did you check the order of scripts being executed during shutdown? maybe your network connection is cut before the ssh command? As a test you could let your script a) write random text to a file to see if it really is executed, b) make the script do a network test (ifconfig output, ping whatever) and write the output to a file, so that you know if you still have an active network connection during script execution.
    – FelixJN
    Jul 29, 2015 at 9:19
  • 1
    Many thanks. Outputting some info in a txt file I realised the file was created by su instead of my user, and helped to solve my problem. Jul 29, 2015 at 10:54

2 Answers 2

ssh user@remote sudo halt & sleep 10

Solved. The thing I didn't take into account is that the script is exectuted as root and not with my user. So when running ssh, password is required as I only set up the keys for my user. The workaround I used is forcing ssh to use my user keys:

ssh -i /home/user/.ssh/id_rsa user@remote sudo halt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.