Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 918317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:21:18+00:00 2026-05-15T18:21:18+00:00

I am trying to do some process control in a script (bash). For this

  • 0

I am trying to do some process control in a script (bash). For this reason I output the process group id of the current process to a file.

ps ax -o '%p %r'|perl -ne 'if ($_ =~ /\s*$$\s*(\d+)/) { print "$1"; exit; }' > $pgidfile
nohup $JAVA_HOME/bin/java -cp somejar.jar some.java.Program & > /dev/null 2>&1

I have also tried:

ps ax -o '%p %r'|awk '/'"$$"' [0-9]+/ {print $2}' > $pgidfile
nohup $JAVA_HOME/bin/java -cp somejar.jar some.java.Program & > /dev/null 2>&1

However in both these cases, the file (in $pgidfile) seems to be empty. (Although on certain rare occasions it does seem to have the correct value.) Also, just running the commands(to output the process group id – option 1 or option 2 above) themselves on the commandline seem to do the right thing.

It would be great if someone could suggest a solution to the above problem or answer either (or both) of the following questions:

1) What is the suggested way to get a process’s group id in a shell or perl script?

2) Does running a command under nohup change the output redirection of previous/subsequent commands that aren’t related to the command executed with nohup?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:21:20+00:00Added an answer on May 15, 2026 at 6:21 pm

    1) What is the suggested way to get a process’s group id in a shell or perl script?

    An improvement to your current approach would be to do an exact field match on the PID you care about. Any unanchored regex technique (such as your original post) runs the risk of matching the wrong thing. If $$ happens to be 123, you’ll match ps output lines for PIDs 1123, 1234, etc.

    Now, awk and perl are both very good at processing field delimited input (see perl’s -a switch), but you don’t need to do that, since you can limit ps to just the process you care about:

    $ ps -o '%r' $$ | tail -n 1 > $pgfile
    

    Find out how your ps can be told to omit headers, and you can remove tail from that pipeline.

    2) Does running a command under nohup change the output redirection of previous/subsequent commands that aren’t related to the command executed with nohup?

    No. Something else is amiss in your scenario.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 486k
  • Answers 486k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer System.Math.Truncate (102.123456789 * factor) / factor; In your case factor… May 16, 2026 at 7:54 am
  • Editorial Team
    Editorial Team added an answer You are setting ctx2 to layer1's context: ctx2 = canvas.getContext("2d");… May 16, 2026 at 7:54 am
  • Editorial Team
    Editorial Team added an answer I resorted to writing a little command line utility myself. May 16, 2026 at 7:54 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.