Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a script on Windows startup without a user logged on

This is a Windows 2003 (or later) machine that gets switched on every morning, but no one logs on until some hours later.

I want to use the time in between to run a backup script c:\script\backup.cmd

How can I start this unattended after the machine has come up?

I tried 2 registry keys, but this resulted in the script being run after a user logs on (which is too late):

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunServices

In the end I used Windows TaskScheduler, who has such an option, but I was wondering if there is another possibility?

like image 607
Karl Thorwald Avatar asked Mar 05 '09 13:03

Karl Thorwald


People also ask

How do I run a program without logging in?

To allow it to run without a user session, you need a windows service. That should handle all the background stuff. You can then register the service and set it to start when the system starts. To allow it to have a UI, and show up in the notification area, you need a windows application.

How do I get a Windows script to run on startup?

On Windows, the simplest way of running a program at startup is to place an executable file in the Startup folder. All the programs that are in this folder will be executed automatically when the computer opens. You can open this folder more easily by pressing WINDOWS KEY + R and then copying this text shell:startup .

What account do startup scripts run as?

Group Policy supports four main types of scripts: computer startup, computer shutdown, user logon, and user logoff. The computer startup and shutdown scripts execute under the local system account; user logon and logoff scripts run as the current user account.


2 Answers

Short answer: GPEDIT.MSC (Start, Run, GPEdit.msc)

Windows 2000 and above [1] offer a computer Startup Scripts collection in the policy editor:

  • Computer Settings -> Windows Settings -> Scripts (Startup/Shutdown)

There's an equivalent logon script area (i.e. after computer startup, when a user logs on) in the User configuration bit.

Longer:

GPEDIT.MSC is the Group Policy editing console, and runs against the local computer's Local Group Policy store when it's used directly, so it's useful for setting local-only parameters. When using Active Directory, a similar interface is used to edit AD Forest-hosted group policy objects (GPOs), so the same settings are available across a bunch of machines.

The computer startup scripts run in the computer context, i.e. as LocalSystem, as you noted, so they often can't access network drives which require a certain user or group membership to work. When domain-member computers access network resources, they generally (with exceptions) authenticate using their MACHINENAME$ account - which lets you set Share/NTFS permissions to allow a computer to access those resources.

A startup script is a quick and easy way of getting a process running when the machine boots.

The computer startup process will be affected by the time it takes to run the program, though, so you might want to ensure you call it with the START command from a batch file, or specifying not to wait for the executable to complete in whatever script language you use. (the key point there is: run the script asynchronously unless it's critical, or doesn't need to be run asynchronously cos it will always take no time at all. Long boots = unhappy users).

Using a Win32 Service is an alternative option - you can use the SRVANY utility from the Resource Kit to "service-ify" pretty much any executable. VS.Net 2002 and later also let you build a managed service directly.

And Task Scheduler gets much more capable as of Vista/2008, able to run scripts at startup, on idle, and/or when Event Logs are generated or certain other conditions are met: it's pretty cool! Scheduled Tasks has the possible advantage of being able to specify the user account under which the task runs, if that's important to you.

Caveat Scriptor: http://support.microsoft.com/kb/256320

Run Startup Scripts Asynchronously: http://msdn.microsoft.com/en-us/library/ms811602.aspx

Vista Task Scheduler (what's new): http://technet.microsoft.com/en-us/appcompat/aa906020.aspx

[1] Windows XP, 2003, Vista/2008, Windows 7/2008R2, Windows 8/2012, Windows 8.1/2012R2, Windows 10/Windows Server 2016. Everything. But NT4 didn't!

like image 191
TristanK Avatar answered Sep 21 '22 14:09

TristanK


You have already outlined a good solution:

Setup a scheduled task to run at Start Up and allow the job to run when the user isn't logged on.

like image 33
GateKiller Avatar answered Sep 17 '22 14:09

GateKiller