68

It seems I can shutdown using sudo shutdown by specifying a time or minutes.

Is there a way to specify datetime for shutdown?

Askar
  • 1,098

5 Answers5

106

You can do this directly from the shutdown command, see man shutdown:

SYNOPSIS
   /sbin/shutdown [-akrhPHfFnc] [-t sec] time [warning message]

[...]

   time   When to shutdown.

So, for example:

shutdown -h 21:45

That will run shutdown -h at 21:45.


For commands that don't offer this functionality, you can try one of:

A. Using at

The at daemon is designed for precisely this. Depending on your OS, you may need to install it. On Debian based systems, this can be done with:

sudo apt-get install at

There are three ways of giving a command to at:

  1. Pipe it:

    $ echo "ls > a.txt" | at now + 1 min
    warning: commands will be executed using /bin/sh
    job 3 at Thu Apr  4 20:16:00 2013
    
  2. Save the command you want to run in a text file, and then pass that file to at:

    $ echo "ls > a.txt" > cmd.txt
    $ at now + 1 min < cmd.txt
    warning: commands will be executed using /bin/sh
    job 3 at Thu Apr  4 20:16:00 2013
    
  3. You can also pass at commands from STDIN:

    $ at now + 1 min
    warning: commands will be executed using /bin/sh
    at> ls
    

    Then, press CtrlD to exit the at shell. The ls command will be run in one minute.

You can give very precise times in the format of [[CC]YY]MMDDhhmm[.ss], as in

$ at -t 201403142134.12 < script.sh

This will run the script script.sh at 21:34 and 12 seconds on the 14th of March 2014.

B. Using cron (though this not a good idea for shutdown)

The other approach is using the cron scheduler which is designed to perform tasks at specific times. It is usually used for tasks that will be repeated but you can also give a specific time. Each user has their own "crontabs" which control what jobs are executed and when. The general format of a crontab is:

*     *     *     *     *  command to be executed
-     -     -     -     -
|     |     |     |     |
|     |     |     |     +----- day of week (0 - 6) (Sunday=0)
|     |     |     +------- month (1 - 12)
|     |     +--------- day of month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)

So, for example, this will run ls every day at 14:04:

04 14 * * * ls

To set up a cronjob for a specific date:

  1. Create a new crontab by running crontab -e. This will bring up a window of your favorite text editor.

  2. Add this line to the file that just opened. This particular example will run at 14:34 on the 15th of March 2014 if that day is a Friday (so, OK, it might run more than once):

    34 14 15 5  /path/to/command        
    
  3. Save the file and exit the editor.

This SO answer suggests a way to have it run only once but I have never used it so I can't vouch for it.

terdon
  • 242,166
  • 3
    Scheduling a shutdown with cron is a really bad habit. Suddenly you will wonder why the server is down every Monday morning. – Ouki Mar 20 '14 at 02:12
  • @Ouki Which is why I did not even use it as an example, if you read carefully, I gave the way to do it for shutdown and then moved to other programs. Also, you're assuming the OP is using a server which is probably not the case. – terdon Mar 20 '14 at 02:14
  • 5
    @Ouki I-m sorry but I just don't see where I recommend using cron for shutdown. I mention in a parenthesis that it is possible (it is) but do not recommend it. We tend to like comprehensive answers here which is why I offered the other alternatives while never suggesting they should be used for shutdown. I feel for that bad experience though, that couldn't have been pleasant. – terdon Mar 20 '14 at 02:24
  • 2
    This is more about suggestion, as you are mixing shutdown scheduling and cron in the same answer... Whether it is a workstation or a 1000 users mail server (my sad experience) is more about the consequences of a bad habit. – Ouki Mar 20 '14 at 02:39
  • 3
    I'm with Ouki here. When discussing shutdown, one should not include cron. (esp. with someone who won't know why that's a horrible idea.) – Ricky Mar 20 '14 at 02:57
  • Is it really possible to have a unix system where at is not yet installed? – gerrit Mar 20 '14 at 14:12
  • @gerrit "User-friendly" distributions like Ubuntu don't install it by default. Debian does, but not Ubuntu so, presumably, not any of the Ubuntu derivatives either. – terdon Mar 20 '14 at 14:22
  • 1
    This is the linux/unix answer. It is unfortunate that the "install this gtk application" was chosen. – dfc Mar 20 '14 at 16:31
  • 5
    I'm a little confused that this answer seems to lead with information already assumed by the question. It looks like the OP knows how to specify a time for shutdown, but wants to know how to specify both a date and a time. That is addressed in this answer, but it's provided as if it's extra information, when it's what was being asked for in the first place. – Weeble Mar 21 '14 at 12:19
  • @RickyBeam (and OP, i.e. terdon), why is using cron for shutdown horrible idea? – Muhamed Huseinbašić Jul 18 '18 at 11:26
  • @Ouki, I would like your inputs about why is using cron for shutdown bad idea as well. Is there anything else except the possibility one could forget about it? – Muhamed Huseinbašić Jul 18 '18 at 11:27
  • 1
    @MuhamedHuseinbašić please don't start that sort of discussion in comments. Ask a new question instead. – terdon Jul 18 '18 at 12:04
  • See https://unix.stackexchange.com/a/465337/5132 for some of the differences and similarities between giving the time to shutdown and giving the time to at. – JdeBP Jan 07 '19 at 14:07
31

No, you can't specify a date at the shutdown command, but two alternatives exist:

  1. The easiest is to use the at command.  The following example will execute shutdown +5 at a specific time and day:
    echo "shutdown +5" | at 10:05am 2019-01-19
    
  2. If you do not mind using your calculator and want to shutdown in, say, 24 hours (24×60=1440 minutes) and you are absolutely sure the system will not reboot in between:
    shutdown +1440
    
ndemou
  • 2,809
  • 3
    The reason for my answer above: terdon's answer is correct and very comprehensive but too chatty (IMHO at least) – ndemou Dec 23 '14 at 11:18
  • 3
    If by chatty you mean helpful and comprehensive... Your answer didn't even explain the at command . – Anthony Nov 14 '16 at 18:37
  • @Anthony I believe this way of using at deserves no explanation. I can see you believe otherwise. Different minds, different views. – ndemou Nov 15 '16 at 07:41
  • This answer is no longer true. The van Smoorenburg, Upstart, and systemd shutdown commands do not allow a date specification. But the nosh toolset's shutdown does, supporting the long-standing BSD syntax. – JdeBP Jan 07 '19 at 14:35
  • @JdeBP: what alternative of the two do you believe is no longer true and have you tested and verified they are not? I've just tested both commands in 3 systems (Ubuntu 18.04,16.04 & CentOS 7) and they worked fine. Regarding the rest of your comment ("The van Smoorenburg...") I'm not sure if it's a clarification of your statement that this answer is no longer true or a general comment. Maybe you should post an answer with a bit more information. – ndemou Jan 08 '19 at 09:22
  • The bit that is no longer true is the bit that I explicitly contradicted. – JdeBP Jan 08 '19 at 16:05
  • Sorry JdeBP but your explicit contradiction is not clear to me and these commands still work on the platforms that I've tested them. Either me or you are misunderstanding the other. Really sincere question: Have you done any testing and found that my commands fail and if yes which command and on what platform(s)? – ndemou Jan 08 '19 at 16:44
  • Please read the first ten words of your answer. – JdeBP Jan 13 '19 at 13:30
  • :-D yes, I was concentrating on the alternatives and you on the shutdown command options. Do you know if this nosh toolset is popular on some subset of the *nix world. I didn't know about it but I'm only regularly in contact with Ubuntu/Debian/CentOS. – ndemou Jan 14 '19 at 10:48
8

It will shutdown your system at 12:00 :

$ sudo shutdown -h 12:00

Options:

-h, -P, --poweroff

Power-off the machine.

-r, --reboot

Reboot the machine.

-c

Cancel a pending shutdown.

0

This is how I solved it (change DATE as you need it):

DATE="2022-11-19 13:18" && crontab -l | { cat; echo "${DATE:14:2} ${DATE:11:2} ${DATE:8:2} ${DATE:5:2} * [[ \$(date +\%Y) -eq ${DATE:0:4} ]] && $(which shutdown) now"; } | crontab -

This adds a new entry to the crontab, which can be checked as follows:

crontab -l | grep shutdown
18 13 19 11 * [[ $(date +\%Y) -eq 2022 ]] && /sbin/shutdown now

18 13 19 11 means that it gets executed at the 19th of november every year at 13:18, but as the crontab contains a condition, it is only executed in the year 2022.

Delete all scheduled shutdown entries:

crontab -l | sed -E '/.*&&.*shutdown/d' | sed '${/^$/d}' | crontab -
mgutt
  • 467
-2

If you want to shutdown you machine immediately you can type in terminal

$ shutdown -P -h now

With that "now" argument. Computer don't don't halt anyway.

samnodier
  • 101
  • 1
    Welcome to the site. I think you may have misinterpreted the OP's intention to specify an "absolute" date in the future at which the computer is to shut down. – AdminBee Jan 27 '20 at 13:52