Python Forum
Close script from another script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Close script from another script
#3
Yes! This is entirely possible. However, like ndc85430 says, it is dependent on your platform. I can show you how to kill them on both Linux, and Windows. This is achieved through using shell commands, which you can access from the os.system function from the os library.

On Linux, there are two main ways I know of to kill a process, or processes. This is by using the shell commands kill and killall. They both send a signal SIGTERM by default, which essentially asks the process to stop. This also means the process doesn't have to listen to it, so if you need to forcefully kill a process, you can prefix each command with -9 to send a SIGKILL, which will force it to end, but I suggest you don't do this unless it's ABSOLUTELY NECESSARY, as it could cause problems with I/O operations like writing to files.

import os

os.system("kill process_id") # This will send a SIGTERM signal by default.
os.system("kill -9 process_id") # This will send a SIGKILL signal to a process with the id provided.

os.system("killall process_name") # This will send a SIGTERM signal to all processes named process_name. This can be dangerous, so be careful!
os.system("killall -9 process_name") # This will send a SIGKILL signal to all processes named process_name. This is even more dangerous, use this with care!
On Windows, you can use the TASKKILL command to end a process, or forcefully end a process. However, I'm not sure if you can pull off killing all processes of a specific name with the command alone.

import os

os.system("taskkill /pid process_id") # This will kill the process that has this process id.
os.system("taskkill /f /pid process_id") # This will forcefully kill the process that his this process id.
Hope this helps!
Reply


Messages In This Thread
Close script from another script - by ilgrabbo - Jan-20-2021, 10:00 AM
RE: Close script from another script - by ndc85430 - Jan-20-2021, 12:50 PM
RE: Close script from another script - by spaceraiders - Jan-20-2021, 04:43 PM
RE: Close script from another script - by ilgrabbo - Jan-20-2021, 05:19 PM
RE: Close script from another script - by ilgrabbo - Jan-21-2021, 10:00 AM
RE: Close script from another script - by ilgrabbo - Jan-21-2021, 03:11 PM
RE: Close script from another script - by ilgrabbo - Jan-21-2021, 04:45 PM
RE: Close script from another script - by ilgrabbo - Jan-22-2021, 09:33 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  run part of a script with a different version of Python jdog 3 591 May-27-2024, 01:57 AM
Last Post: Alice12
Shocked Script get's really weird error DaJohn 7 246 May-26-2024, 09:10 AM
Last Post: snippsat
Music Python Script Repeating Number When Saving Facebook Photos ThuanyPK 2 200 May-13-2024, 10:59 PM
Last Post: ebn852_pan
  Trying to generating multiple json files using python script dzgn989 4 302 May-10-2024, 03:09 PM
Last Post: deanhystad
  Problems passing arguments containing spaces to bash script and then on to python kaustin 6 569 Apr-03-2024, 08:26 PM
Last Post: deanhystad
  Using a script to open multiple shells? SuchUmami 9 714 Apr-01-2024, 10:04 AM
Last Post: Gribouillis
  How to include one script into another? MorningWave 8 665 Mar-21-2024, 10:34 PM
Last Post: MorningWave
  ChromeDriver breaking Python script genericusername12414 1 403 Mar-14-2024, 09:39 AM
Last Post: snippsat
  using PowerShell from Python script for mounting shares tester_V 8 679 Mar-12-2024, 06:26 PM
Last Post: tester_V
  No Internet connection when running a Python script basil_555 8 834 Mar-11-2024, 11:02 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020