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 3211662
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T14:45:25+00:00 2026-05-17T14:45:25+00:00

I’ve searched for a while but i can’t either find an answer or come

  • 0

I’ve searched for a while but i can’t either find an answer or come up with a solution of my own, so I turn to you guys. First question I actually ask here 🙂

I would like to run several instances of the same program, and redirect each of these programs’ standard output to a file that contains that same process’ pid, something like:

my_program > <pid of the instance of my_program that is called in this command>.log

I’m aware that this is not even close of the way to go 😛 I have tinkered around with exec and $PPID but to no avail. My bash-fu is weak 😐 please help me, point me somewhere! Thanks!

  • 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-17T14:45:26+00:00Added an answer on May 17, 2026 at 2:45 pm

    The problem here is that each new process started by bash gets new PID and you have to redirect the output of that process before starting it. But you cannot tell what PID will be assigned to that process by OS.

    The solution to this problem is not to start a new process, but replace the existing bash process with a new one using exec.

    Here is an example. First, we write a basic C program that prints its PID:

    // printpid.c
    
    #include <sys/types.h>
    #include <unistd.h>
    #include <stdio.h>
    
    int main()
    {
        printf ("C process pid is %d\n", getpid());
        return 0;
    }
    

    Then we write a simple bash script that will print its PID and replace itself with this program using exec:

    #!/bin/bash
    # printpid.sh
    
    echo Bash process PID is $$
    exec ./printpid > $$.log
    

    Now, let’s write a script that will call this printpid.sh script multiple times:

    #!/bin/bash
    # example.sh
    
    ./printpid.sh
    ./printpid.sh
    ./printpid.sh
    

    Now, let’s make sure it works:

    $ ls
    example.sh  printpid  printpid.c  printpid.sh
    $ ./example.sh 
    Bash process PID is 6397
    Bash process PID is 6398
    Bash process PID is 6399
    $ ls
    6397.log  6398.log  6399.log  example.sh  printpid  printpid.c  printpid.sh
    $ cat 6397.log 
    C process pid is 6397
    $ cat 6398.log 
    C process pid is 6398
    $ cat 6399.log 
    C process pid is 6399
    $ 
    

    Be aware that when you are using exec you cannot put anything else after that in the script as bash shell replaces itself with a new process specified as command line arguments for exec.

    Good luck hacking!

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

Sidebar

Related Questions

No related questions found

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.