Wednesday, July 5, 2017

Adding Commands to Linux PATH

If you know in which directory the command you want execute is, just type the path until .sh file and run it. This might be boring if the path is too long though. A more effective way is creating a script containing the whole command and add it to the environment variable PATH.

In order to know which directories are mapped by the PATH, run:
echo $PATH
A possible output is this:
/home/rafael/bin:/home/rafael/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8 oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin
The mapped directories are split by two points. Some directories are mapped by default. As you can see ~/bin (/home/rafael/bin) is one of these. This means that if we add some scripts in ~/bin, we will be able to run those scripts from anywhere by just prompting the file script name.

Example: Running Wildfly10 server through the PATH

Assuming your Wildfly server is located at /usr/share/wildfly-10.1.0.Final, let's add to PATH two commands, one to start the server and other to start the server client interface whose full path is /usr/share/wildfly-10.1.0.Final/bin/jboss-cli.sh.

We are just going to create one file called wildfly10 (touch wildfly10) to start the server and other called wildfly_cli (touch wildfly_cli) to run the client, each one containing the respective scripts (echo "..."). After that we give permission (chmod 755...) to make these files executable and that's all.

Starting from your home directory, type:
cd bin
touch wildfly10
echo "sudo /usr/share/wildfly-10.1.0.Final/bin/standalone.sh" > wildfly10
chmod 755 wildfly10
touch wildfly10_cli
echo "sudo /usr/share/wildfly-10.1.0.Final/bin/jboss-cli.sh" > wildfly10_cli
chmod 755 wildfly10_cli
Ready! Wildfly command is on your PATH. Let's try it:

First we start the server by prompting wildfly10

And now running the client server:

Prompting wildfly_cli from anywhere we start the client

And accessing server http port:

localhost:9990

You can add any commands you want to the PATH!


No comments:

Post a Comment