Find the answer to your Linux question:
Results 1 to 4 of 4
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Exclamation Shell Script SSH


    Dear all,

    I am trying to write a shell script that would ssh to a machineX and after that it will give that machine some job to do. The problem is that I do not want my shell script to wait for machineX to finish it's tasks, but I want it to return ASAP from the shell.

    Here is the example of my shell script:

    _______________________________
    echo "Start"
    ssh -T machineX <<EOI
    echo " - I will make machineX to execute scriptX"

    ./scriptX >> result.txt

    echo " - I don't want to wait here... but the problem is that SSH waits here for scriptX"
    EOI
    echo "Stop"
    ______________________________

    I have tried for instance :

    ./scriptX >> result.txt &

    and this makes the script to run in background, but still is not helpful since SSH waits for it ....

    I think that I should make SSH not to wait for the tasks that is doing... but I don't know how.

    Thank you !
    Vio.

    P.S. I post this thread under "Redhat / Fedora Linux Help" forum as well, but I think the quiestion is more suitable for this forum.

  2. #2
    How do you put the job in the background. Are you familiar with the nohup command?

    You could try, after ssh-ing in do something like

    nohup <command> &
    exit
    I haven't tested this exact situation, but I think it will work.

  3. #3

    Thumbs up Foud the solution

    Thanks Scottro for your reply. I have tried that as well and it did not work....

    I just found out the answer to my quiestion but it took me several hours . So here you go :

    If you go on: http://www.openssh.org/faq.html#3.10 you'll see that there is a problem with the shell SSH who does not want to close if there is a thread running in background. There are 2 tricks showed there, anyways I have tried only the first one and it works fine. (I think that there is an error on their page and instead of "sleep 20 < /dev/null > /dev/null 2>&1 &" they should have "sleep 20 < /dev/null >&1 &" .....)

    So my new script (that works) is:

    _______________________________
    echo "Start"
    ssh -T machineX <<EOI
    echo " - I will make machineX to execute scriptX"

    ./scriptX </dev/null>&result.txt &

    echo " - I don't wait here... and the output of scriptX is redirected to result.txt"
    exit
    EOI
    echo "Stop"
    ______________________________

    Cheers,
    Vio

  4. $spacer_open
    $spacer_close
  5. #4
    Quote Originally Posted by Vio View Post
    I think that there is an error on their page and instead of "sleep 20 < /dev/null > /dev/null 2>&1 &" they should have "sleep 20 < /dev/null >&1 &" .....
    Actually Vio,
    "sleep 20 < /dev/null > /dev/null 2>&1 &" is correct.
    > /dev/null redirects stdout to the 'black hole' of /dev/null, while
    2>&1 redirects stderr to stdout

    So in other words
    > /dev/null 2>&1
    redirects all output to /dev/null

    Manos

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •