Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
closed by
Can anyone tell me how to process the SIGTERM signal gracefully?
closed

4 Answers

0 votes
by (19k points)
selected by
 
Best answer
To gracefully handle the SIGTERM signal in Python, follow these steps:

Begin by importing the required modules:

import signal

import sys

Define a signal handler function that will be invoked upon receiving the SIGTERM signal:

def sigterm_handler(signal, frame):

    # Add code here to perform cleanup or any necessary operations for graceful shutdown

    # ...

    # Terminate the program gracefully

    sys.exit(0)

Register the signal handler function to handle the SIGTERM signal:

signal.signal(signal.SIGTERM, sigterm_handler)

Proceed with your main program logic:

def main():

    # Insert your main program logic here

    # ...

    # Keep the program running

    while True:

        # Your code goes here

        pass

if __name__ == "__main__":

    main()

Upon receiving a SIGTERM signal (e.g., by executing kill -SIGTERM <pid> in the terminal, where <pid> represents the process ID of your program), the sigterm_handler function will be triggered. Within this function, you can include any necessary cleanup tasks or actions required for a graceful shutdown, such as saving data, closing connections, or releasing resources. Finally, using sys.exit(0) ensures that the program terminates gracefully with an exit status of 0.

By implementing this approach, your Python program will be equipped to handle SIGTERM signals effectively, allowing for proper cleanup and a smooth shutdown process.
0 votes
by (26.4k points)

Kindly check the below code:

import signal

import time

class GracefulKiller:

  kill_now = False

  def __init__(self):

    signal.signal(signal.SIGINT, self.exit_gracefully)

    signal.signal(signal.SIGTERM, self.exit_gracefully)

  def exit_gracefully(self,signum, frame):

    self.kill_now = True

if __name__ == '__main__':

  killer = GracefulKiller()

  while not killer.kill_now:

    time.sleep(1)

    print("doing something in a loop ...")

  print("End of the program. I was killed gracefully :)")

Want to learn python to get expertise in the concepts of python? Join python certification course and get certified

0 votes
by (25.7k points)
To process the SIGTERM signal gracefully in Python, you can follow these steps:

Import the necessary modules:

import signal

import sys

Define a signal handler function that will be called when the SIGTERM signal is received:

def sigterm_handler(signal, frame):

    # Perform any cleanup or graceful shutdown operations here

    # ...

    # Exit the program

    sys.exit(0)

Register the signal handler function to handle the SIGTERM signal:

signal.signal(signal.SIGTERM, sigterm_handler)

Run your main program logic:

def main():

    # Your main program logic goes here

    # ...

    # Keep the program running

    while True:

        # Your code here

        pass

if __name__ == "__main__":

    main()

When a SIGTERM signal is received (for example, by running kill -SIGTERM <pid> in the terminal where <pid> is the process ID of your program), the sigterm_handler function will be called. Inside this function, you can perform any necessary cleanup or graceful shutdown operations, such as saving data, closing connections, or releasing resources. Finally, the sys.exit(0) statement is used to exit the program gracefully with a status code of 0.

By implementing this signal handling mechanism, your program can respond to SIGTERM signals and perform any necessary actions before terminating gracefully.
0 votes
by (15.4k points)
To gracefully handle the SIGTERM signal in Python, follow these simplified steps:

Import the required modules:

import signal

import sys

Define the signal handler function:

def sigterm_handler(signal, frame):

    sys.exit(0)

Register the signal handler:

signal.signal(signal.SIGTERM, sigterm_handler)

Implement your main program logic as needed.

By following these steps, the program will gracefully handle the SIGTERM signal by exiting with a status code of 0.

Related questions

0 votes
4 answers
0 votes
1 answer
0 votes
1 answer
asked Dec 13, 2020 in Python by ashely (50.2k points)

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...