4

Is there a way to sigterm a process with a timeout? If the process does not gracefully terminate within 30 minutes, the process should get sigkill. Ideally, this graceful shutdown should be executed on the background.

dimo414
  • 47,227
  • 18
  • 148
  • 244
dwong
  • 103
  • 5
  • 14

1 Answers1

7

There's the timeout command, which allows you to cap a process' execution time and escalate to a SIGKILL if it doesn't respond promptly to the initial signal (SIGTERM by default). This isn't quite what you're asking for, but it might be sufficient.

To do what you're actually describing (send a signal, briefly await, then send a kill) you may have to do a bit of bookkeeping yourself, as this question details.

One option would be to use Upstart (or I imagine other service managers), which provides a kill timeout n command that does what you want.


As an aside, many systems would treat 30 minutes as much too long to wait for SIGTERM. Linux does something akin to what you're describing on shutdown, for instance, but gives processes barely a few seconds to clean up and exit before SIGKILLing them. For other use cases you certainly can have a long-lived termination like you describe (e.g. with Upstart), but YMMV.

dimo414
  • 47,227
  • 18
  • 148
  • 244
  • Thanks a lot. I'll definite look into your recommendations! – dwong Mar 13 '18 at 00:07
  • How should look the command to terminate pid and if there after 10 seconds kill it? My try is: "sudo timeout -vk 5 7 kill PIDhere" but not sure if it is good or how to adjust values. – 16851556 Feb 22 '22 at 17:45
  • @16851556 I'd suggest posting a new question with more details about what you're trying to do and what you've tried so far. Feel free to share a link to your post here and I can take a look. – dimo414 Feb 22 '22 at 21:54
  • 1
    @dimo414 here please https://stackoverflow.com/questions/71237085/linux-short-simple-command-how-to-send-sigterm-to-a-process-and-sigkill-if-it-fa – 16851556 Feb 23 '22 at 12:39