Get process information (the command, etc) from pid

The name of the pictureThe name of the pictureThe name of the pictureClash 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.







share|improve this question


























    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.







    share|improve this question
























      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.







      share|improve this question














      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.









      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 24 at 13:55









      Jeff Schaller

      31.2k846105




      31.2k846105










      asked Mar 24 at 4:50









      Alexander Mills

      1,885929




      1,885929




















          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





          share|improve this answer




















          • 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 of jobs -p exits first.
            – Kusalananda
            Mar 24 at 7:09


















          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"





          share|improve this answer





























            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





            share|improve this answer




















              Your Answer







              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "106"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: false,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );








               

              draft saved


              draft discarded


















              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






























              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





              share|improve this answer




















              • 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 of jobs -p exits first.
                – Kusalananda
                Mar 24 at 7:09















              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





              share|improve this answer




















              • 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 of jobs -p exits first.
                – Kusalananda
                Mar 24 at 7:09













              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





              share|improve this answer












              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






              share|improve this answer












              share|improve this answer



              share|improve this answer










              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 of jobs -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










              • 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 of jobs -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













              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"





              share|improve this answer


























                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"





                share|improve this answer
























                  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"





                  share|improve this answer














                  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"






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 24 at 8:40

























                  answered Mar 24 at 7:48









                  Kusalananda

                  102k13201317




                  102k13201317




















                      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





                      share|improve this answer
























                        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





                        share|improve this answer






















                          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





                          share|improve this answer












                          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






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Mar 24 at 8:55









                          Camion

                          62




                          62






















                               

                              draft saved


                              draft discarded


























                               


                              draft saved


                              draft discarded














                              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













































































                              Popular posts from this blog

                              How to check contact read email or not when send email to Individual?

                              Displaying single band from multi-band raster using QGIS

                              How many registers does an x86_64 CPU actually have?