Sample Header Ad - 728x90

how to write a pgrep pattern that (never) matches a zero byte?

1 vote
1 answer
416 views
I'd like to kill a process with a long name that ends with foo. Currently my plan is to use pkill (though for testing, to be friendly to the process, I've been checking my patterns with pgrep instead). I've seen [this question](https://unix.stackexchange.com/q/267007/66679) that shows that for long process names, you must use -f to get the whole command line. The works great for getting the rest of the long process name; the problem is that then the pattern is matched against the whole command line rather than just the process name, and I would not like to accidentally kill a different program that happened to have foo as an argument. So I believe I would like to write a pattern something like this: pgrep -f '^[^\0]*foo' where I've written \0 to mean a zero byte -- the separator that -f puts before each argument to a process. Of course \0 doesn't work; this pattern actually matches any sequence of characters that are not \ or (ASCII) 0 instead. I also tried this: pgrep -f '^[^'echo -n '\0'']*foo' Here I have confirmed that my shell produces a single 0 byte for echo -n '\0'. Unfortunately, because of the way arguments are passed, pgrep stops scanning a pattern when it sees an actual 0 byte, so this command behaves exactly the same as pgrep -f '^[^' would -- that is, it throws an error about receiving an invalid pattern. How can I write a pattern that refers to a zero byte in it (or, well, any byte that isn't a zero byte)?
Asked by Daniel Wagner (192 rep)
Sep 23, 2020, 04:36 PM
Last activity: Nov 21, 2020, 01:56 PM