Running a function or BASH command after exiting npyscreen

132 Views Asked by At

I'm trying to run a function after entering in npyscreen, tried a few things and am still stuck. Just exits npyscreen and returns to a bash screen. This function is supposed to start a watchdog/rsync watch-folder waiting for files to backup.

#!/usr/bin/env python
# encoding: utf-8
import npyscreen as np
from nextscript import want_to_run_this_function

class WuTangClan(np.NPSAppManaged):
    def onStart(self):
         self.addForm('MAIN', FormMc, name="36 Chambers")

class FormMc(np.ActionFormExpandedV2):
     def create(self):
         self.rza_gfk = self.add(np.TitleSelectOne, max_height=4, name="Better MC:", value=[0], values=["RZA", "GhostFace Killah"], scroll_exit=True)

     def after_editing(self):
        if self.rza_gfk.value == [0]:
            want_to_run_this_function()
            self.parentApp.setNextForm(None)
        else:
            self.parentApp.setNextForm(None)

if __name__ == "__main__":
    App = WuTangClan()
    App.run()
1

There are 1 best solutions below

0
xhenrique On

I not sure if i understood correctly what you want.

For executing any kind of bash command i like to use subprocess module, he has the Popen constructor, which you can use to run anything from a bash.

e.g, on windows

import subprocess
process = subprocess.Popen(['ipconfig','/all'])

On unix like system:

import subprocess
process = subprocess.Popen(['ip','a'])

If you have a ".py" file you can pass the parameters like if you where running it from the terminal

e.g

import subprocess
process = subprocess.Popen(['python3','sleeper.py'])

You can even retrieve the process pid and kill it whenever you want, you can look at subprocess module documentation here