Auto-start a shell script on Ubuntu Server

Got a shell script that you want automatically run at bootup on Ubuntu Server Edition? Here’s how:

  1. Create a script in the /etc/init.d/ directory
  2. Make the script executable
    $ sudo chmod +x /etc/init.d/myscript.sh
  3. Make the script start at bootup
    $ sudo update-rc.d myscript.sh defaults

Note: the option “defaults” puts a link to start your script in runlevels 2, 3, 4 and 5, and puts a link to stop in runlevels 0, 1 and 6.

Referenced from:

13 thoughts on “Auto-start a shell script on Ubuntu Server

  1. DJ

    Hi,

    I want to use myscript.sh to start an application. after reboot, I found my application was indeed started (it has some log files), but it was somehow killed by the system. Do you know why?

  2. Jonathon Post author

    Are you running on a shared hosting service? Many of them will kill long-running scripts after a period of time.

  3. Jonathon Post author

    In that case, I’ll need to know more about your setup. Email me at jhill9693 at gmail dot com if you would like to continue the conversation.

  4. Prashant

    when i fire command sudo update-rc.d myscript.sh defaults
    then display the below error

    update-rc.d: warning: /etc/init.d/test.sh missing LSB information
    update-rc.d: see
    System start/stop links for /etc/init.d/test.sh already exist.

  5. Pingback: Node.js Dev Environtment » Note

  6. stephen

    I did that on Ubuntu 10.04 and it does not run the script. is there a log somewhere to show swhat is the problem? if I start it manually by executing sudo /etc/init.d/myscript.sh
    it runs fine.
    also there is a file /etc/rc.local by default and I changed the execution bits on that and still it does nothing.
    thanks for looking at this.

  7. Jonathon Post author

    Unfortunately I’m not familiar enough with Ubuntu (Debian) Linux internals to help you on that one, try Google :)

  8. recard

    How can I auto-start a shell script once-per-day (e.g. every day at 10:00 pm) under Ubuntu/Linux Server ?

  9. blizzz

    @recard the question is more than a year ago … but … you want to use a cron job for this. But I suppose you know it meanwhile ;)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>