Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

Is there a way to specify datetime for shutdown?

share|improve this question

3 Answers 3

up vote 34 down vote accepted

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 -g 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.

share|improve this answer
1  
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 at 2:12
1  
@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 at 2: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 at 2:39
2  
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 Beam Mar 20 at 2:57
1  
This is the linux/unix answer. It is unfortunate that the "install this gtk application" was chosen. –  dfc Mar 20 at 16:31

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.

share|improve this answer
    
Your method not showing how to shutdown on a specific date... –  oscar Mar 26 at 1:21

If you're running a Debian system, you will find Gshutdown in the repositories.

In GShutdown you can select from three ways to turn off your PC:

  1. At a specified time AND date
  2. After a set delay
  3. Now

If you simply want to restart or end the session, you can also do that from the bottom drop-down menu. Select the date from the calendar, set the time and press start. Everything should work just fine, but if something is acting up, you can go in the Preferences and manually specify the desktop environment and display manager that you're currently using (if GShutdown fails to detect them) or type in a command to perform before the shutdown procedure begins. Moreover, you can set GShutdown to warn you a few minutes before executing the action, just in case you forget.

This link gives more information on Gshutdown. However,this one might not be applicable if you are planning to shutdown from terminal.

share|improve this answer
1  
Debian has no package called gshutdown. It seems to be a GUI program, whereas the question asked for a command line method. –  Gilles Mar 20 at 21:20
    
I had clearly mentioned that this method is not applicable if planning to shutdown from terminal. Yesterday, @terdon's answer was only accepted. Today, I see my answer was accepted. I do not know why the OP chose to accept this answer. I can delete the answer if you want :) –  Ramesh Mar 20 at 21:22
    
@Ramesh You can't delete your answer if it's been accepted. Mods can, but they're very unlikely to. –  Izkata Mar 21 at 3:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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