1

I am running a script called mystepper.py in the background (/home/pi/mystepper.py); it moves two stepper motors for my RPi Camara. I would like to understand the easiest way to kill the mystepper.py script by name if possible. I then plan to create an alias in /home/pi/.bashrc so I can control the camera using my iPhone with the iFreeRDP app. The iFreeRDP app and similar apps have a broken key board such that the period and space bar don't work, thus the need for a shortcut alias to remove the need for spaces and periods.

After killing that script, I will make stepper motor position adjustments with /home/pi/moveit.py and then retart mystepper.py.

So if someone could show me the least complex method of killing a script, that would be great!

1 Answer 1

5

pkill can kill a process by matching against its command line by using the -f flag. See the manpage.

$ pkill -f mystepper.py

should do the trick.

Updated to include Marks suggestion in the comment below

To shorten this into a single command eg: killit, the following line should be placed into ~/.bashrc

alias killit='sudo pkill -f /home/pi/mystepper.py'

Note: The above works without password auth on a Raspberry Pi because the default Raspbian /etc/suoders file disables password challenge when using sudo as the pi user.

i.e., in /etc/suoders

pi ALL=(ALL) NOPASSWD: ALL

See manpage for syntax.

As a different user or in a situation where this does not apply, then adding the following via the visudo command at the very end of /etc/sudoers should work. Replace pi with the correct user that calls the script (replacing with ALL should allow all users to run mystepper.py with root privileges - have not tested this).

pi ALL=(ALL) NOPASSWD: /usr/bin/python /home/pi/mystepper.py

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, that works great! I would modify your answer just a touch. How I got it to work was: alias killit='sudo pkill -f /home/pi/mystepper.py That extra bit helps the newbies like myself but is just a given for you experts. Now I can just type killit to get the job done. Thanks!
No probs, have updated as you've suggested. Also included a reason as to why it works with sudo without a password challenge on Raspbian and how to achieve on other systems.
Thanks. Oh, I just noticed the alias command needs to have a closed quote. I think I missed that on my last reply. Should look like this with the ' at the end, right? killit='sudo pkill -f /home/pi/mystepper.py'
yes, correct. Sorry, didn't see that either. Fixed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.