Stop/kill a process from the command line after a certain amount of time

268

The simplest solution would be to use timeout from the collection of GNU coreutils (probably installed by default on most Linux systems):

timeout 10 ./sopare.py -l

See the manual for this utility for further options (man timeout). On non-GNU systems, this utility may be installed as gtimeout if GNU coreutils is installed at all.

Another alternative, if GNU coreutils is not available, is to start the process in the background and wait for 10 seconds before sending it a termination signal:

./sopare.py -l &
sleep 10
kill "$!"

$! will be the process ID of the most recently started background process, in this case of your Python script.

In case the waiting time is used for other things:

./sopare.py -l & pid=$!
# whatever code here, as long as it doesn't change the pid variable
kill "$pid"
Share:
268

Related videos on Youtube

d4rty
Author by

d4rty

Updated on September 18, 2022

Comments

  • d4rty
    d4rty over 1 year

    I have a component with the following template (...component.html):

    <canvas></canvas>

    Furthermore I have in ...component.ts:

    export class TestComponent implements AfterViewInit {
    
      @ViewChild('canvas')
      private canvasRef: ElementRef;
    

    How can I now calculate the to the window border as marked in the picture below (see red lines)?

    enter image description here

    For jQuery users: I'm looking for a way to calculate $("#desiredCanvasElement").position();

    • enno.void
      enno.void over 6 years
    • d4rty
      d4rty over 6 years
      @mr.void using jQuery is fine for me. Just don't know how to use the canvasRef property together with jQuery
    • enno.void
      enno.void over 6 years
      just do $(canvasRef) , ensure jquery is loaded
    • d4rty
      d4rty over 6 years
      just $(canvasRef) leads to an error, $(this.canvasRef.nativeElement) works!
    • Stephen Boston
      Stephen Boston over 5 years
    • Haxiel
      Haxiel over 5 years
      Since you want to end the program after a specified amount of time, you can look at timeout : ss64.com/bash/timeout.html
  • d4rty
    d4rty over 6 years
    This doesn't work, since the padding is part of the element itself (as you use it in your fiddle). If you change padding to margin (which is an example for my usecase) your solution doesn't work.
  • Pezetter
    Pezetter over 6 years
    Fair enough, but the same methodology should work, no?
  • Kusalananda
    Kusalananda over 5 years
    You don't start anything in the background. If you did (using &), the PID of that process would be available in "$!".
  • Z E Nir
    Z E Nir over 5 years
    I thought "$!" will return the PID of the invoking script... good to learn new things. Thanks! (btw i didn't try it with python, but my script is working with a while(1) C program) Also in your solution, you must consider 10 seconds is a lot of time and maybe $! value is no longer what you wanted...
  • Kusalananda
    Kusalananda over 5 years
    You have $$ which would be the PID of the current script. Is that what you're thinking of?