Windows系统配置python脚本开机启动的3种方法分享
测试环境:windows Server 2003 R2
一、开始菜单启动项实现
用户必须登录才可执行。
测试脚本(python代码):
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、常规操作
1.1 创建快捷方式;
1.2 将创建的快捷方式放入开始菜单启动项;
1.3、开机验证;
2、隐藏命令行窗口启动
上述操作方法有命令行窗口,有些场合感觉不太实用,我们可以通过以下两种方式去掉命令行窗口。
2.1 将python脚本的文件扩展名改为".pyw"
其它操作和上述过程类似,这里不再赘述。
2.2 通过vbs之类的脚本启动
vbs代码如下:
Set ws = CreateObject("Wscript.Shell")
ws.run "cmd /c E:\test1.py",vbhide
路径根据具体情况进行配置,其它的和之前的操作类似,这里不再赘述。
如果不想用快捷方式的话,把脚本直接放入启动项也可以。
附:
配置账户自动登录
通过开始菜单启动项来实现的话,必须进行相应保证用户登录系统,这里介绍一种账户自动登录的方法。
a、 在运行框中键入“Rundll32 netplwiz.dll,UsersRunDll”;
b、 打开用户账户界面,将“要使用本机,用户必须输入用户名和密码”前面的勾去掉,按”确定“后输入需要自动登录的用户名和密码;
二、开机脚本
不能用循环,最好配置超时时间。
测试代码(python):
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()
步骤如下:
a、运行中输入gpedit.msc打开组策略编辑器;
b、选择“计算机配置”=>“Windows 设置”=>“脚本”=>“启动”选项;
c、选择脚本;
d、配置脚本最长等待时间,路径为“计算机配置”=>“管理模版”=>“系统”=>“脚本”=>“组策略脚本的最长等待时间”;
三、通过一个服务调用该脚本
a、编写脚本启动服务serviceStartShell,代码如下(这里只列出main函数的代码):
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、服务安装;
serviceStartShell.exe C:\Python27\python.exe e:\test1.py /install
c、服务卸载;
serviceStartShell.exe /uninstall

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Summary of some reasons why crontab scheduled tasks are not executed. Update time: January 9, 2019 09:34:57 Author: Hope on the field. This article mainly summarizes and introduces to you some reasons why crontab scheduled tasks are not executed. For everyone Solutions are given for each of the possible triggers, which have certain reference and learning value for colleagues who encounter this problem. Students in need can follow the editor to learn together. Preface: I have encountered some problems at work recently. The crontab scheduled task was not executed. Later, when I searched on the Internet, I found that the Internet mainly mentioned these five incentives: 1. The crond service is not started. Crontab is not a function of the Linux kernel, but relies on a cron.

PyCharm is a powerful Python integrated development environment that provides a wealth of functions and tools to help developers improve efficiency. Among them, PyInstaller is a commonly used tool that can package Python code into an executable file (EXE format) to facilitate running on machines without a Python environment. In this article, we will introduce how to use PyInstaller in PyCharm to package Python code into EXE format, and provide specific

Orange3 is a powerful open source data visualization and machine learning tool. It has rich data processing, analysis and modeling functions, providing users with simple and fast data mining and machine learning solutions. This article will briefly introduce the basic functions and usage of Orange3, and combine it with actual application scenarios and Python code cases to help readers better master the usage skills of Orange3. The basic functions of Orange3 include data loading, data preprocessing, feature selection, model establishment and evaluation, etc. Users can use the intuitive interface to drag and drop components to easily build data processes. At the same time, more complex data processing and modeling tasks can also be completed through Python scripts. Below we will go through a practical

How to set Win11 to automatically open the browser when booting? In fact, the method is very simple. Users can directly open the shell:startup under the run window, and then quickly open the startup folder. In the startup folder, you can put some programs that need to be started and run. Let this site carefully introduce to users how to automatically start the browser when Win11 is turned on. How to automatically start the browser when Win11 starts up: 1. Press the [win+R] shortcut key to open the run window, then enter [shell:startup] and press Enter to confirm. 2. This will quickly open the startup folder. Some programs that need to be started can be placed in the startup folder. 3. For example, we will put Edge on the desktop

1. First open pycharm and enter the pycharm homepage. 2. Then create a new python script, right-click - click new - click pythonfile. 3. Enter a string, code: s="-". 4. Then you need to repeat the symbols in the string 20 times, code: s1=s*20. 5. Enter the print output code, code: print(s1). 6. Finally run the script and you will see our return value at the bottom: - repeated 20 times.

How to read Excel data using PyCharm? The steps are as follows: install the openpyxl library; import the openpyxl library; load the Excel workbook; access a specific worksheet; access cells in the worksheet; traverse rows and columns.

Flask installation and configuration tutorial: A tool to easily build Python Web applications, specific code examples are required. Introduction: With the increasing popularity of Python, Web development has become one of the necessary skills for Python programmers. To carry out web development in Python, we need to choose a suitable web framework. Among the many Python Web frameworks, Flask is a simple, easy-to-use and flexible framework that is favored by developers. This article will introduce the installation of Flask framework,

Website subdomain query tools include: 1. Whois Lookup: can query the registration information of a domain name, including subdomain names; 2. Sublist3r: can automatically scan the subdomain name of a domain name with the help of search engines and other tools; 3. DNSdumpster: can query Information such as the subdomain name, IP address and DNS record of the domain name; 4. Fierce: You can query the subdomain name information of the domain name through the DNS server: 5. Nmap; 6. Recon-ng; 7. Google Hacking.
