Search code examples
bashapacheubuntu-14.04bootinotify

Run looping bash script simultaneously with server?


I have a bash script that uses inotifywait to upload any file moved into a directory in an Apache server. In my old set up, Apache would startup on boot; I would manually open a terminal, run the script and then minimize that terminal while it runs/monitors indefinitely.

I would like both apache and the script to start on bootup so that I can turn on my machine, and have the process not require any further input.

I've tried moving the script into /etc/init.d and running

sudo chmod +x myscript.sh
sudo update-rc.d myscript.sh defaults

in order to get it to run on boot.

Now it seems like Apache is not starting on bootup like it used to. I try to navigate to localhost in firefox and the page can't load. After manually starting the server, everything works fine, but now I'm just manually starting the server instead of the script. And if I

pgrep -fl myscript.sh

I see instances of the script running.

What is happening? Why does the script running on boot interfere with apache? The only interaction the script has with apache is monitoring a certain directory for creation of a new file. Certainly none of the files are changing on bootup?

I am a noob to this whole process, I have only gotten this far through extensive googling, but I can't find anything about what I should do next. Thanks


Solution

  • I believe you are using the pre Ubuntu 6.10 instructions for bootup customization.

    You have a couple of options. My suggestion would be to use rc.local.

    From The Debian GNU/Linux FAQ:

    The rc.local script is executed at the end of each multiuser runlevel. In Debian it is configured to do nothing. This provides customisation of the boot process, but might not be sufficient for all situations.

    So all you need to do is edit /etc/rc.local to call myscript.sh and you should be set.

    Alternatively, you can create an upstart compatible service file and enable it.

    The ubuntu bootup howto has some instructions for writing services:

    The most current reference for job/service definition is available in the man page for init, available by running man 5 init. There are also some very useful pointers in The Upstart Cookbook.

    Here is an example of a simple upstart job config: /etc/init/myservice.conf

    # myservice - myservice job file
    
    description "my service description"
    author "Me <myself@i.com>"
    
    # Stanzas
    #
    # Stanzas control when and how a process is started and stopped
    # See a list of stanzas here: http://upstart.ubuntu.com/wiki/Stanzas#respawn
    
    # When to start the service
    start on runlevel [2345]
    
    # When to stop the service
    stop on runlevel [016]
    
    # Automatically restart process if crashed
    respawn
    
    # Essentially lets upstart know the process will detach itself to the background
    expect fork
    
    # Run before process
    pre-start script
        [ -d /var/run/myservice ] || mkdir -p /var/run/myservice
        echo "Put bash code here"
    end script
    
    # Start the process
    exec myprocess