Search code examples
linuxsolrsharedboot

How to start Solr automatically?


At the moment I have to go to /usr/java/apache-solr-1.4.0/example and then do:

java -jar start.jar

How do I get this to start automatically on boot?

I'm on a shared Linux server.


Solution

  • If you have root access to your machine, there are a number of ways to do this based on your system's initialization flow (init scripts, systemd, etc.)

    But if you don't have root, cron has a clean and consistent way to execute programs upon reboot.

    First, find out where java is located on your machine. The command below will tell you where it is:

    $ which java
    

    Then, stick the following code into a shell script, replacing the java path below (/usr/bin) with the path you got from the above command.

    #!/bin/bash
    
    cd /usr/java/apache-solr-1.4.0/example
    /usr/bin/java -jar start.jar
    

    You can save this script in some location (e.g., $HOME) as start.sh. Give it world execute permission (to simplify) by running the following command:

    $ chmod og+x start.sh
    

    Now, test the script and ensure that it works correctly from the command line.

    $ ./start.sh
    

    If all works well, you need to add it to one of your machine's startup scripts. The simplest way to do this is to add the following line to the end of /etc/rc.local.

    # ... snip contents of rc.local ...
    # Start Solr upon boot.
    /home/somedir/start.sh
    

    Alternatively, if you don't have permission to edit rc.local, then you can add it to your user crontab as so. First type the following on the commandline:

    $ crontab -e
    

    This will bring up an editor. Add the following line to it:

    @reboot /home/somedir/start.sh
    

    If your Linux system supports it (which it usually does), this will ensure that your script is run upon startup.

    If I don't have any typos above, it should work out well for you. Let me know how it goes.