The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

run linux .sh scripts from anywhere ?

Discussion in 'General Discussion' started by Valuehosted, Jul 24, 2010.

  1. Valuehosted

    Valuehosted Well-Known Member

    Joined:
    Dec 12, 2002
    Messages:
    124
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Sweden
    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
     
  2. dalem

    dalem Well-Known Member
    PartnerNOC

    Joined:
    Oct 24, 2003
    Messages:
    2,577
    Likes Received:
    40
    Trophy Points:
    48
    Location:
    SLC
    cPanel Access Level:
    DataCenter Provider
    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
     
  3. Valuehosted

    Valuehosted Well-Known Member

    Joined:
    Dec 12, 2002
    Messages:
    124
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Sweden
    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
     
  4. madaboutlinux

    madaboutlinux Well-Known Member

    Joined:
    Jan 24, 2005
    Messages:
    1,052
    Likes Received:
    2
    Trophy Points:
    38
    Location:
    Earth
    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

    then move the filename.sh.x version under the /usr/bin directory. That's it.
     
  5. WiredTree Joe

    WiredTree Joe Well-Known Member
    PartnerNOC

    Joined:
    Dec 13, 2006
    Messages:
    68
    Likes Received:
    1
    Trophy Points:
    8
    Location:
    Chicago, IL
    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.
     
  6. dalem

    dalem Well-Known Member
    PartnerNOC

    Joined:
    Oct 24, 2003
    Messages:
    2,577
    Likes Received:
    40
    Trophy Points:
    48
    Location:
    SLC
    cPanel Access Level:
    DataCenter Provider
    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
     
Loading...

Share This Page