-1

The first simple answer to the Linux equivalent killall(ProgramName)

The program is to be a toggle program. I can launch Firefox/program. def tolaunch(program): os.system("firefox")

When launched I wish to save the name of program launched, in array(simple to do), then launch the program(Firefox)/program.

Here the idea off the toggle come’s in, when Launched and I have finished my browsing, I wish to call the same program(mine), check the array for said program and exit Firefox/program, by running a simple command like killall(“Firefox”) but in Python code. I don’t want to write a long winded command/script, that first has to workout the ‘pid’.

I seem very close to the answer but cant find it.

Edit: people asked for code example | here is my code

# Toggler for macro key -|- many keys = one toggler program
import os
import sys


def startp(program):
    # hard-coded
    os.system("firefox")


def exitp(program):
    # os.close(program)
      sys.exit(program)


# Begin
# hard-coded : program start
startp("Firefox")
# loop while program/s active
# exitp("Firefox")
# exit(program)   or program end

I tried to include some explanations in way off comments

Data
  • 113
  • 2
  • 11
  • Show your current code, and what exactly you want to do. (note that `os.system` is blocking, so it isn't entirely clear where you're going to put the code) – user202729 Jan 11 '21 at 01:32

1 Answers1

0

What you are looking for is the sys.exit() command.

There are 4 functions that exit a program. These are the quit(), exit(), sys.exit() and os._exit(). They have almost same functionality thanks to the fact that they raise the SystemExit exception by which the Python interpreter exits and no stack traceback is printed on the screen.

Among the above four exit functions, sys.exit() is mostly preferred because the exit() and quit() functions cannot be used in production code while os._exit() is for special cases only when an immediate exit is required.

To demonstrate this behavior using the sys.exit() function:

import sys

i=0
while i in range(10):
    i += 1
    print(i)
    if i == 5:
        print('I will now exit')
        sys.exit()
    elif i > 5:
        print('I will not run, program alredy exited :(')
Skeptic
  • 1,254
  • 14
  • 18
  • I have two function a startp and an exitp I don't want to stop my program(toggler)I wish to kill/exit the programs it starts. like os.system(program) I then want it to continue until I call exitp(program) the program(toggler) would continue running in the back ground until told to quit. Iam looking at how to send it(toggler) msgs Skeptic – Data Jan 11 '21 at 17:14