Get process information (the command, etc) from pid
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have this code:
for job in `jobs -p`; do
code=0;
wait $job || code=$?
if [[ "$code" != "0" ]]; then
echo "At least one job failed with exit code => $code" ;
exit 1;
fi
done
right after the line:
echo "At least one job failed with exit code => $CODE" ;
I want to add a line that logs the command that failed, something like this:
for job in `jobs -p`; do
code=0;
wait $job || code=$?
if [[ "$code" != "0" ]]; then
echo "At least one job failed with exit code => $code" ;
echo "The job that failed was $(ps -p $job)"
exit 1;
fi
done
not that a job
is just the pid of the subprocess. The problem is that this line:
echo "The job that failed was $(ps -p $job)"
doesn't really about anything - I need the actually command that was run, given the pid=job.
bash shell-script process job-control
add a comment |Â
up vote
0
down vote
favorite
I have this code:
for job in `jobs -p`; do
code=0;
wait $job || code=$?
if [[ "$code" != "0" ]]; then
echo "At least one job failed with exit code => $code" ;
exit 1;
fi
done
right after the line:
echo "At least one job failed with exit code => $CODE" ;
I want to add a line that logs the command that failed, something like this:
for job in `jobs -p`; do
code=0;
wait $job || code=$?
if [[ "$code" != "0" ]]; then
echo "At least one job failed with exit code => $code" ;
echo "The job that failed was $(ps -p $job)"
exit 1;
fi
done
not that a job
is just the pid of the subprocess. The problem is that this line:
echo "The job that failed was $(ps -p $job)"
doesn't really about anything - I need the actually command that was run, given the pid=job.
bash shell-script process job-control
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have this code:
for job in `jobs -p`; do
code=0;
wait $job || code=$?
if [[ "$code" != "0" ]]; then
echo "At least one job failed with exit code => $code" ;
exit 1;
fi
done
right after the line:
echo "At least one job failed with exit code => $CODE" ;
I want to add a line that logs the command that failed, something like this:
for job in `jobs -p`; do
code=0;
wait $job || code=$?
if [[ "$code" != "0" ]]; then
echo "At least one job failed with exit code => $code" ;
echo "The job that failed was $(ps -p $job)"
exit 1;
fi
done
not that a job
is just the pid of the subprocess. The problem is that this line:
echo "The job that failed was $(ps -p $job)"
doesn't really about anything - I need the actually command that was run, given the pid=job.
bash shell-script process job-control
I have this code:
for job in `jobs -p`; do
code=0;
wait $job || code=$?
if [[ "$code" != "0" ]]; then
echo "At least one job failed with exit code => $code" ;
exit 1;
fi
done
right after the line:
echo "At least one job failed with exit code => $CODE" ;
I want to add a line that logs the command that failed, something like this:
for job in `jobs -p`; do
code=0;
wait $job || code=$?
if [[ "$code" != "0" ]]; then
echo "At least one job failed with exit code => $code" ;
echo "The job that failed was $(ps -p $job)"
exit 1;
fi
done
not that a job
is just the pid of the subprocess. The problem is that this line:
echo "The job that failed was $(ps -p $job)"
doesn't really about anything - I need the actually command that was run, given the pid=job.
bash shell-script process job-control
edited Mar 24 at 13:55
Jeff Schaller
31.2k846105
31.2k846105
asked Mar 24 at 4:50
Alexander Mills
1,885929
1,885929
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
3
down vote
At the point you're running the ps
command, the process already exited (you're handling its return code, right?)
Just change the logic so you run the ps
command earlier in the script, before the wait
and store that in a variable:
for job in $(jobs -p); do
process_info=$(ps -p "$job" -o args=)
wait "$job" ||
code=$?
echo "Job running [$process_info] failed with exit code $code." >&2
exit 1
done
thanks, seems like the right idea, but not sure if this works on MacOS though, got something x-platform?
â Alexander Mills
Mar 24 at 5:11
I don't see why it wouldn't...
â Filipe Brandenburger
Mar 24 at 5:26
take a look at this: gist.github.com/ORESoftware/2f0444cadef5b3546fca0eec749b4b70
â Alexander Mills
Mar 24 at 5:28
I just get a blank line => $process_info is just empty...I tried a lot of things...nothing is working :(
â Alexander Mills
Mar 24 at 5:28
@FilipeBrandenburger You can't do this either in case the jobs exit out of order. For example, if the last job in the output ofjobs -p
exits first.
â Kusalananda
Mar 24 at 7:09
 |Â
show 1 more comment
up vote
0
down vote
As pointed out, you can't use ps
on a non-existing process.
If you have a bash
that supports associative arrays:
declare -A jobinfo
# collect job information
joblist=( $(jobs -p) )
for pid in "$joblist[@]"; do
jobinfo["$pid"]="$( ps -p "$pid" -o args= )"
done
# later...
# wait for jobs
err=0
for pid in "$joblist[@]"; do
if ! wait "$pid" ; then
printf 'Got non-zero exit status (%d) from PID %d: %sn'
"$code" "$pid" "$jobinfo[$pid]"
err=1
fi
done
# exit 0 if all was good, exit 1 otherwise
exit "$err"
Here I collect all the necessary data before starting to wait for jobs to exit. I also wait for all jobs to finish so that the user gets information about not just the first failing job.
Without using associative arrays (should work in the default bash
on macOS):
# collect job information
joblist=( $(jobs -p) )
for pid in "$joblist[@]"; do
jobinfo+=( "$( ps -p "$pid" -o args= )" )
done
# later...
err=0
i=0
for pid in "$joblist[@]"; do
if ! wait "$pid" ; then
printf 'Got non-zero exit status (%d) from PID %d: %sn'
"$code" "$pid" "$jobinfo[$i]"
err=1
fi
i=$(( i + 1 ))
done
exit "$err"
add a comment |Â
up vote
0
down vote
If your OS (e.g. Linux) has a /proc directory, you can get every needed information from /proc/$PID
for PID in $(jobs -p); do
dosomethingwith /proc/$PID/ # please take a look at what it contents ;-)
done
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
At the point you're running the ps
command, the process already exited (you're handling its return code, right?)
Just change the logic so you run the ps
command earlier in the script, before the wait
and store that in a variable:
for job in $(jobs -p); do
process_info=$(ps -p "$job" -o args=)
wait "$job" ||
code=$?
echo "Job running [$process_info] failed with exit code $code." >&2
exit 1
done
thanks, seems like the right idea, but not sure if this works on MacOS though, got something x-platform?
â Alexander Mills
Mar 24 at 5:11
I don't see why it wouldn't...
â Filipe Brandenburger
Mar 24 at 5:26
take a look at this: gist.github.com/ORESoftware/2f0444cadef5b3546fca0eec749b4b70
â Alexander Mills
Mar 24 at 5:28
I just get a blank line => $process_info is just empty...I tried a lot of things...nothing is working :(
â Alexander Mills
Mar 24 at 5:28
@FilipeBrandenburger You can't do this either in case the jobs exit out of order. For example, if the last job in the output ofjobs -p
exits first.
â Kusalananda
Mar 24 at 7:09
 |Â
show 1 more comment
up vote
3
down vote
At the point you're running the ps
command, the process already exited (you're handling its return code, right?)
Just change the logic so you run the ps
command earlier in the script, before the wait
and store that in a variable:
for job in $(jobs -p); do
process_info=$(ps -p "$job" -o args=)
wait "$job" ||
code=$?
echo "Job running [$process_info] failed with exit code $code." >&2
exit 1
done
thanks, seems like the right idea, but not sure if this works on MacOS though, got something x-platform?
â Alexander Mills
Mar 24 at 5:11
I don't see why it wouldn't...
â Filipe Brandenburger
Mar 24 at 5:26
take a look at this: gist.github.com/ORESoftware/2f0444cadef5b3546fca0eec749b4b70
â Alexander Mills
Mar 24 at 5:28
I just get a blank line => $process_info is just empty...I tried a lot of things...nothing is working :(
â Alexander Mills
Mar 24 at 5:28
@FilipeBrandenburger You can't do this either in case the jobs exit out of order. For example, if the last job in the output ofjobs -p
exits first.
â Kusalananda
Mar 24 at 7:09
 |Â
show 1 more comment
up vote
3
down vote
up vote
3
down vote
At the point you're running the ps
command, the process already exited (you're handling its return code, right?)
Just change the logic so you run the ps
command earlier in the script, before the wait
and store that in a variable:
for job in $(jobs -p); do
process_info=$(ps -p "$job" -o args=)
wait "$job" ||
code=$?
echo "Job running [$process_info] failed with exit code $code." >&2
exit 1
done
At the point you're running the ps
command, the process already exited (you're handling its return code, right?)
Just change the logic so you run the ps
command earlier in the script, before the wait
and store that in a variable:
for job in $(jobs -p); do
process_info=$(ps -p "$job" -o args=)
wait "$job" ||
code=$?
echo "Job running [$process_info] failed with exit code $code." >&2
exit 1
done
answered Mar 24 at 5:00
Filipe Brandenburger
3,461621
3,461621
thanks, seems like the right idea, but not sure if this works on MacOS though, got something x-platform?
â Alexander Mills
Mar 24 at 5:11
I don't see why it wouldn't...
â Filipe Brandenburger
Mar 24 at 5:26
take a look at this: gist.github.com/ORESoftware/2f0444cadef5b3546fca0eec749b4b70
â Alexander Mills
Mar 24 at 5:28
I just get a blank line => $process_info is just empty...I tried a lot of things...nothing is working :(
â Alexander Mills
Mar 24 at 5:28
@FilipeBrandenburger You can't do this either in case the jobs exit out of order. For example, if the last job in the output ofjobs -p
exits first.
â Kusalananda
Mar 24 at 7:09
 |Â
show 1 more comment
thanks, seems like the right idea, but not sure if this works on MacOS though, got something x-platform?
â Alexander Mills
Mar 24 at 5:11
I don't see why it wouldn't...
â Filipe Brandenburger
Mar 24 at 5:26
take a look at this: gist.github.com/ORESoftware/2f0444cadef5b3546fca0eec749b4b70
â Alexander Mills
Mar 24 at 5:28
I just get a blank line => $process_info is just empty...I tried a lot of things...nothing is working :(
â Alexander Mills
Mar 24 at 5:28
@FilipeBrandenburger You can't do this either in case the jobs exit out of order. For example, if the last job in the output ofjobs -p
exits first.
â Kusalananda
Mar 24 at 7:09
thanks, seems like the right idea, but not sure if this works on MacOS though, got something x-platform?
â Alexander Mills
Mar 24 at 5:11
thanks, seems like the right idea, but not sure if this works on MacOS though, got something x-platform?
â Alexander Mills
Mar 24 at 5:11
I don't see why it wouldn't...
â Filipe Brandenburger
Mar 24 at 5:26
I don't see why it wouldn't...
â Filipe Brandenburger
Mar 24 at 5:26
take a look at this: gist.github.com/ORESoftware/2f0444cadef5b3546fca0eec749b4b70
â Alexander Mills
Mar 24 at 5:28
take a look at this: gist.github.com/ORESoftware/2f0444cadef5b3546fca0eec749b4b70
â Alexander Mills
Mar 24 at 5:28
I just get a blank line => $process_info is just empty...I tried a lot of things...nothing is working :(
â Alexander Mills
Mar 24 at 5:28
I just get a blank line => $process_info is just empty...I tried a lot of things...nothing is working :(
â Alexander Mills
Mar 24 at 5:28
@FilipeBrandenburger You can't do this either in case the jobs exit out of order. For example, if the last job in the output of
jobs -p
exits first.â Kusalananda
Mar 24 at 7:09
@FilipeBrandenburger You can't do this either in case the jobs exit out of order. For example, if the last job in the output of
jobs -p
exits first.â Kusalananda
Mar 24 at 7:09
 |Â
show 1 more comment
up vote
0
down vote
As pointed out, you can't use ps
on a non-existing process.
If you have a bash
that supports associative arrays:
declare -A jobinfo
# collect job information
joblist=( $(jobs -p) )
for pid in "$joblist[@]"; do
jobinfo["$pid"]="$( ps -p "$pid" -o args= )"
done
# later...
# wait for jobs
err=0
for pid in "$joblist[@]"; do
if ! wait "$pid" ; then
printf 'Got non-zero exit status (%d) from PID %d: %sn'
"$code" "$pid" "$jobinfo[$pid]"
err=1
fi
done
# exit 0 if all was good, exit 1 otherwise
exit "$err"
Here I collect all the necessary data before starting to wait for jobs to exit. I also wait for all jobs to finish so that the user gets information about not just the first failing job.
Without using associative arrays (should work in the default bash
on macOS):
# collect job information
joblist=( $(jobs -p) )
for pid in "$joblist[@]"; do
jobinfo+=( "$( ps -p "$pid" -o args= )" )
done
# later...
err=0
i=0
for pid in "$joblist[@]"; do
if ! wait "$pid" ; then
printf 'Got non-zero exit status (%d) from PID %d: %sn'
"$code" "$pid" "$jobinfo[$i]"
err=1
fi
i=$(( i + 1 ))
done
exit "$err"
add a comment |Â
up vote
0
down vote
As pointed out, you can't use ps
on a non-existing process.
If you have a bash
that supports associative arrays:
declare -A jobinfo
# collect job information
joblist=( $(jobs -p) )
for pid in "$joblist[@]"; do
jobinfo["$pid"]="$( ps -p "$pid" -o args= )"
done
# later...
# wait for jobs
err=0
for pid in "$joblist[@]"; do
if ! wait "$pid" ; then
printf 'Got non-zero exit status (%d) from PID %d: %sn'
"$code" "$pid" "$jobinfo[$pid]"
err=1
fi
done
# exit 0 if all was good, exit 1 otherwise
exit "$err"
Here I collect all the necessary data before starting to wait for jobs to exit. I also wait for all jobs to finish so that the user gets information about not just the first failing job.
Without using associative arrays (should work in the default bash
on macOS):
# collect job information
joblist=( $(jobs -p) )
for pid in "$joblist[@]"; do
jobinfo+=( "$( ps -p "$pid" -o args= )" )
done
# later...
err=0
i=0
for pid in "$joblist[@]"; do
if ! wait "$pid" ; then
printf 'Got non-zero exit status (%d) from PID %d: %sn'
"$code" "$pid" "$jobinfo[$i]"
err=1
fi
i=$(( i + 1 ))
done
exit "$err"
add a comment |Â
up vote
0
down vote
up vote
0
down vote
As pointed out, you can't use ps
on a non-existing process.
If you have a bash
that supports associative arrays:
declare -A jobinfo
# collect job information
joblist=( $(jobs -p) )
for pid in "$joblist[@]"; do
jobinfo["$pid"]="$( ps -p "$pid" -o args= )"
done
# later...
# wait for jobs
err=0
for pid in "$joblist[@]"; do
if ! wait "$pid" ; then
printf 'Got non-zero exit status (%d) from PID %d: %sn'
"$code" "$pid" "$jobinfo[$pid]"
err=1
fi
done
# exit 0 if all was good, exit 1 otherwise
exit "$err"
Here I collect all the necessary data before starting to wait for jobs to exit. I also wait for all jobs to finish so that the user gets information about not just the first failing job.
Without using associative arrays (should work in the default bash
on macOS):
# collect job information
joblist=( $(jobs -p) )
for pid in "$joblist[@]"; do
jobinfo+=( "$( ps -p "$pid" -o args= )" )
done
# later...
err=0
i=0
for pid in "$joblist[@]"; do
if ! wait "$pid" ; then
printf 'Got non-zero exit status (%d) from PID %d: %sn'
"$code" "$pid" "$jobinfo[$i]"
err=1
fi
i=$(( i + 1 ))
done
exit "$err"
As pointed out, you can't use ps
on a non-existing process.
If you have a bash
that supports associative arrays:
declare -A jobinfo
# collect job information
joblist=( $(jobs -p) )
for pid in "$joblist[@]"; do
jobinfo["$pid"]="$( ps -p "$pid" -o args= )"
done
# later...
# wait for jobs
err=0
for pid in "$joblist[@]"; do
if ! wait "$pid" ; then
printf 'Got non-zero exit status (%d) from PID %d: %sn'
"$code" "$pid" "$jobinfo[$pid]"
err=1
fi
done
# exit 0 if all was good, exit 1 otherwise
exit "$err"
Here I collect all the necessary data before starting to wait for jobs to exit. I also wait for all jobs to finish so that the user gets information about not just the first failing job.
Without using associative arrays (should work in the default bash
on macOS):
# collect job information
joblist=( $(jobs -p) )
for pid in "$joblist[@]"; do
jobinfo+=( "$( ps -p "$pid" -o args= )" )
done
# later...
err=0
i=0
for pid in "$joblist[@]"; do
if ! wait "$pid" ; then
printf 'Got non-zero exit status (%d) from PID %d: %sn'
"$code" "$pid" "$jobinfo[$i]"
err=1
fi
i=$(( i + 1 ))
done
exit "$err"
edited Mar 24 at 8:40
answered Mar 24 at 7:48
Kusalananda
102k13201317
102k13201317
add a comment |Â
add a comment |Â
up vote
0
down vote
If your OS (e.g. Linux) has a /proc directory, you can get every needed information from /proc/$PID
for PID in $(jobs -p); do
dosomethingwith /proc/$PID/ # please take a look at what it contents ;-)
done
add a comment |Â
up vote
0
down vote
If your OS (e.g. Linux) has a /proc directory, you can get every needed information from /proc/$PID
for PID in $(jobs -p); do
dosomethingwith /proc/$PID/ # please take a look at what it contents ;-)
done
add a comment |Â
up vote
0
down vote
up vote
0
down vote
If your OS (e.g. Linux) has a /proc directory, you can get every needed information from /proc/$PID
for PID in $(jobs -p); do
dosomethingwith /proc/$PID/ # please take a look at what it contents ;-)
done
If your OS (e.g. Linux) has a /proc directory, you can get every needed information from /proc/$PID
for PID in $(jobs -p); do
dosomethingwith /proc/$PID/ # please take a look at what it contents ;-)
done
answered Mar 24 at 8:55
Camion
62
62
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f433208%2fget-process-information-the-command-etc-from-pid%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password