1

I'm making a script to start up a number of services at server reboot. To do this, I'm looping over the directory, checking that each has a start.sh script, and calling that script if they do. However, as part of this, I would like to set up the script to ignore a service it has a select name, in this case mongo:

for service in $HOME/start/* ; do
    if [ -e $HOME/start/$service/start.sh ] && [ $service != mongo ]
        then
            cd $HOME/start/$service
            ./start.sh
    else
        pwd
        echo "No start script found for $service"
    fi
done

This is because mongo is started before this loop is called, as a prequesite for the services, and you cannot have more than one instance of it at a time. However, it continues to loop and call mongo. How can I get it to ignore this?

Edit: corrected appName to service

  • 3
    $service never equals mongo - it equals $HOME/start/mongo – steeldriver Feb 26 at 16:47
  • 2
    ... however it's a bit puzzling that -e $HOME/test/$service/start.sh tests true at all – steeldriver Feb 26 at 16:49
2

When you iterate over files with a glob the path will be added to the file. So if your file is just mongo (which it should be based on your code, if it is not that is also a problem), your loop will set service to /home/<YOUR USER>/start/mongo. You are then trying to see if that is not equal to mongo which it is not. You could use basename to fix that:

for s in "$HOME/start/"*
do
    service=$(basename "$s")
    if [ -f "$HOME/test/$service/start.sh" ] && [ "$service" != mongo ]
    then
            cd "$HOME/start/$service"
            ./start.sh
    else
            pwd
            echo "No start script found for $service"
    fi
done
| improve this answer | |
0

Might be an interesting case for "extended globbing / extended pattern matching" - if your shell (bash?) provides that:

$ shopt -s extglob
$ for s in "$HOME/start/"!(mongo); do echo $s; done
| improve this answer | |
0

Don't do it this way! Use the machinery to start up services in your distribution (presumably systemd today if Linux; other Unixy systems have their own endearing quirks...). Then use the distribution's mechanisms to enable/disable services to start.

Your distribution's documentation should give details. If you install the service from the distribution's packages, this should be taken care of automatically. And that is very highly recommended: the distribution (really: the maintainer of the package in it) will make sure the installation is done right, configured securely, and will keep an eye out for bug reports (and potential security risks, critical to consider if it is some sort of server, exposed to all sort of miscreants from all over the world).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.