import
threading
import
sched
import
time
import
sys
import
locale
import
codecs
def
get_system_encoding():
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):
ProcessNameToKill
=
name
print
import
psutil
import
sys
def
getuser():
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
x
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
:
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):
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
countdown(
int
(sec))
def
display_scheduler(name):
s
=
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()