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

  • Home
  • SEARCH
  • 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 8617573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:53:34+00:00 2026-06-12T05:53:34+00:00

I want to start a process using nohup on a remote machine via ssh.

  • 0

I want to start a process using nohup on a remote machine via ssh. The problem is how to get the PID of the process started with nohup, so the “process actually doing something”, not some outer shell instance or the like. Also, I want to store stdout and stderr in files, but that is not the issue here…

Locally, it works flawlessly using

nohup sleep 30 > out 2> err < /dev/null & echo $!

It is echoing me the exact PID of the command “sleep 30”, which I can also see using “top” or “ps aux|grep sleep”.

But I’m having trouble doing it remotely via ssh. I tried something like

ssh remote_machine 'nohup bash -c "( ( sleep 30 ) & )" > out 2> err < /dev/null'

but I cannot figure out where to place the “echo $!” so that it is displayed in my local shell. It is always showing me wrong PIDs, for example the one of the “bash” instance etc.

Has somebody an idea how to solve this?

EDIT:
OK, the “bash -c” might not be needed here. Like Lotharyx pointed out, I get the right PID just fine using

ssh remote 'nohup sleep 30 > out 2> err < /dev/null & echo $!'

but then the problem is that if you substitute “sleep 30” with something that produces output, say, “echo Hello World!”, that output does not end up in the file “out”, neither on the local nor on remote side. Anybody got an idea why?

EDIT2: My fault! There was just no space left on the other device, that’s why the files “out” and “err” stayed empty!

So this is working. In addition, if one wants to call multiple commands in a row, separated by a semicolon (;), one can still use “bash -c”, like so:

ssh remote 'nohup bash -c "echo bla;sleep 30;echo blupp" > out 2> err < /dev/null & echo $!'

Then it prints out the PID of the “bash -c” on the local side, which is just fine. (It is impossible to get the PID of the “innermost” or “busy” process, because every program itself can spawn new subprocesses, there is no way to find out…)

  • 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-06-12T05:53:35+00:00Added an answer on June 12, 2026 at 5:53 am

    I tried the following (the local machine is Debian; the remote machine is CentOS), and it worked exactly as I think you’re expecting:

        ~# ssh someone@somewhere 'nohup sleep 30 > out 2> err < /dev/null & echo $!'
        someone@somewhere's password:
        14193
        ~#
    

    On the remote machine, I did ps -e, and saw this line:

        14193 ?        00:00:00 sleep
    

    So, clearly, on my local machine, the output is the PID of “sleep” executing on the remote machine.

    Why are you adding bash to your command when sending it across an SSH tunnel?

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

Sidebar

Related Questions

i want to call the command prompt command using Process.Start and then using StandardOutput
I'm trying to start the del command using System.Diagnostic.Process. Basically I want to delete
I want to write an application that launches another app using Process.Start , but
I'm executing 3 exes using Process.Start() in my C# application. I want to run
Here's what I'm doing. I have a loop that fires 20 processes using Process.Start().
Is there a way to start a process using ssh that doesn't terminate when
I am using python 2.7 multiprocessing module.I want to start a process and then
I want to start a background process in a Java EE (OC4J 10) environment.
I want to be able to start a process and then be able to
I want to start automating more of my web development process so I'm looking

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.