+ Post New Thread
Results 1 to 11 of 11
Raspberry Pi, Arduino & Other SBCs Thread, Kill process(es) of a running script in Linux in Technical; I've created a script Code: #!/bin/bash sudo python scratch_gpio_handler.py that runs a python program to handle traffic between Scratch and ...
  1. #1

    SimpleSi's Avatar
    Join Date
    Jun 2005
    Location
    Lancashire
    Posts
    5,809
    Thank Post
    1,476
    Thanked 592 Times in 444 Posts
    Rep Power
    168

    Kill process(es) of a running script in Linux

    I've created a script
    Code:
    #!/bin/bash
    sudo python scratch_gpio_handler.py
    that runs a python program to handle traffic between Scratch and the GPIO pins on a RaspberryPi.

    This program consumes a lot of CPU (as its just sits in a loop awaiting changes) and I'd like users (e.g pupils) to kill it when they are not using it.

    Using ps aux I find I've got 3 processes running

    /bin/bash ./sgh (the name of the script file)
    sudo python scratch_gpio_handler.py
    python scratch_gpio_handler.py

    I'm finding that if a I find out the pid of the last one and kill it - the other two seem to be killed as well

    But I can't work out the syntax to script killing it

    I can use pkill python but don't want to assume that my script is the only python one running.

    Any ideas please?
    regards
    Simon

  2. #2


    Join Date
    May 2009
    Posts
    2,946
    Thank Post
    259
    Thanked 778 Times in 591 Posts
    Rep Power
    285
    I think the ultimate is :

    kill -9 <pid>

    the ps command will as standard only give you your processes and permissions should stop students killing other peoples processes (if that is what you are worried about). With tight loops that idle for long periods, consider "sleep(t)" which will yeild to the thread scheduler. You should find though that you are only using a lot of CPU because that is available to you (i.e, there is nothing else happening on the system).

  3. #3

    jinnantonnixx's Avatar
    Join Date
    Mar 2011
    Location
    In the Calamatorium.
    Posts
    1,954
    Thank Post
    109
    Thanked 482 Times in 331 Posts
    Blog Entries
    2
    Rep Power
    281
    You can kill by name, rather than pid, with:

    killall -v /full_path_to_your_process
    Last edited by jinnantonnixx; 2nd August 2012 at 10:26 AM.

  4. #4


    tom_newton's Avatar
    Join Date
    Sep 2006
    Location
    Leeds
    Posts
    4,463
    Thank Post
    866
    Thanked 845 Times in 667 Posts
    Rep Power
    195
    killall is probably your best method.

    Ideally though, you need to change your script so it doesn't busyloop...

  5. #5

    SimpleSi's Avatar
    Join Date
    Jun 2005
    Location
    Lancashire
    Posts
    5,809
    Thank Post
    1,476
    Thanked 592 Times in 444 Posts
    Rep Power
    168
    Sorry - I'm not making myself understood - I know what needs doing - I just don't know the syntax to do it

    what syntax/script would kill the running process that shows up as

    Code:
    python scratch_gpio_handler.py
    in the command column?
    Ideally though, you need to change your script so it doesn't busyloop...
    Well, its a python prog that sits waiting for data to appear on 127.0.0.1:42001 and also monitors changes to GPIO pin inputs so its a bit hard to make it not loop

    Under normal circumstances (e.g running under windows on a 1.6Ghz machine) then the running python prog wouldn't slow down the machine enough to be an issue but I'm finding that its dragging down the Scratch GUI speed to a point where kids would think it unusable so I'm wanting (as current workaround until we can get Scratch directly talking to the hardware) to just have 2 desktop icons - one to start the script and 1 to kill it.

    I'm also working on the python prog terminating on receiving a defined Scratch broadcast but since I know <0.1% Python ATM, I'm having difficulty in closing threads gracefully within the program - but I'll get their eventually.

    I just need this bodge as I'm demo-ing my efforts so far at Monday's RaspberryJam at Preston


    So back to the question

    Code:
    sudo killall ......... (replace the dots please :) )
    Si

  6. #6

    jinnantonnixx's Avatar
    Join Date
    Mar 2011
    Location
    In the Calamatorium.
    Posts
    1,954
    Thank Post
    109
    Thanked 482 Times in 331 Posts
    Blog Entries
    2
    Rep Power
    281
    I might be being dense here, but wouldn't that be killall python\ scratch_gpio_handler.py

    OK, I've never been a fan of killing processes, so could you start it with a nice value?
    (e.g. nice -n 10 python scratch_gpio_handler.py)

    or maybe a 'renice' to change the running priority? If you choose a high nice value it drops down in the scheduler priority, perhaps your system won't choke up.

    For your original problem, I've done something like this to kill specific running stuff, but my library files are at work so I can look tomorrow.
    Last edited by jinnantonnixx; 2nd August 2012 at 07:17 PM.

  7. #7


    tom_newton's Avatar
    Join Date
    Sep 2006
    Location
    Leeds
    Posts
    4,463
    Thank Post
    866
    Thanked 845 Times in 667 Posts
    Rep Power
    195
    Or have the python daemon write a pidfile out... then kill `cat /path/to/pidfile`

  8. #8


    tom_newton's Avatar
    Join Date
    Sep 2006
    Location
    Leeds
    Posts
    4,463
    Thank Post
    866
    Thanked 845 Times in 667 Posts
    Rep Power
    195

  9. Thanks to tom_newton from:

    SimpleSi (2nd August 2012)

  10. #9

    SimpleSi's Avatar
    Join Date
    Jun 2005
    Location
    Lancashire
    Posts
    5,809
    Thank Post
    1,476
    Thanked 592 Times in 444 Posts
    Rep Power
    168
    [code]sudo killall python\ scratch_gpio_handler.py[code]

    gives
    Code:
    python scratch_gpio_handler.py: no process found :(
    I've now got this code from another forum
    Code:
    sudo ps aux | grep "python scratch_gpio_handler.py" | grep -v grep | awk '{print $2}' | xargs sudo kill -9
    which seems to do the job
    (its gives an error if no scratch_gpio_handler.py actaully running but I can live with that )

    Or have the python daemon write a pidfile out... then kill `cat /path/to/pidfile`
    You DID read my bit about understanding python <0.001%?

    Simon

  11. #10

    dhicks's Avatar
    Join Date
    Aug 2005
    Location
    Knightsbridge
    Posts
    5,622
    Thank Post
    1,240
    Thanked 777 Times in 674 Posts
    Rep Power
    235
    Quote Originally Posted by tom_newton View Post
    But, if I understand correctly, the problem is with this part (from the link above):

    Python’s select() function is a direct interface to the underlying operating system implementation. It monitors sockets, open files, and pipes (anything with a fileno() method that returns a valid file descriptor)
    The assorted Raspberry Pi Linux distributions don't, by default, provide a socket for the GPIO ports. I think there are kernel patches available, but for which version of which kernel is tricky to work out.

  12. #11

    dhicks's Avatar
    Join Date
    Aug 2005
    Location
    Knightsbridge
    Posts
    5,622
    Thank Post
    1,240
    Thanked 777 Times in 674 Posts
    Rep Power
    235
    Quote Originally Posted by SimpleSi View Post
    You DID read my bit about understanding python <0.001%?
    os.getpid() should do it:

    Code:
    import os
    pidFile = open("/var/tmp/gpio.pid", "w")
    pidFile.write(os.getpid())
    pidFile.close()

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 8
    Last Post: 19th April 2012, 04:29 PM
  2. Running WPKG (and scripts in general) on WIn7
    By SimpleSi in forum Windows 7
    Replies: 9
    Last Post: 24th June 2011, 11:34 AM
  3. Can you run a script in a PDF?
    By GREED in forum Scripts
    Replies: 2
    Last Post: 2nd November 2010, 05:11 PM
  4. Hide running script in windows 7?
    By reggiep in forum Scripts
    Replies: 2
    Last Post: 7th October 2010, 04:31 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •