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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:14:21+00:00 2026-05-22T19:14:21+00:00

Getting a background process ID is easy to do from the prompt by going:

  • 0

Getting a background process ID is easy to do from the prompt by going:

$ my_daemon &
$ echo $!

But what if I want to run it as a different user like:

su - joe -c "/path/to/my_daemon &;"

Now how can I capture the PID of my_daemon?

  • 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-22T19:14:22+00:00Added an answer on May 22, 2026 at 7:14 pm

    Succinctly – with a good deal of difficulty.

    You have to arrange for the su’d shell to write the child PID to a file and then pick the output. Given that it will be ‘joe’ creating the file and not ‘dex’, that adds another layer of complexity.

    The simplest solution is probably:

    su - joe -c "/path/to/my_daemon & echo \$! > /tmp/su.joe.$$"
    bg=$(</tmp/su.joe.$$)
    rm -f /tmp/su.joe.$$   # Probably fails - joe owns it, dex does not
    

    The next solution involves using a spare file descriptor – number 3.

    su - joe -c "/path/to/my_daemon 3>&- & echo \$! 1>&3" 3>/tmp/su.joe.$$
    bg=$(</tmp/su.joe.$$)
    rm -f /tmp/su.joe.$$
    

    If you’re worried about interrupts etc (and you probably should be), then you trap things too:

    tmp=/tmp/su.joe.$$
    trap "rm -f $tmp; exit 1" 0 1 2 3 13 15
    su - joe -c "/path/to/my_daemon 3>&- & echo \$! 1>&3" 3>$tmp
    bg=$(<$tmp)
    rm -f $tmp
    trap 0 1 2 3 13 15
    

    (The caught signals are HUP, INT, QUIT, PIPE and TERM – plus 0 for shell exit.)

    Warning: nice theory – untested code…

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

Sidebar

Related Questions

Getting the subdomain from a URL sounds easy at first. http://www.domain.example Scan for the
Following the advice from my previous question , I placed my background process in
Getting started with jquery and having trouble getting hello world type example going for
Im getting a string (simplified) from the backend that should be : { menu:
Anyone getting this error when using the new free chart controls MS bought from
Background My business is getting more and more requests for web development and as
I've got a background process that makes a transparent window appear when a hotkey
If i have file for eg: a log file created by any background process
Our application has a background thread which spawns a process through System.Diagnostics.Process : Process.Start(
Background: I'm writing an installation for a SharePoint component. In addition to getting 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.