0

I have a task to stop a running process and run it again with the very same command it was run previously with.

In a nutshell, I need to change the configs of the running program and restart it to apply changes.

I have a program.pid file where the application stores its PID. A ps program on my setup doesn't have a -p or alike flags.

What I want to do is to ps | grep by the PID I get by more program.pid.

The question is how can I put in the result of the second command into the first as the second parameter of grep?

1 Answer 1

0

Most shells support $(command) or `command` to use the command's output as a variable. At least in Bash, there is also the shortcut $(<file) to read a file's contents without invoking any subcommand at all.

grep "$(cat program.pid)"
pid=$(cat program.pid)
grep "$pid"
grep "$(< program.pid)"

However, there's a more direct way to read command lines on Linux without using ps – you can read from /proc/<pid>/cmdline. The virtual file contains NUL-delimited arguments, which can be read using e.g. Bash's mapfile function:

mapfile -d "" args < /proc/$pid/cmdline

Now args contains an array of the individual command-line arguments, which you can use as:

myprogram "${args[@]}"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .