Community Forums
Connect with us on LinkedIn
+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    Member Valuehosted's Avatar
    Join Date
    Dec 2002
    Location
    Sweden
    Posts
    124

    Default run linux .sh scripts from anywhere ?

    I have an odd question that I am hoping one of our more Linux capable members could toss me an answer to.

    I have certain commands that I like to run sometimes like:

    netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr |more

    that displays the number of connections to my server right now for instance; but that is a pretty long thing to remember so I end up having to do a history and cut and paste...

    Is there anyway that I could create a bash script which would run the command above and put it in a place so I don't need to include a path to the command just type servconnect.sh for instance and it would run that script - kind of like making my own "linux commands".

    I would greatly appreciate your thoughts and help on this.

    Kind Regards,
    Tony
    Joomla Views - Joomla views and news.
    WordPress Views - WordPress views and news (to be launched)

  2. #2
    cPanel Partner NOC cPanel Partner NOC Badge
    Join Date
    Oct 2003
    Posts
    1,931

    Default

    nano -w myscript.sh

    put

    netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr |more

    inside it
    cntl x save

    run i it

    sh myscript.sh
    Lowest Host/Empire Technology LLC
    Affordable hosting solutions http://empire-hosting.net
    List Your hosting site FREE in http://hostgeneration.com

  3. #3
    Member Valuehosted's Avatar
    Join Date
    Dec 2002
    Location
    Sweden
    Posts
    124

    Default

    Thank you Dalem, that did the trick!

    I put these in /usr/bin/ is that ok or is it a NONO? This way I can type the commands from anywhere and none of them are server critical - just shorter versions of commands that I use on a daily basis.

    Kind Regards,
    Tony
    Joomla Views - Joomla views and news.
    WordPress Views - WordPress views and news (to be launched)

  4. #4
    Member
    Join Date
    Jan 2005
    Location
    Earth
    Posts
    1,050

    Default

    The .sh script won't work directly from the /usr/bin. In such a case, you have to use the complete path to the file. However, there is a workaround for this

    Once you create a .sh file and put all your commands in it (don't forget to include the shebang at the start of the .sh file), encrypt the file using the "shc" tool which will create a binary file of your .sh file as .sh.x (binary file). You can now move the binary file under /usr/bin directory and execute the file as a command from any directory of your server.

    With shebang I mean the following line at the start of the .sh file

    #!/bin/bash

    You have to download the "shc" tool from the net and install it. Once done, encrypt the .sh file by executing

    shc -f filename.sh
    then move the filename.sh.x version under the /usr/bin directory. That's it.

  5. #5
    cPanel Partner NOC cPanel Partner NOC Badge WiredTree Joe's Avatar
    Join Date
    Dec 2006
    Location
    Chicago, IL
    Posts
    68

    Default

    Quote Originally Posted by Valuehosted View Post
    Thank you Dalem, that did the trick!

    I put these in /usr/bin/ is that ok or is it a NONO? This way I can type the commands from anywhere and none of them are server critical - just shorter versions of commands that I use on a daily basis.
    I normally create a bin directory in my user's home directory for my homemade scripts, that are specific to my needs, and then add that to my path. Add the following to your .bashrc:

    Code:
    export PATH="/bin:/sbin:/home/USERNAME/bin:$PATH"
    make sure you replace USERNAME with your linux username. Once that is done type:

    source ~/.bashrc

    to set the new path. Then you can create your bin directory:

    Code:
    cd ~
    mkdir bin
    cd bin
    touch myscript
    chmod 770 myscript
    vim myscript
    I create my scripts with:

    Code:
    #!/bin/bash
    at the top so I don't have to type sh in front of the command. For example:

    Code:
    #!/bin/bash
    netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr |more
    You should be able to then call:

    Code:
    myscript
    from anywhere on CLI and it will exec your commands inside. Just don't name your commands the same as other commands in /bin, /sbin etc etc.

    Hope that helps.
    Joe Doss
    www.wiredtree.com
    Fully Managed Dedicated Servers and VPS Hosting

  6. #6
    cPanel Partner NOC cPanel Partner NOC Badge
    Join Date
    Oct 2003
    Posts
    1,931

    Default

    all are good the above are good suggestions
    how ever

    how ever I just put them in a custom folder for later use and change directory to it

    or just chmod it to 750

    i you want to just run one command

    /myscripts/directory/something.sh
    Lowest Host/Empire Technology LLC
    Affordable hosting solutions http://empire-hosting.net
    List Your hosting site FREE in http://hostgeneration.com

Similar Threads & Tags
Similar threads

  1. Any user can run /scripts ??
    By dexus in forum cPanel and WHM Discussions
    Replies: 2
    Last Post: 01-12-2009, 01:54 AM
  2. How to run scripts?
    By SXR1337 in forum New User Questions
    Replies: 3
    Last Post: 05-05-2007, 10:13 AM
  3. some pl scripts only run with -w
    By bernieberg in forum cPanel and WHM Discussions
    Replies: 4
    Last Post: 04-09-2006, 12:33 PM
  4. Need to run own scripts as root
    By metal_cd in forum cPanel and WHM Discussions
    Replies: 6
    Last Post: 10-03-2005, 02:18 AM
Linkedin       Facebook       Twitter       RSS       Flickr       YouTube