Linux shutdown in 30 seconds?

8

1

Simple question: what params can be used to shutdown a computer running Linux/OSX in 30 seconds? I've always run Windows, where I would go shutdown -s -t 30 but the parameters are different. I've looked it up here but it will only let you shut a computer down at a specific time (like 8:00) rather than in a specific amount of seconds.

icnhzabot

Posted 2011-05-12T22:47:55.920

Reputation: 183

Answers

8

In OS X you can shutdown in one minute using the following command:

sudo shutdown -h +1

AFAIK, it's not possible to specify seconds instead of minutes with this command.

You can also reboot by using -r instead of -h.


EDIT:

As you mentioned in your comment, you can add a delay programmatically, then shut down the system immediately with

shutdown -h now

But note that that command will require root access.

Austin

Posted 2011-05-12T22:47:55.920

Reputation: 196

meh. So no using decimals.

I suppose that's alright, I was wondering because I'm trying to work on a cross-platform Java application that can shutdown a computer. So I could just make it sleep for 30 seconds before shutting down immediately.

But would sudo shutdown -h shutdown the computer without an intentional delay? – None – 2011-05-12T22:55:29.723

@icnhzabot sudo shutdown -h now would shut the computer down immediately with no warning. – Austin – 2011-05-12T23:07:14.177

ok thanks. so for root access I should do sudo shutdown -h now right? – icnhzabot – 2011-05-12T23:29:45.057

@icnhzabot That won't work for an automated script/program because it prompts the user for their administrator password at the command line. You'd have to run your script as root and just call shutdown -h now. If you need help with that, you should make a new question (assuming you can't find the answer elsewhere). Also, don't forget to mark the best answer here as "accepted" if it answered your question. – Austin – 2011-05-12T23:43:56.130

3

Using the basic calls, I don't see a way to do it with seconds, but it looks like you can do it with minutes:

time Time is the time at which shutdown will bring the system down and
     may be the word now (indicating an immediate shutdown) or specify
     a future time in one of two formats: +number, or yymmddhhmm,
     where the year, month, and day may be defaulted to the current
     system values.  The first form brings the system down in number
     minutes and the second at the absolute time specified.

In other words:

shutdown -h +1

If you want to shut it down in 1 minute.

onteria_

Posted 2011-05-12T22:47:55.920

Reputation:

do you know if it would work with .5? – None – 2011-05-12T22:53:26.120

There's an error here -- the -r option is for reboot, not shutdown. You have to use -h for shutdown. – Austin – 2011-05-12T22:54:03.610

@Austin ugh, fixed that inline. – None – 2011-05-12T22:56:10.673

3

As pointed out, the command

sudo shutdown -h +1

Adds one minute.

If you wanted to do it in seconds or hours or something very specific you could do something like:

shutdown -h `date --date "now + 60 seconds"`

EDIT: The above no longer works on more recent builds of Ubuntu. Thanks for pointing that out @zitrax. My mistake you're right.

But you can still do it but it seems maybe to the nearest minute.

sudo shutdown -h `date --date "now + 10 minutes" "+%H:%M"`

Which is somewhat pointless when the +m parameter is easier to type.... ahhh oh well.

Matt H

Posted 2011-05-12T22:47:55.920

Reputation: 3 823

Does not work (at least on linux), this gives the time in another format than expected. – Zitrax – 2014-02-21T12:30:19.087

@Zitrax, just because it didn't work in your case doesn't mean it doesn't work in all flavours of Linux. I'd like you to also note the backtick ` is different to a single quote '. I'm guessing this is why it didn't work for you. This absolutely works in all versions of Ubuntu since at least 10.04. And as Ubuntu is based on Debian it'll work on that too. It might be more constructive to say "Does not work on ...." rather down down vote and make a blanket statement. Linux is a generic term, it's not a version. But I actually think you typed it wrong. Try again. – Matt H – 2014-02-22T13:18:51.340

I tried again on my home machine and it's not working there either. I know how backticks works. So I have tried on Ubuntu 13.10 and Kubuntu 13.10. Could be that I using a different locale ? Output from date --date "now + 60 seconds" for me is lör 22 feb 2014 21:25:03 CET and if I use that together with shutdown it prints "invalid timevalue" (translated to English). So your statement that the version you wrote absolutely work in all versions of ubuntu is simply wrong. If I specify the format to date it works though, like this: date --date "now +60 seconds" +%H:%M. – Zitrax – 2014-02-22T20:32:53.103

Tried switching to english locale, but it didn't work either. So I am not sure why it works for you. – Zitrax – 2014-02-22T20:37:32.280

Must be your version of the shutdown command. Looks like the version that comes with Ubuntu may be different. It's authored by Scott Remnant from Conanical. It must be that it accepts a wider range of date input values. Works for me and at least 3 others who upvoted. – Matt H – 2014-02-23T22:22:25.040

I have: shutdown --version: shutdown (upstart 1.10) Copyright (C) 2013 Scott James Remnant, Canonical Ltd. – Zitrax – 2014-02-24T10:41:22.087

I beg your pardon. You're right it no longer works. It actually used to work. I think the shutdown command isn't as flexible as it used to be for some reason. This used to work on 10.04. – Matt H – 2014-02-24T14:07:17.460

Alright, then we sorted that out. Could add the format flags I mentioned and it should work also on new versions (although on minute resolution). – Zitrax – 2014-02-24T14:11:47.030