Automatic emails using bash and sendgrid

Problem

You would like to be able to automatically send some emails (ie reports) from a linux server, using a bash script that runs in cronjob, without installing an email server on the linux server.

Solution

  • Create an account in Sendgrid and follow the directions for using WebAPI with curl
    here
  • Set up your bash script to use the API key and have a script like the following. If you want to use big or multiple files you will need to use a temporary file for the base64 encoding, as in the example below, as there is a limit in curl.
  • Add your script to crontab
  • ERROR: Couldn’t connect to Docker daemon at http+docker://localunixsocket – is it running?

    Problem

    Using docker-compose up (or build), displays the following error message (even though the same command used to work previously):

    Solution

    This is quite possible caused by permissions issue, as one of the folders files that docker is trying to use is owned by a different user/group from the one trying to use the docker-compose commands. Try to find the file/folder with the different permissions and change it to your user name and group, or use change the files by using something like:

    Copying files between two folders without overwriting existing ones in destination folder.

    Problem

    You would like to copy the contents of a source directory to a destination directory without overwriting existing files on the destination.

    Solution

    You can use

    and you can also use the –dry-run option to test before running it.

    Bash getting the parent folder name from a bash defined variable

    Problem

    You have defined a variable in a bash script (ie MY_FOLDER) but you also want to dynamically get the parent folder (ie MY_FOLDER can have multiple values).

    Solution

    Use the dirname as in the following

    Removing a range of history lines from bash history

    Problem

    You would like to remove some lines from the bash history (ie using cut and paste with wrong values).

    Solution

    You can use the following, taken from the answer here

    where start_line is the start of the lines you want to remove and no_of_lines is the number of lines you want to be removed.

    Use tee command to send output to a file and email

    Problem

    You would like to send the error output from a command or a script to both an error file and an email.

    Solution

    By using tee, you could do the following:

    Bash script using default value for empty parameter

    Problem

    You have a bash script that accepts parameter(s) but you want to give a default value for a missing parameter.

    Solution

    For example you have a script that accepts a parameter with the year and month (ie 201703), but you want to give it a default of two months ago if the parameter is missing.