Score:1

Wait for process with name to spawn in Bash

kh flag

I want to write a Bash script that runs in a background, waiting for a command to spawn.

Suppose I ran script.sh which waits for ls. After I run ls, it should trigger actions?

Score:1
vn flag

You can use pgrep to create a while loop like this:

#!/bin/bash

# Wait for particular process to run
while ! pgrep "process-name " > /dev/null; do

    # Set optional delay
    sleep .1

done

# Do something when the process has started
your-commands-here

You need to adjust the delay, so that it "catches" the process you run. This of course works best if it should detect a service that doesn't stop again, but this will work for any command that takes longer time to finish than the delay you have set (ls is a bad example, since it only takes milliseconds to finish the process).

Also see this post on Unix and Linux.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.