Linux shutdown in 30 seconds?
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.
Solution 1:
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.
Solution 2:
In a nutshell do: (sleep 5 && shutdown now) &
NOTE: the command backgrounds everything in the paren, the commands will sleep for 5 seconds then shutdown now.
It is possible to get second granularity for the shutdown
command.
You can do it by background-ing a bash command.
bash -c "sleep 30; shutdown -h now"&
Here is an example with output. I used the date command to illustrate how long the sleep was.
$ bash -c "date; sleep 5; date"&
[1] 2425
chris@SR-ENG-P18 /cygdrive/c/Projects
$ Tue, Sep 15, 2020 4:30:04 PM
Tue, Sep 15, 2020 4:30:09 PM
[1]+ Done bash -c "date; sleep 5; date"
In this example, I do the exact same thing except that I spam the enter key a few times to show you that I'm at a prompt during the sleep.
chris@SR-ENG-P18 /cygdrive/c/Projects
$ bash -c "date; sleep 5; date"&
[1] 2467
chris@SR-ENG-P18 /cygdrive/c/Projects
$ Tue, Sep 15, 2020 4:33:47 PM
chris@SR-ENG-P18 /cygdrive/c/Projects
$
chris@SR-ENG-P18 /cygdrive/c/Projects
$
chris@SR-ENG-P18 /cygdrive/c/Projects
$
chris@SR-ENG-P18 /cygdrive/c/Projects
$
chris@SR-ENG-P18 /cygdrive/c/Projects
$
chris@SR-ENG-P18 /cygdrive/c/Projects
$ Tue, Sep 15, 2020 4:33:52 PM
[1]+ Done bash -c "date; sleep 5; date"
chris@SR-ENG-P18 /cygdrive/c/Projects
I would use this in scripts that need to force a shutdown.
Solution 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.
Solution 4:
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.