July 2023

Archive page for July 2023 by Linux Bash
  • Posted on

    If you're suffering with server lag, ping latency, degraded system functionality or generally slow response times you may need to add extra RAM to your server.

    Of course, adding RAM is a solution - but it's not the only option you have. Why not think about utilising swap space, this enables you to let your HDD act as a virtual-backup solution to out-of-memory situations.

    These days of course using your HDD for RAM doesn't seem as bad as what it sounds, with SSD's widely used the temptation to switch to swap space is quite understandable.

    Plus, for most use-cases, you will only need as much as 2-4GB of RAM in order to compile your favourite popular software title - such as Apache, or similar. To simply run the program may require less RAM thus less overhead for cost. You could use the package manager to install Apache to avoid having to compile it but why do that when you a) want to compile and b) can simply add swap space.

    To add swap space, follow these instructions:

    sudo fallocate -l 1G /swapfile
    

    Here, we are creating a swap file with a size of 1G. If you need more swap, replace 1G with the desired size.

    A good rule of thumb is to have 3x more swap than your physical RAM. So for 2GB of RAM you would have 6GB of swap space.

    If the fallocate utility is not available on your system or you get an error message saying fallocate failed: Operation not supported, use the dd command to create the swap file:

    sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
    

    Next, set the swap space files permissions so that only the root user can read and write the swap file:

    sudo chmod 600 /swapfile
    

    Next, set up a Linux swap area on the file:

    sudo mkswap /swapfile
    

    Activate the swap by executing the following command:

    sudo swapon /swapfile
    

    And that should do it. Check the memory is allocated by simply using free.

    sudo free -h
    

    Finally, make the changes permanent by adding a swap entry in the /etc/fstab file:

    echo  '/swapfile swap swap defaults 0 0' >> /etc/fstab
    

    Now, when your server reboots the swap space will be reconfigured automatically.


    And that's it! If you enjoyed this post please feel free to leave a like or a comment using the messaging options below.

  • Posted on

    When it happens that your VPS is eating data by the second and there is disk read/write issues one port of call you are bound to visit is searching and identifying large files on your system.

    Now, you would have been forgiven for thinking this is a complicated procedure considering some Linux Bash solutions for fairly simple things, but no. Linux Bash wins again!

    du -sh /path/to/folder/* | sort -rh

    Here, du is getting the sizes and sort is organising them, -h is telling du to display human-readable format.

    The output should be something like this:

    2.3T    /path/to/directory
    1.8T    /path/to/other
    

    It does take a while to organise as it is being done recursively however given 3-5mins and most scenarios will be fine.

  • Posted on

    So yeah, getting used to Bash is about finding the right way to do things. However, learning one-liners and picking up information here and there is all very useful, finding something you don't need to Google in order to recall is extremely important.

    Take the case of recursive find and replace. We've all been there, it needs to be done frequently but you're either a) scared or b) forgetful. Then you use the same snippet from a web resource again and again and eventually make a serious error and boom, simplicity is now lost on you!

    So here it is, something you can remember so that you don't use different methods depending on what Google throws up today.

    grep -Rl newertext . | xargs sed -i 's/newertext/newesttext/g'

    Notice, we use grep to search -R recursively with results fed to xargs which does a simple sed replace with g global/infinite properties.

    Say you want to keep it simple though and find and review the files before doing the "simple" replace. Well, do this.

    grep -R -l "foo" ./*

    The uppercase -R denotes to follow symlinks and the ./* indicates the current directory. The -l requests a list of filenames matched. Neat.