Jancarius
Jancarius

Reputation: 1

How to Parse Values from output in BASH

I'm writing a script that should create a rotating series of debug logs as it runs over a period of time. My current problem is that when I ran it with -vx attached, I can see that it stops during the actual debugging process and doesn't proceed through the loop. This is reflective of how the command would run normally. So I thought to continue the process, I want to run with &.

The problem is that this will become exponentially messier over time (since none of the processes are stopping). So what I'm looking for is a way to parse the PID output of the & command into a variable, and then I will add a kill command at the start of the loop pointed at that variable.

Figuring out how to parse the output of commands will also be useful in the other part of my project, which is to terminate the while loop based on a particular % free in a df -h for a select partition

Upvotes: 0

Views: 63

Answers (1)

John Kugelman
John Kugelman

Reputation: 362107

No parsing needed. The PID of the most recent background process is stored in $!.

command &    # run command in background
pid=$!       # save pid as $pid

...

kill $pid    # kill command

Upvotes: 3

Related Questions