Running infinite loop and getting commands from "outside" (e.g. shell or other scripts)

how to break infinite while loop in shell script
bash infinite loop one line
bash infinite loop with counter
bash while loop
bash for loop
bash sleep
linux command line while true loop
ubuntu infinite while loop

I am working on my Raspberry Pi, that is handling some WS2812B RGB-LEDs. I can control the light and everything with the neopixel library and Python. So fine right now.

I want this Python script running an infinite loop that only deals with light management. Dimming LEDs, changing color and lots more. But, I want to be able to get commands from other scripts. Let's say I want to type in a shell command that will change the color. In my infinite Python script (LED Handler), I will be able to recognize this command and change the color or the light mode softly to the desired color.

One idea is, to constantly look into a text file, if there is a new command. And my shell script is able to insert command lines into this text file.

But can you tell me, if there is a better solution of doing it?

Many thanks in advance.

One method would be to expose a TCP server, then communicate with the Python process over TCP. A simple example on how to create a TCP server is here, showcasing both the server script (running the LEDs) and the command scripts: example

Infinite while loop - Linux Shell Scripting Tutorial, You can use : special command with while loop to tests or set an infinite loop or an endless loop. An infinite loop occurs when the condition will never be met,  For example, the menu driven program typically continue till user selects to exit his or her main menu (loop). To set an infinite while loop use: true command - do nothing, successfully (always returns exit code 0) false command - do nothing, unsuccessfully (always returns exit code 1): command - no effect; the command does nothing (always returns exit code 0) Syntax. Use : command to set an infinite loop:

I suggest opening a port with your python script and make it receive commands from that port (network programming). Although this would make your project more complicated, it is a very robust implementation.

Bash while Loop, An infinite loop is a loop that repeats indefinitely and never terminates. If the condition always evaluates to true, you get an infinite loop. In the following example, we are using the built-in command : to create an infinite loop. : always The while loop above will run indefinitely. A loop that repeats indefinitely and never terminates is called an Infinite loop. Most of the time we create infinite loops by mistake. However, this doesn’t mean that the infinite loops are not useful. Infinite loops are commonly used in programs that keep running for long periods of time until they are stopped like the web server.

You can use ZeroMQ and host it locally. It provides bindings for Python. Here is an example script (sender and receiver):

from threading import Thread
import zmq

class Sender(Thread):
    def run(self):
        context = zmq.Context()
        socket = context.socket(zmq.PUB)
        while True:
            socket.send_string(input('Enter command: '))

class Receiver(Thread):
    def run(self):
        context = zmq.Context()
        socket = context.socket(zmq.SUB)
        socket.setsockopt(zmq.SUBSCRIBE, b'')
        while True:
            data = socket.recv().decode('ascii')
            print(data)  # Do stuff with data.

The receiver would be the instance that controls the lights on the RPi and the sender is the command line script that let's you input the various commands. An advantage is that ZeroMQ supports bindings for various programming languages and you can also send/receive commands over a network.

How to break out of a program in an infinite loop?, As mentioned, you can simply add a & to the command line. You can also hit CTRL-Z (this puts the process in the Stopped state), and then type bg to get it running  How to Create an Infinite Loop in Python. In this article, we show how to create an infinite loop in Python. An infinite loop that never ends; it never breaks out of the loop. So, whatever is in the loop gets executed forever, unless the program is terminated. For certain situations, an infinite loop may be necessary.

Another solution is to allow commands from a network connection. The script with the "infinite loop" will read input from a socket and perform the commands.

Unknowingly ran an infinite loop in terminal, You can run jobs command in your session to list active background jobs and then put them to foreground by typing fg <job number> . I disagree with jave.web - there's nothing wrong with goto if you use it correctly. For example, I'm about to use it to ensure the program that is run within the loop auto-respawns if it dies for whatever reason. It may however be wise to put a small wait in the loop to stop cpu thrashing if my program dies instantly for some reason.

Shell infinite loop to execute at specific time, Check the time in the loop, and then sleep for a minute if it's not the time you want​. while : do if [ $(date '+%H%M') = '0600' ] then  Start a background process at application start up. This process will run in background, so that it does not block UI and user can work on User interface. Stop this process when application is closes. I am using an infinite loop to run my background process forever. I am using backgroundworker for running the above process in background.

Run an infinite loop in bash, I want to run a command for ever in the Linux bash. translation and definition "​infinite loop", Bash – While Loop Example Getting Stuck in an Infinite Loop. file1.txt differs everytime based upon the find command. I need to use the unlock command.but it's going to infinite loop if i run the scipt.It's not going to another step.Could some one please help me on this. Output of the code:

Setting Up an Infinite Loop with repeat, Setting Up an Infinite Loop with repeat If you simply write repeat, as in the following, Theoretically, this loop will execute forever, which of course, you don'​t want to happen. Get Beginning AppleScript® now with O'Reilly online learning. Stack Exchange network consists of 177 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

  • The socket suggestions below are the way to go. The only thing I'd add is that if you don't need to receive these commands externally (if it's a script going to control them), you may want to consider using Unix Domain Sockets instead.
  • @JohnSzakmeister Feel free to post this as an answer.
  • This one is very nice. Thank you so much. But I have one question. In your example, the call "server.accept()" blocks the script until a new client-connection is established. But I want to keep the main script ongoing. Is there something like a function with a returning value indicating if new data is available? Thank you