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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:40:03+00:00 2026-05-16T15:40:03+00:00

NOTE: I thought I was using bash, but /bin/sh is linked to /bin/dash, which

  • 0

NOTE: I thought I was using bash, but /bin/sh is linked to /bin/dash, which has this weird exec problem.

I have a simple bash shell script on Linux which is used to launch a server process (which I did not write or control) and would like to have the bash shell script output the launched process’s PID to a pidfile.

The problem is that bash-exec command does not replace its own process with the launched server process!

So:

echo $$ | cat > /var/run/launched-server.pid

This does not work because the pid in the file will be that of bash not the server process. And if the server process exits bash may not exit leaving the stupid launch script hanging out in the process list.

Does anyone know a way to use bash (or dash maybe?) so that:

  1. Is it possible?
  2. The launched-server server process’s PID will be in my pidfile?
  3. Ensure that the bash script will die when the launched-server exists and not leave defunct shell processes hanging out in the process list?

Edit:
This snippet from the bash reference manual be of use…

exec
           exec [-cl] [-a name] [command [arguments]]

If command is supplied, it replaces the shell without creating a new process. If the -l option is supplied, the shell places a dash at the beginning of the zeroth argument passed to command. This is what the login program does. The -c option causes command to be executed with an empty environment. If -a is supplied, the shell passes name as the zeroth argument to command. If no command is specified, redirections may be used to affect the current shell environment. If there are no redirection errors, the return status is zero; otherwise the return status is non-zero.


Edit 2:
This is the script (sanitized):

#!/bin/sh

# this is where the server expects to run its services for Daemontools
SERVICE_DIR='/var/service';

# kill stdout, stderr, stdin
exec </dev/null
exec >/dev/null
exec 2>/dev/null

logger -ip daemon.debug -- stdout, stderr, stdin redirected to /dev/null

if [ -d $SERVICE_DIR ]; then
    # sanitized...
    logger -ip daemon.debug -- services and supervisors exited
else
    logger -ip daemon.err -- $SERVICE_DIR does not exist, exiting
    exit 1;
fi

if [ -d /var/run/pid ]; then
    echo $$ | cat > /var/run/pid/launched-server.pid
    logger -ip daemon.debug -- creating launched-server pidfile
fi

# start the server process
logger -ip daemon.info -- launching server on $SERVICE_DIR
exec /usr/local/bin/launched-server

And some ps output to maybe be more clear?

me@chainsaw: ~/dev $ ps ax | grep launched-server
13896 pts/1    S+     0:00 /bin/sh ./server_boot
13905 pts/1    S+     0:00 launched-server /var/service
13938 pts/2    R+     0:00 grep --color=auto launched-server
  • 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-16T15:40:03+00:00Added an answer on May 16, 2026 at 3:40 pm

    Does your distribution include start-stop-daemon(8)? Wonderfully useful little tool, it was built specifically for starting daemons from shell scripts. (Of course, the daemons outlive the shell scripts, so it may not be a perfect match — it depends on why you want the shell to outlive your daemon.)

    Or, for something simpler:

    Could your problem be solved with bash’s exec command? It replaces the shell process with whatever program you ask to execute:

    #!/bin/bash
    
    echo $$ > /tmp/pidfile
    exec /bin/sleep 60
    
    $ ./show_exec.sh 
    [nothing happens]
    

    And, in another shell:

    $ cat pidfile
    24686
    $ ps auxw | grep 24686
    sarnold  24686  0.0  0.0   9728   816 pts/1    S+   04:53   0:00 /bin/sleep 60
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(Note: I thought about posting this to serverfault, but I figured more developers have
I thought about using NSUserDefaults to store a single image, but i know this
NOTE: This is a followup to my question here. I have a program that
Note: I understand this is not strictly about development, but in the earlier threads
I'm using php to invoke gpg, but I'm getting a pipe error. I thought
I’m using Drupal (please note that this is not a Drupal related question), and
I run across this problem frequently suppose I have a text file that I
I have been trying to find a solution for this for a while but
I have an EditorGrid in which I'm binding data using XML. One of the
I thought I knew how to do this, but obviously not so I'd appreciate

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.