It seems I can shutdown using sudo shutdown by specifying a time or minutes.
Is there a way to specify datetime for shutdown?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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:
-
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
-
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
-
You can also pass
atcommands from STDIN:$ at now + 1 min warning: commands will be executed using /bin/sh at> ls
Then, press CtrlD to exit the
atshell. Thelscommand 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:
-
Create a new crontab by running
crontab -e. This will bring up a window of your favorite text editor. -
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
- 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.
Method 2
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 don’t mind using you calculator and want to shutdown in say 24hours (24*60=1440 minutes) and you’re absolutely sure the system will not reboot in between:
shutdown -r +1440
Method 3
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.
Method 4
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 -
Method 5
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0