Dennis
Dennis

Reputation: 757

Setting a delayed shutdown time on a remote LInux via SSH then exiting

I have looked at all the answers here on SOF, and not seen what would work.

My goal:

Have a EC2 QA server halt itself 55 minutes after last usage

My strategy:

Everytime the remote machine is used, it is first checked to see if it is running. If it is, then do the following:

  1. ssh into machine
  2. run 'sudo shutdown -c' #clears last pending shutdown
  3. run 'sudo shutdown -h +55' #restarts timer for 55 minute shutdown
  4. exit

Go about my script's business

What Works:

All commands work locally 1 and 2. Numbers 3 and 4 are partial as explained below.

What Doesn't Work:

The line invoking the new shutdown halt, it waits for the ssh connection to shutdown, which never does for the versions of the command without '&' at the end.

What I've tried:

echo "nohup sudo shutdown -h +55; exit" | ssh -i $HOME/.ec2/cert_name [email protected]

echo "nohup sudo shutdown -h +55& exit" | ssh -i $HOME/.ec2/cert_name [email protected]

echo "sudo shutdown -h +55& exit" | ssh -i $HOME/.ec2/cert_name [email protected]

ssh -i $HOME/.ec2/cert_name [email protected] nohup sudo shutdown -h +55

ssh -i $HOME/.ec2/cert_name [email protected] nohup sudo shutdown -h +55; exit

What Sort Of Works:

ssh -i $HOME/.ec2/cert_name [email protected] nohup sudo shutdown -h +55&

ssh -i $HOME/.ec2/cert_name [email protected] sudo shutdown -h +55&

What is 'Sort Of':

The partially successful commands set up the shutdown, and exit ssh just fine. But they fills the LOCAL screen buffer with the last command results on the REMOTE screen. And that comes out with the results of the next command in the script.

I suppose that I could just issue 'echo "flushing buffer"' twice in a row and clear it.

Anything more complete and cleaner out there?

TIA

Upvotes: 2

Views: 2894

Answers (2)

jarno
jarno

Reputation: 856

This seems to shutdown after <time> seconds even if you exit SSH-connection:

( sleep <time>; sudo shutdown now ) &

provided your sudoers file allows shutdown without password.

Upvotes: 0

Dennis
Dennis

Reputation: 757

OK, 4 days later, I have something that works pretty good. I did NOT see anything like this on google or stack overflow searches.

Essentially, a cheap server is used as a keep alive (for 120 minutes) or restart (if already timed out) controller for a more expensive server. There are hints on how to do it for more complex setups than (1 cheap server)->(1 expensive server). Otherwise known as a 'exercise for the reader' :-)

https://github.com/kwince/costlyServerShutDown

The trigger to keep alive or restart is doing a 'wget' to file on the apache server root of the cheap server. Again, quick and dirty, but very effective.

Upvotes: 1

Related Questions