This article mainly introduces three methods for configuring a python script for Windows to start up. This article describes how to implement a Start menu, a boot script, and how to call the script through a service, for more information, see test environment: windows Server 2003 R2.
I. implementation of Start Menu startup items
You must log on to the console to perform this operation.
Test script (python code ):
The code is as follows:
Import time
Fout = open ('E: \ 1.txt ', 'w ')
While True:
Tmp = '% d-% 02d-% 02d % 02d: % 02d: % 02d \ r \ n' % time. localtime () [0: 6]
Print tmp
Fout. write (tmp)
Fout. flush ()
Time. sleep (5)
1. general operations
1.1 create a shortcut;
1.2 add the created shortcut to the Start menu;
1.3 boot verification;
2. hide the startup of the command line window
The above operation method has a command line window, which is not practical in some cases. we can remove the command line window in the following two ways.
2.1 Change the python script file extension to ". pyw"
Other operations are similar to the above process and will not be described here.
2.2 start with a script such as vbs
The vbs code is as follows:
Set ws = CreateObject ("Wscript. Shell ")
Ws. run "cmd/c E: \ test1.py", vbhide
The path is configured according to the actual situation. other operations are similar.
If you do not want to use shortcuts, you can directly add the script to the startup item.
Appendix:
Configure automatic account logon
To enable the Start menu, you must ensure that you log on to the system. here is a method for automatic account logon.
A. type "Rundll32 netplwiz. dll, UsersRunDll" in the running box ";
B. open the user account interface and remove the check box before "to use the local machine, the user must enter the user name and password". press "OK" and enter the user name and password for automatic logon;
II. boot script
Cycle is not supported. it is best to configure the timeout time.
Test Code (python ):
The code is as follows:
Import time
Fout = open ('E: \ 1.txt ', 'w ')
Tmp = '% d-% 02d-% 02d % 02d: % 02d: % 02d \ r \ n' % time. localtime () [0: 6]
Print tmp
Fout. write (tmp)
Fout. close ()
The procedure is as follows:
A. enter gpedit. msc in the running state to open the group policy editor;
B. select "computer configuration" => "Windows settings" => "script" => "start;
C. select a script;
D. configure the maximum waiting time for the script, path: "Computer Configuration" => "management template" => "system" => "script" => "maximum waiting time for group policy scripts ";
3. call the script through a service
A. write a script to start serviceStartShell. the code is as follows (only the code of main function is listed here ):
The code is as follows:
Int main (int argc, char * argv [])
{
Init ();
DwThreadID = GetCurrentThreadId ();
SERVICE_TABLE_ENTRY st [] =
{
{SzServiceName, (LPSERVICE_MAIN_FUNCTION) ServiceMain },
{NULL, NULL}
};
// Printf ("argc = % d \ n", argc );
If (4 = argc) & 0 = stricmp (argv [3], "/install "))
{
Install (argv [1], argv [2]);
WriteReg (argv [1], argv [2]);
}
Else if (2 = argc) & 0 = stricmp (argv [1], "/uninstall "))
{
Uninstall ();
}
Else
{
If (! StartServiceCtrlDispatcher (st ))
{
// Printf ("Register Service Main Function Error! ");
}
}
Return 0;
}
B. service installation;
The code is as follows:
ServiceStartShell.exe C: \ Python27 \ python.exe e: \ test1.py/install
C. uninstall the service;
The code is as follows:
ServiceStartShell.exe/uninstall