Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Batch file to reboot PC only if no one is using it

Status
Not open for further replies.

DayLaborer

Programmer
Jan 3, 2006
347
US
This question pertains to my Windows XP Pro PC that has "Fast User Switching" enabled.

I would like to schedule a task to execute a batch file that would reboot my PC only if:
- there are no users with programs running at the time (even if they're suspended)
and
- no one is "active" (i.e. actively logged-in)

In other words, if there are users who are logged in ("Disconnected" - i.e. not "active") but do not have any programs running, I want to reboot. But if any users are either actively logged-in or are "Disconnected" but have programs running, I don't want anything to happen.

What would the contents of such a batch file be?

Thanks so much,
Eliezer
 
Can you use the available "settings" in Scheduled Tasks and schedule the Shutdown.exe with the /r switch?

Specifies whether the scheduled task is started only after you have not used your keyboard or mouse for a specified period of time. If the task is scheduled to repeat, the first run of the task occurs only if the computer has been idle for the specified period of time.
If the computer is not idle when the task first tries to start, Task Scheduler keeps checking to see if the computer is idle for the amount of time specified in "Only start the task if the computer has been idle for at least".

If the computer does not become idle during this time, no occurrences of the task run.

"To Schedule a new task" is a Help and Support Center article.

Set up XP to turn off
thread779-606975
 
Linney,

Nope, unfortunately I really need to base the reboot exclusively on
- no one being actively logged in
and
- no one having any programs running

Eliezer
 
The environment variable %username% - is it filled in when no one is actively online?

Running programs would be a harder one...
 
Your term and definition of "Actively logged in" may need further explaining. Do you mean a point in time when all users have logged off from the machine? Or do you mean when users are logged in but not actually using the machine?
 
Linney,

I'll try to be clearer - but apparently I'm not doing too well so far. :-(

I do not want the reboot to happen if:
- a user is using the computer - i.e. logged in / can see their Desktop / can click around and do stuff
or if
- any users have any programs running but are "disconnected" - like can be seen in this screenshot.

Thanks!
Eliezer
 
Glen, how can I check that?

it's an environment variable which can be referred to like I described. For your batch file, you can do something like:

Code:
echo %username% > whichuser.txt
 
I'm not aware of a way to do it (without writing a program myself), but let me ask a different question: Why do you need to reboot the system when it's not in use? Is it to fix some sort of problem? If so, maybe we can find a better solution than just rebooting...
 
I keep coming back to my original suggestion of incorporating a batch file with the Shutdown /r switch.

Have you tried that, because Task Scheduler should realize that the machine is not idle, if processes from any logged on user (Fast Switching user too), or XP, are running and also if the Keyboard or Mouse have been used during the necessary set period length of idle time before any task can run.
 
after creating a scheduled shutdown task in Task Scheduler, i found the options to run when the pc is idle are easy to configure - if you can get the job done more easily, or with more difficulty, which do you prefer?
 
CPJust,

Good question. The issue is that after I run a full backup (overnight) with Acronis, the computer is very sluggish. I wanted to reboot it before morning, so that way it will be all ready to go for anyone using it in the morning.

In any case, though, I will try the scheduled task and report back.

Thanks very much!
Eliezer
 
I did try scheduling a task. The problem is that although I set it to "Only run when idle", it still reboots if I have "disconnected" users with programs "running."

So unfortunately, that didn't work...

Is there really no way to do this with a "one-liner" batch file?...

Thanks (again!),
Eliezer
 
Is there anything mentioned in the Schedule Task logfile, that might indicate any failure of the task? See SchedLgU.txt in the Windows folder.

I wish I knew of a magic one line batch file that would solve your problem, but I don't.

Have you asked in the VBscript Forum?

 
Thank you, Linney, I just posted it in the VBS forum at thread329-1385047
Eliezer
 
Let me try a simpler question: can I have the batch file reboot the PC only if no one is logged-in - regardless of whether the users are "active" or "disconnected", but if any user is logged-in, it won't reboot?

Thanks!
Eliezer
 
You could check for the "explorer.exe" in the list of running processes.
As far as remember, but you would need to verify it, if there is a user logged on explorer.exe will be in the process list.

On XP you could use something like "tasklist| findstr /I explorer.exe" and some control magic...

G.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top