0

Here is my problem: a user needs to log in via remotedesktop to a Windows server during given periods of the day. I have a working bit of code however I believe threads are never closed correctly since after a given time the program is Stopped.

I would like to close this thread started by AP Scheduler, can someone tell me how I would do this properly? I have tried joining the thread and exiting as well as ._Exit() but neither work (or really should work) I am lost.

import sys
import os
import subprocess
import signal
import time
from apscheduler.scheduler import Scheduler
from pykeyboard import PyKeyboard
from threading import Thread

def rdp_start():
    os.system('rdesktop -d domain -u username -p password -g 1600x1050 -a 16 123.123.123.123')

def rdp_check():
    p = subprocess.Popen(['ps', '-A'], stdout=subprocess.PIPE)
    out, err = p.communicate()
    for line in out.splitlines():
        if 'rdesktop' in str(line):
            print("Rdesktop is running!")
    else:
        print("Starting rdesktop!")
        rdp_job = Thread(target = rdp_start, args = ())
        rdp_job.start()
        time.sleep(5)
        k = PyKeyboard()
        k.tap_key(k.enter_key)
        #time.sleep(600)
        #Where I would like to kill rdp_job, and remove rdp_kill scheduling

def rdp_kill():
    p = subprocess.Popen(['ps', '-A'], stdout=subprocess.PIPE)
    out, err = p.communicate()
    for line in out.splitlines():
        if 'rdesktop' in str(line):
            pid = int(line.split(None, 1)[0])
            os.kill(pid, signal.SIGKILL)
    print("Killed RDP")

def idle():
    # Stop from sleepin
    k = PyKeyboard()
    k.tap_key(k.scroll_lock_key)
    k.tap_key(k.scroll_lock_key)

sched = Scheduler()
sched.daemonic = False
sched.start()

# Fix screen issues with PyUserInput
os.system('xhost + > /etc/null')

sched.add_cron_job(rdp_check, hour=15)
sched.add_cron_job(rdp_kill, hour=15, minute=8)
sched.add_cron_job(rdp_check, hour=23)
sched.add_cron_job(rdp_kill, hour=23, minute=8)
sched.add_cron_job(rdp_check, hour=7)
sched.add_cron_job(rdp_kill, hour=7, minute=8)
sched.add_cron_job(idle, second='*/60')

I know that killing threads is generally bad practice, however I really need this program to run for any given amount of time, can anyone point me in the right direction?

sunshinekitty
  • 2,307
  • 6
  • 19
  • 31

1 Answers1

0

If you're on Linux, consider the following changes:

1) instead of using Thread, just run the rdesktop command in the background:

os.system('rdesktop ... &')

2) the killall command finds running programs and optionally sends them a signal.

To see if a rdesktop command is running, send it signal #0. It'll return status 0 if it found something, or status > 0 if no such process exists:

$ killall -0 sleep
$ echo $?
0
$ killall -0 beer
beer: no process found

3) to kill rdesktop:

os.system('killall rdesktop')

Note the above assumes that you have at most one rdesktop process running, and that you started it therefore you can probe it with killall -0.

johntellsall
  • 14,394
  • 4
  • 46
  • 40