Shut down PC at a determined time

1

I have a software (calibre ebook manager) that fetches news at 6 am. I want to do the following :

  1. Wake computer at 5:50 if not already on.(easily done in bios)
  2. A scheduler opens calibre at 5:55 (done)
  3. Calibre fetches the news as it starts up.

The above steps were easy to accomplish, but there are some problems. I want to shutdown the pc IF AND ONLY IF it was started at 5:50 automatically. If , for some reason, the pc was already on, I DO NOT WANT to shut it down.

How should that be done ?

harveyslash

Posted 2014-12-04T08:45:02.197

Reputation: 135

2Hack: when the PC is booted up, write a value via a script, such as the on time. Save this value to a file on your PC. Then, at shutdown time, check the value of this file and act accordingly. – Dave – 2014-12-04T08:47:12.937

Cool idea, wrote a script for it, without the need to save it. :) – Charm_quark – 2014-12-04T09:57:17.707

Answers

2

Here is the AutoHotKey Script that does it. Make sure you run this script on boot. (Add a shortcut in startup folder)

;
; AutoHotkey Version: 1.x
; Language:       English
; Platform:       Win9x/NT
; Author:         AEonAX
;
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
if (A_Hour=5 and A_Min>55 and A_Min<59) ;Check if script starting time between 05:55 and 05:59 (24Hr clock)
{
Sleep,1000*60*5     ;Waits 5 Minutes
msgbox,4,,Will Shutdown in 60 seconds. Do you want to Stop the Shutdown.,60 ;Shows a msgbox for 60 seconds, If no response then shutsdown
ifmsgbox,Yes
    MsgBox Shutdown Cancelled
else
    Shutdown,1
}

AEonAX

Posted 2014-12-04T08:45:02.197

Reputation: 441

3

Since the condition is to reset only if computer was booted automatically at 5:50 i guess one way would be to rely on boot time. You might adjust the minute timer to detect if when BIOS start computer at 5:50 what boot time is seen by system. But this a detail.

For the shutdown and detecting a boot time you can use following powershell script:

# GETTING BOOT TIME
$last_boot_date_time = Get-WmiObject win32_operatingsystem | select @{LABEL='LastBootUpTime';EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}

# SELECTING HOUR:MINUTES:SECONDS PART OF THE DATE
$last_boot_time_only = $last_boot_date_time | foreach { ([String]$_).split(" ")[1] }

# CHECKING IF BOOT TIME EQUALS 05:50
# IF SO - INITIATE SHUTDOWN
if ( $last_boot_time_only.split(":")[0] -eq "05" -and $last_boot_time_only.split(":")[1] -eq "50")
{ 
    Write-host "Shutting down in 10 seconds."; 
    shutdown -s -f -t 10
}

This is assuming you are using Windows with powershell version 1 or 2. If you have higher version of powershell, you might want to replace the command that gets the boot time with Get-CimInstance -ClassName win32_operatingsystem | select lastbootuptime

mnmnc

Posted 2014-12-04T08:45:02.197

Reputation: 3 637

wont the boot time vary a bit with every boot ? – harveyslash – 2014-12-06T13:11:28.367

It might vary a bit, but unless you make some big change how much could it vary really? Couple of seconds at maximum. I think that is minor issue that could be fixed with additional if condition. – mnmnc – 2015-02-10T15:43:38.323

0

Try the below batch script, it rely's on the the system uptime. Run this when you think it is appropriate, with the Scheduler

@echo off
setlocal enabledelayedexpansion

for /f "tokens=2 delims=," %%a in ('systeminfo ^| findstr Boot ^| findstr System') do (
  set output=%%a
  set /a hrs=!output:~1,1!
  set mins=!output:~3,2!
 ::echo !output!
 ::echo !hrs! 
 ::echo !mins!
if !hrs! == 5 (
    if !mins! == 50 (
        shutdown -s -f -t 100
    )
) 

)

Well that is the idea, tweak as required

Explanation.

  1. Extract the time that the computer was switched on,

systeminfo ^| findstr Boot ^| findstr System

systeminfo has a lot of information in there, So we are filtering only the parameter that we need we are looking for. eg,

C:\Users>systeminfo | findstr Boot |findstr System

System BootTime: 12/4/2014, 7:20:37 AM

  1. Select the time field only that is separated by the comma

for /f "tokens=2 delims=," %%a in ('systeminfo ^| findstr Boot ^| findstr System') do ( set output=%%a

and in that entire string only select the 1st character ( 1 space), the 3rd and 4th character (2 space)

set /a hrs=!output:~1,1! set mins=!output:~3,2!

To give only the hr and the minute

  1. IF function to shutdown if the time is a match.

if !hrs! == 5 (

  if !mins! == 50 (

      shutdown -s -f -t 100

PS: The bellow is the comment to see, if there is any mistake in the code,

::echo !output!

::echo !hrs!

::echo !mins!

Charm_quark

Posted 2014-12-04T08:45:02.197

Reputation: 735

If you check only one character at ~38,1 - wont it by a chance trigger at 15:50 as well? Just a thought :) – mnmnc – 2014-12-04T10:32:27.627

oops, you are correct.. let me modify it, thanks – Charm_quark – 2014-12-04T10:48:38.260

I think it would still work, because if the other character comes in place, it would move the enter positioning, and it will not match the if function to initiate shutdown. – Charm_quark – 2014-12-04T11:04:30.793

i am not so much into cmd. could you explain what your script is doing? Maybe add comments ? – harveyslash – 2014-12-04T11:16:36.207

1i have modified it with explanations, hope this helps. Copy and save it in a '.bat' file. ;) – Charm_quark – 2014-12-04T12:16:22.483

@Charm_quark if I see this correctly, your script depends on the date as well in System BootTime: 12/4/2014, 7:20:37 AM. ~38,1 will work when date has particular length, for example 12/4/2014 but I think the length in your command may vary. It can be 1/4/2014 (shorter) and it can be 12/12/2014 (longer) - I think it might cause your script to fail. – mnmnc – 2014-12-04T13:08:27.883

@Charm_quark i think you need nested for with delims set to , – mnmnc – 2014-12-04T13:09:44.100

yup, i see my error, I modified it again ( I used the same logic, that if the time changes to two places it will not be equal). Thanks – Charm_quark – 2014-12-04T16:30:46.383

-1

try nncron for Windows http://www.nncron.ru/help/help.htm English version

Вячеслав Вячеслав

Posted 2014-12-04T08:45:02.197

Reputation: 11

Please expand your answer and explain how to use the software for the specific problem mentioned in the question. – slhck – 2014-12-04T09:09:06.357