How to run .sh (shell script) and ignore user abort ?

Discussion in 'Site & Server Administration' started by deriklogov, Aug 7, 2012.

  1. #1
    Hi,

    Is there any way to run .sh script from ssh and in case if I loose connection script will still be working until finish ?

    Thank You
     
    deriklogov, Aug 7, 2012 IP
  2. shindig

    shindig Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Yes. The easiest way would be to use the Linux program called 'screen' (assuming it's installed). You start a screen session, run the command, and then detach the screen session. The commands you entered while in the screen session will continue to run. When you log back into SSH, you can simply re-attach that screen session.

    You could also use commands like nohup but that's more technical to understand.
     
    Last edited: Aug 7, 2012
    shindig, Aug 7, 2012 IP
  3. madaboutlinux

    madaboutlinux Member

    Messages:
    250
    Likes Received:
    7
    Best Answers:
    2
    Trophy Points:
    43
    #3
    Right, you can use screen to execute such commands. To install screen

    # yum install screen

    to start the screen session

    # screen

    execute your command there and detach the screen session, press

    Ctrl+a+d

    to re-attach to screen, use

    # screen -r

    Another method, is to execute the command in the background by adding '&' ampersand at the end

    # sh filename.sh &
     
    madaboutlinux, Aug 8, 2012 IP
  4. lucas03

    lucas03 Member

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #4
    if you add ampersand and close connection process will end as well.
    I used cron to run scripts while I am offline, but I may try "screen" as well. Thanks.
     
    lucas03, Aug 9, 2012 IP