Python脚本用于定时关闭网易云音乐PC客户端

简介:

本文主要讲述如何使用Python在指定的秒数后关闭Windows上运行的程序(此程序以网易云音乐为例)。本文的背景是昨晚发现网易云音乐的PC客户端没有定时关闭的功能,可以使用Python编写一个简单的脚本,用于定时关闭这样的计划任务。经过改良后,可以对此做一些有用的扩展,用于日常运维中。

为什么使用Python来做这件事?

用cmd、计划任务或者批处理做这件事不可以吗?如果说忽略过程,只看结果的话,这些方式确实可能更简单也能达到目的,但是通过Python来做可以从过程和结果两个方面获得很多好处:

  1. 可以拿来练手,熟能生巧,而且很多东西不用就忘记

  2. 控制程序的行为上更加灵活,想输出什么就输出什么,想扩展功能可以扩展功能,如写入日志等

  3. 移植性和复用性比较好,还可以用到Linux和Mac OSX

脚本运行原理:

1.使用python内置模块sched实现计划任务

2.使用psutil模块实现枚举和kill进程

3.使用thread模块并行执行两个阻塞任务

此脚本涉及的知识:

  1. 获取系统语言默认编码

  2. 枚举和kill 进程

  3. 获取当前用户的用户名

  4. 实现倒计时功能

  5. 实现计划任务功能

  6. Python多线程执行

运行环境与使用方法:

  1. Python 2.7

  2. psutil

  3. 使用python直接运行此脚本即可

  4. 其中,在脚本的__main__中可以修改时间(多少秒后执行)和进程的名字

运行结果截图:

这是设置的10s后关闭网易云音乐的运行截图

wKiom1jnLsPB27zQAAJY8Y_udxY871.jpg

说明:

第一行显示当前运行时的时间;

第二行会实时显示当前时间和剩余的小时、分钟和秒数;

第3、4、5、6行表示查到到进程并杀死进程;

最后两行打印结束时的时间和退出信息;

脚本内容:

脚本可以从GitHub上的LinuxBashShellScriptForOps项目中获取,并获得更新和错误修正版本。

https://github.com/DingGuodong/LinuxBashShellScriptForOps/blob/master/projects/WindowsSystemOps/System/pyScheduleTask.py

脚本内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/python
# encoding: utf-8
# -*- coding: utf8 -*-
"""
Created by PyCharm.
File:               LinuxBashShellScriptForOps:pyScheduleTask.py
User:               Guodong
Create Date:        2017/4/6
Create Time:        22:33
  """
# https://docs.python.org/2/library/sched.html
import  threading
import  sched
import  time
import  sys
import  locale
import  codecs
 
 
def  get_system_encoding():
     """
     The encoding of the default system locale but falls back to the given
     fallback encoding if the encoding is unsupported by python or could
     not be determined.  See tickets #10335 and #5846
     """
     try :
         encoding  =  locale.getdefaultlocale()[ 1 or  'ascii'
         codecs.lookup(encoding)
     except  LookupError:
         encoding  =  'ascii'
     return  encoding
 
 
DEFAULT_LOCALE_ENCODING  =  get_system_encoding()
 
 
def  shutdown_NetEaseCloudMusic(name):
     # define NetEaseCloudMusic process name
     ProcessNameToKill  =  name
 
     print
     import  psutil
     import  sys
 
     # learn from getpass.getuser()
     def  getuser():
         """Get the username from the environment or password database.
 
         First try various environment variables, then the password
         database.  This works on Windows as long as USERNAME is set.
 
         """
 
         import  os
 
         for  username  in  ( 'LOGNAME' 'USER' 'LNAME' 'USERNAME' ):
             user  =  os.environ.get(username)
             if  user:
                 return  user
 
     currentUserName  =  getuser()
 
     if  ProcessNameToKill  in  [x.name()  for  in  psutil.process_iter()]:
         print  "[I] Process \"%s\" is found!"  %  ProcessNameToKill
     else :
         print  "[E] Process \"%s\" is NOT running!"  %  ProcessNameToKill
         sys.exit( 1 )
 
     for  process  in  psutil.process_iter():
         if  process.name()  = =  ProcessNameToKill:
             try :
                 # root user can only kill its process, but can NOT kill other users process
                 if  process.username().endswith(currentUserName):
                     process.kill()
                     print  "[I] Process \"%s(pid=%s)\" is killed successfully!"  %  (process.name(), process.pid)
             except  Exception as e:
                 print  e
 
 
def  display_countdown(sec):
     def  countdown(secs):
         """
         blocking process 1
         :param secs: seconds, int
         :return:
         """
         current_time  =  time.strftime( "%Y-%m-%d %H:%M:%S %Z" ).decode(DEFAULT_LOCALE_ENCODING).encode( "utf-8" )
         print  "Time current: %s"  %  current_time
         while  secs:
             now  =  time.strftime( "%Y-%m-%d %H:%M:%S %Z" ).decode(DEFAULT_LOCALE_ENCODING).encode( "utf-8" )
             hours, seconds  =  divmod (secs,  3600 )
             minutes, seconds  =  divmod (seconds,  60 )
             clock_format  =  '{:02d}:{:02d}:{:02d}' . format (hours, minutes, seconds)
             sys.stdout.write( '\rTime now: %s Time left: %s'  %  (now, clock_format))
             sys.stdout.flush()
             time.sleep( 1 )
             secs  - =  1
 
     # set a human readable timer here, such as display how much time left to shutdown
     countdown( int (sec))
 
 
def  display_scheduler(name):
     """
     blocking process 2
     :return:
     """
     =  sched.scheduler(time.time, time.sleep)
     s.enter( 10 1 , shutdown_NetEaseCloudMusic, (name,))
     s.run()
     now  =  time.strftime( "%Y-%m-%d %H:%M:%S %Z" ).decode(DEFAULT_LOCALE_ENCODING).encode( "utf-8" )
     print  "Time finished: %s\nGood bye!"  %  now
 
 
if  __name__  = =  '__main__' :
     seconds_to_shutdown  =  10
     process_name_to_shutdown  =  "cloudmusic.exe"
 
     threadingPool  =  list ()
     threading_1  =  threading.Thread(target = display_countdown, args = (seconds_to_shutdown,))
     threading_2  =  threading.Thread(target = display_scheduler, args = (process_name_to_shutdown,))
     threadingPool.append(threading_1)
     threadingPool.append(threading_2)
 
     for  thread  in  threadingPool:
         thread.setDaemon( False )
         thread.start()
 
     thread.join()

tag: python计划任务,python定时任务,python sched

--end--




本文转自 urey_pp 51CTO博客,原文链接:http://blog.51cto.com/dgd2010/1913818,如需转载请自行联系原作者



相关文章
|
21天前
|
Linux Shell Python
Linux执行Python脚本
Linux执行Python脚本
26 1
|
2月前
|
Web App开发 数据采集 自然语言处理
python脚本抢各大平台大额优惠卷
python脚本抢各大平台大额优惠卷
48 0
|
10天前
|
JSON 测试技术 持续交付
自动化测试与脚本编写:Python实践指南
【4月更文挑战第9天】本文探讨了Python在自动化测试中的应用,强调其作为热门选择的原因。Python拥有丰富的测试框架(如unittest、pytest、nose)以支持自动化测试,简化测试用例的编写与维护。示例展示了使用unittest进行单元测试的基本步骤。此外,Python还适用于集成测试、系统测试等,提供模拟外部系统行为的工具。在脚本编写实践中,Python的灵活语法和强大库(如os、shutil、sqlite3、json)助力执行复杂测试任务。同时,Python支持并发、分布式执行及与Jenkins、Travis CI等持续集成工具的集成,提升测试效率和质量。
|
17天前
|
存储 监控 异构计算
【Python】GPU内存监控脚本
【Python】GPU内存监控脚本
|
17天前
|
Ubuntu Unix Linux
【Linux/Ubuntu】Linux/Ubuntu运行python脚本
【Linux/Ubuntu】Linux/Ubuntu运行python脚本
|
25天前
|
XML Shell Linux
性能工具之 JMeter 使用 Python 脚本快速执行
性能工具之 JMeter 使用 Python 脚本快速执行
40 1
性能工具之 JMeter 使用 Python 脚本快速执行
|
1月前
|
数据采集 测试技术 Python
Python自动化脚本的魅力与实践
Python自动化脚本的魅力与实践
48 0
|
1月前
|
数据安全/隐私保护 Python
使用Python脚本实现图片合成PDF功能
使用Python脚本实现图片合成PDF功能
28 0
|
1月前
|
安全 数据安全/隐私保护 开发者
如何使用Pyarmor保护你的Python脚本
如何使用Pyarmor保护你的Python脚本
42 0
|
1月前
|
数据处理 Python
Python自动化脚本
Python自动化脚本
24 0