Results 1 to 9 of 9

Thread: Run script in background

  1. #1
    Join Date
    Sep 2007
    Location
    ~/
    Beans
    397
    Distro
    Ubuntu

    Run script in background

    Hey guys. How can i run my SH script in the background, like a daemon. Thanks in advance!
    Registered Ubuntu user #25321

  2. #2
    Join Date
    Mar 2008
    Beans
    4,714
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Run script in background

    If the script is called myscript.sh, then run
    Code:
    myscript.sh &

  3. #3
    Join Date
    Jan 2007
    Location
    $here ? $here : $there
    Beans
    3,717
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Run script in background

    If you run it from a terminal you will also need to use disown to keep it running after you close the terminal:

    Code:
    my_script.sh &
    disown
    exit
    Don't try to make something "fast" until you are able to quantify "slow".

  4. #4
    Join Date
    Mar 2008
    Beans
    4,714
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Run script in background

    Vor, thanks for pointing out the 'disown' command -- I didn't know about that. I've always used nohup. Is that the same?

    Also, just out of curiosity, I saved this script as 'test.sh':
    Code:
    #!/bin/sh
    while true ; do
        echo "Hi!" >> ~/test/a;
        sleep 2
    done
    And ran it from a gnome-terminal like this:
    Code:
    test.sh &
    I then closed the gnome-terminal, and looked inside ~/test/a. It was still printing 'Hi!'s!

    Then I logged out of my GNOME session, got to the GDM window, pressed Ctrl-Alt-F2, and logged in at a text terminal. The tesh.sh script was still printing more 'Hi!'s in ~/test/a!

    Is this the effect of some configuration option I don't know about, or is this normal?

  5. #5
    Join Date
    Apr 2006
    Location
    Montana
    Beans
    Hidden!
    Distro
    Kubuntu Development Release

    Re: Run script in background

    or nohup ...

    Code:
    nohup script.sh &
    There are two mistakes one can make along the road to truth...not going all the way, and not starting.
    --Prince Gautama Siddharta

    #ubuntuforums web interface

  6. #6
    Join Date
    Sep 2007
    Location
    Masschusetts
    Beans
    1,510
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Run script in background

    alt+f2 if you don't want to open a terminal to do it.
    add it to /etc/init.d and update init.d to run it on login
    Registered Linux User: 482377
    MacBook (1,1) - OS X Leopard
    ASUS P5LD2 - Core 2 Duo 3.0GHz - 2GB Corsair RAM - Nvidia 8800GT - Ubuntu 9.04 / Windows 7 Professional

  7. #7
    Join Date
    Apr 2006
    Location
    Montana
    Beans
    Hidden!
    Distro
    Kubuntu Development Release

    Re: Run script in background

    Quote Originally Posted by unutbu View Post
    Vor, thanks for pointing out the 'disown' command -- I didn't know about that. I've always used nohup. Is that the same?
    http://playingwithsid.blogspot.com/2...-commands.html
    There are two mistakes one can make along the road to truth...not going all the way, and not starting.
    --Prince Gautama Siddharta

    #ubuntuforums web interface

  8. #8
    Join Date
    Jan 2007
    Location
    $here ? $here : $there
    Beans
    3,717
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Run script in background

    Quote Originally Posted by unutbu View Post
    Vor, thanks for pointing out the 'disown' command -- I didn't know about that. I've always used nohup. Is that the same?

    Also, just out of curiosity, I saved this script as 'test.sh':
    Code:
    #!/bin/sh
    while true ; do
        echo "Hi!" >> ~/test/a;
        sleep 2
    done
    And ran it from a gnome-terminal like this:
    Code:
    test.sh &
    I then closed the gnome-terminal, and looked inside ~/test/a. It was still printing 'Hi!'s!

    Then I logged out of my GNOME session, got to the GDM window, pressed Ctrl-Alt-F2, and logged in at a text terminal. The tesh.sh script was still printing more 'Hi!'s in ~/test/a!

    Is this the effect of some configuration option I don't know about, or is this normal?
    It must be a configuration option. I just tried your script and it stopped printing when I closed the terminal.
    Don't try to make something "fast" until you are able to quantify "slow".

  9. #9
    Join Date
    Mar 2008
    Beans
    4,714
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Run script in background

    Any ideas where I should look to find out why my terminal has nohup/disown always on?

    I checked with pstree and found that when I run test.sh its parent starts as bash (spawned by gnome-terminal), but when gnome-terminal is closed, test.sh's parent jumps to init.

    I checked my .bashrc and .profile, and nothing jumped out at me as the culprit -- though I'm not great at understanding shell stuff.

    I also am researching shopt, but I'm having trouble even finding documentation on what all the options mean.

Bookmarks

Posting Permissions

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