Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have looked around as I would've thought this would be a rather common question, but nothing seems to be working for me.

Basically I'm trying to SSH into a device (Raspberry Pi) to run a process. The process is node ./bin/www. I wrote a publish.sh script that does a few things before this, but when I run the command ssh user@hostname "cd <my-location>;node ./bin/www it respectively starts the process in my current bash shell.

What I'm looking to do is open up a new bash shell to run that command, as that process will start a web server.

I tried wrapping my ssh command in bash -c 'ssh ...' but it still runs in the same shell window that the publish.sh script was kicked off in. Am I doing something wrong? What exactly do I need to do in my shell script to run a command in a new bash shell process?

Thanks!

share|improve this question
    
What is your purpose in running this in a new shell? Are you hoping to log off and leave it running after? If so, you need to do more than just run it in a separate shell, so if so, you should tell us these details. :) – SevenSidedDie Mar 8 at 21:05
    
@SevenSidedDie nope, I just want my current shell to not be "hung up" on running that other process. – Thomas Stringer Mar 8 at 21:15
    
I may be failing to properly imagine what you're doing, but I don't think this will accomplish that either, because the new shell also takes over your terminal until it quits. Are you just wanting to run the server detached from your terminal? – SevenSidedDie Mar 8 at 21:18
    
@SevenSidedDie maybe. So I have a bash terminal open and I'm doing "things". Then I want to SSH into another machine and run a web server (which, will run indefinitely until I kill it). But I want to keep doing "other things" in the shell as well. But if the web server runs in my shell I would have to open up another terminal. I'd rather just have a command to start the web server in a new shell process. Make sense? – Thomas Stringer Mar 8 at 21:23
    
Makes sense to do, yes! And what you've asked for won't (unless I'm seriously out to lunch at the moment) do the job. You should put that description of your goal into your question and solicit new answers. IIRC (I'm not in reach of a terminal at the moment), starting a new shell process will still prevent you from doing other things in the current terminal because it takes over as the “foreground” process. – SevenSidedDie Mar 8 at 21:26
up vote 5 down vote accepted

You can use ssh with -n option as,

ssh -Xn user@hostname <command> <argument>

Use of -X is optional to forward X11. In your case,

ssh -n user@hostname node ./bin/www

Example: To open a text file (file.txt) in remote machine with gedit,

ssh -Xn user@hostname gedit file.txt

From man ssh

-n      A common trick is to use this to run X11 programs on a remote machine.  For example,
         ssh -n shadows.cs.hut.fi emacs & will start an emacs on shadows.cs.hut.fi, and the X11 connection will be
         automatically forwarded over an encrypted channel.  The ssh program will be put in the background.
share|improve this answer
1  
Also see the -f option, in case you want to use password authentication. – muru Mar 8 at 16:34
    
That doesn't seem to be opening a new bash shell/process to run that command. It's still running it in the same window when I do this. – Thomas Stringer Mar 8 at 17:16
    
yes it will run in the same window but does create new process. – souravc Mar 8 at 17:22
    
@souravc ah ok! I think that works for me, thanks! – Thomas Stringer Mar 8 at 17:31

To do this using shell:

ssh -t master 'bash -ic "{ cd <my-location>; node ./bin/www ;} &"'

A bit strict using disown (nohup can be used too):

ssh -t master 'bash -ic "{ cd <my-location>; node ./bin/www ;} & disown"'
  • -t will allocate a pseudo TTY to enable job control

  • bash -i will make the shell interactive, otherwise the connection to the master will be closed

In your case, you can get away without the command grouping {}:

ssh -t master 'bash -ic "cd <my-location>; node ./bin/www &"'
share|improve this answer
    
(1) Maybe I’m just being picky, but I find { …;} & to be misleading/confusing.  Why not use the more familiar ( … ) &?  (2) Are you sure you need the -t?  You definitely need to have a TTY to do Ctrl+Z, and you need job control to do fg and bg, but I believe that you can always do &.  (3) Are you sure you need the -i?  What happens if you don’t use it? – G-Man Mar 8 at 18:32
    
@G-Man 1) that's because I don't see the point of spawning an unnecessary subshell 2)no, backgrounding is a job control method, try it yourself 3)check my answer ....the other answer is always better, the shell way is just for the sake of showing how this can be done using shell internals.. – heemayl Mar 9 at 1:05
    
(1) OK, I know that (…) creates a subshell and {…} doesn’t.  My point is that (cmd₁; cmd₂) & and { cmd₁; cmd₂;} & both create subshells.  That’s why I called {…} & “misleading” — it looks like it uses one fewer process, but it doesn’t.  (2) Well, I don’t have access to a system that I can ssh into right now, so I ran some at jobs.  That’s pretty much the same thing, right? — running sh (dash) with no tty.  And I was able to put a command into the background with &.  Anything else you want me to try?  … (Cont’d) – G-Man Mar 9 at 5:03
    
(Cont’d) …  (3) I’m sorry, but I have absolutely no idea what you’re saying here.  Are you saying that souravc’s answer is better than yours, and you posted yours just for didactic purposes? – G-Man Mar 9 at 5:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.