SohaibAJ
Posts: 4
Joined: Thu Jan 18, 2018 1:02 pm

Multiple scripts on Pi

Thu Jan 18, 2018 1:12 pm

Hello,
I would like to know if it is possible to run multiple scripts on pi at the same time with a script capable of interrupting another script. If possible an example would be great.

What I would like to do is use one script to measure distance from a sensor continuously and another script running a collision avoidance algorithm. The first script measuring the distance will be able to send a stop command for the vehicle if the distance falls below a certain threshold say 50cm. The second script should then run a code for turning left and moving forward 100cm for example. But if there is an object detected in front of the vehicle, the first script should come into play and immediately stop the vehicle (whether or not the 100cm movement is completed) and then restart the second script.

Thanks in advance.
Sohaib AJ

User avatar
DougieLawson
Posts: 33785
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Multiple scripts on Pi

Fri Jan 19, 2018 5:30 pm

What language are you using?

Python has a signal handler, you can use a kill -usr1 $(pidof pythonprogramname) [ or -usr2 ] to send a command.

But it's probably better, easier, simpler to get the scripts that need signalling to subscribe to an MQTT topic, you can then easily publish a MQTT message when needed.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

scotty101
Posts: 3083
Joined: Fri Jun 08, 2012 6:03 pm

Re: Multiple scripts on Pi

Fri Jan 19, 2018 8:34 pm

Not sure why you'd need two separate scripts. Just use threading to run a background task that reads the distance and that can communicate to the main program if the distance is too close.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 25 guests