开发者社区> 技术小胖子> 正文

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()[1or '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(101, 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,如需转载请自行联系原作者



版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
《Python 脚本速查手册》电子版地址
Python 不仅是运维工程师的利器,也是众多运维工具的好伙伴,但现在现实生活中很多开发者却在语法编写上犯了难。本书详解了运维工程师如何用Python打造工具,在有限的时间内写出优雅与实用兼并的脚本。通过阅读本书,读者可以学习到Click语法速查、Python常用运维模块、Python运维脚本案例。掌握了这些运用Python开发的技能,开发者们可以在更短的实践内更快速的使用Python处理事物。
13 0
pm2执行python脚本
pm2执行python脚本
18 0
使用Pycharm设置python脚本文件头
使用Pycharm设置python脚本文件头:位置、效果参考、模板预定义变量参考。。。
28 0
Python千万级字典快速去重脚本
Python千万级字典快速去重脚本
17 0
【python脚本】ICer的脚本入门训练——find_key
【python脚本】ICer的脚本入门训练——find_key
30 0
【python脚本】ICer的脚本入门训练——gen_tc
【python脚本】ICer的脚本入门训练——gen_tc
21 0
【python脚本】ICer的脚本入门训练——svn_back
【python脚本】ICer的脚本入门训练——svn_back
20 0
【python脚本】word批注批量提取器V2实用版
【python脚本】word批注批量提取器V2实用版
36 0
【python脚本】word批注状态批量提取器V1版本
【python脚本】word批注状态批量提取器V1版本
15 0
Python 用一个脚本来控制另一个脚本的运行、关闭
Python 用一个脚本来控制另一个脚本的运行、关闭
36 0
+关注
技术小胖子
文章
问答
视频
文章排行榜
最热
最新
相关电子书
更多
给运维工程师的Python实战课
立即下载
Python 脚本速查手册
立即下载
ACE 区域技术发展峰会:Flink Python Table API入门及实践
立即下载