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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:20:19+00:00 2026-05-30T08:20:19+00:00

I need a bash script that does the following: Starts a background process with

  • 0

I need a bash script that does the following:

  • Starts a background process with all output directed to a file
  • Writes the process’s exit code to a file
  • Returns the process’s pid (right away, not when process exits).
  • The script must exit

I can get the pid but not the exit code:

$ executable >>$log 2>&1 &
pid=`jobs -p`

Or, I can capture the exit code but not the pid:

$ executable >>$log;
# blocked on previous line until process exits
echo $0 >>$log;

How can I do all of these at the same time?

  • 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-30T08:20:21+00:00Added an answer on May 30, 2026 at 8:20 am

    The pid is in $!, no need to run jobs. And the return status is returned by wait:

    $executable >> $log 2>&1 &
    pid=$!
    wait $!
    echo $?  # return status of $executable
    

    EDIT 1

    If I understand the additional requirement as stated in a comment, and you want the script to return immediately (without waiting for the command to finish), then it will not be possible to have the initial script write the exit status of the command. But it is easy enough to have an intermediary write the exit status as soon as the child finishes. Something like:

    sh -c "$executable"' & echo pid=$! > pidfile; wait $!; echo $? > exit-status' &
    

    should work.

    EDIT 2

    As pointed out in the comments, that solution has a race condition: the main script terminates before the pidfile is written. The OP solves this by doing a polling sleep loop, which is an abomination and I fear I will have trouble sleeping at night knowing that I may have motivated such a travesty. IMO, the correct thing to do is to wait until the child is done. Since that is unacceptable, here is a solution that blocks on a read until the pid file exists instead of doing the looping sleep:

    { sh -c "$executable > $log 2>&1 &"'
    echo $! > pidfile
    echo   # Alert parent that the pidfile has been written
    wait $!
    echo $? > exit-status
    ' & } | read
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a bash script that goes trough a text file, finds lines starting
I need to set a system environment variable from a Bash script that would
I got a variable in a bash script that I need to replace. The
I am working in a bash script where I need to analyze the output
I need to run a Bash script that can echo a 300 lines of
I need some help with a Bash script that will automatically add the git's
Does anyone know how to write a bash script that will pull the time
I need to create a bash script that will work on a mac. It
I'm creating Unix bash script that parses web-server log file and inserts this data
I need a bash script that lists files recursively in a directory in the

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.