Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

How to Kill Background Child Process in Bash

Tags: July 13, 2020 12:52 PM
0 comments

Terminate Background Child Process in Bash

In Bash by child process that sent into background is still alive when main program is terminated. Take a look an example below.

#!/bin/bash

# Run a Python web server
python -m http.server src/ &

# Run SASS watcher and compiler...
sass --watch src/scss:src/css &

wait
When we run script above and terminate using CTRL+C Python and SASS are still running.

Solution to terminate Background Child Process in Bash

The solution to above problem is using shell built-in trap command.

#!/bin/bash

# Kill all child process (Python and SASS) when exit
trap "kill 0" EXIT

# Run a Python web server
python -m http.server src/ &

# Run SASS watcher and compiler...
sass --watch src/scss:src/css &

wait

Now when the script is exited all the child process even which has been sent as background process will also terminated.

References

Share on Facebook Twitter

Variable Variables in Shell

Tags: March 10, 2020 8:12 PM
0 comments

Subtitute Variable inside Variable in Bash

Example below is using Bash for variable variables subtitution.

$ hello="Hello World"
$ foobar="hello"
$ echo "${!foobar}"
Hello World

Subtitute Variable inside Variable using eval

This is for other shell which do not recognize "${!}" syntax. It utilise eval so use it with caution.

$ hello="Hello World"
$ foobar="hello"
$ eval echo "\$${foobar}"
Hello World

References for Variable Variables in Shell

Share on Facebook Twitter

Terraform: Force Destroy Resource when prevent_destroy is true

Tags: March 3, 2020 6:55 AM
1 comments

How to Force Destroy Resource in Terraform

Terraform resource that having lifecycle prevent_destroy = true can not be destroyed. You need to manually edit the file inplace and change the value prevent_destroy to false manually each time you want to destroy the resource. Instead of having to edit manually and make git status dirty we can automate this using simple shell script.

Automate Force Destroy Resource in Terraform

The idea is simple.

  1. Search all *.tf files and look the value of prevent_default = true to prevent_default = false
  2. Run terraform destroy command
  3. Revert the changes back to prevent_default = true
Here is the implementation in Bash.
#!/bin/bash

$ find . -name '*.tf' -type f \
-exec perl -i -pe 's@prevent_destroy = true@prevent_destroy = false@g' {} \;

# Run terraform destroy
[ "$IS_PLAN" = "yes" ] && terraform plan -destroy || terraform destroy $@

# Revert the changes
$ find . -name '*.tf' -type f \
 -exec perl -i -pe 's@prevent_destroy = false@prevent_destroy = true@g' {} \;
Save the file with name e.g: terraform-force-destroy.sh. To issue terraform plan -destroy command use the following.
$ IS_PLAN=yes bash terraform-force-destroy.sh
To force destroy the resource use the following command.
$ bash terraform-force-destroy.sh -auto-approve
You can give normal terraform's arguments just like the original terraform destroy.

References for Terraform Force Destroy

Share on Facebook Twitter

Reset All Tables Except Migration Table on MySQL

Tags: April 15, 2016 9:29 PM
0 comments

Overview

When developing an application there is case when you need to clear all the data in your database e.g: testing the seeding or such. But you want to exclude some tables let say migration table which used by the application framework to migrate the schema.

Problem

You want to clean up data on all the tables, except the migration table because you don't want to re-run the schema migration.

Solution

  1. Get list of tables
  2. Exclude the migration table
  3. Append the prefix 'DELETE FROM ' to each line
  4. Append the suffix ';' to each line
  5. Pipe the result to MySQL
$ echo "SHOW TABLES;" | mysql -N DB_NAME | grep -v orb_migration | sed -e 's/^/DELETE FROM /' -e 's/$/;/' | mysql DB_NAME
In case above the table which excluded is orb_migration.

Share on Facebook Twitter

Bash Trim Whitespace

Tags: May 2, 2014 10:45 AM
1 comments

To remove leading and trailing whitespace from a subset of strings in a shell we can use sed.

$ echo '   #FOO#   ' | sed -e 's/\s*$//' -e 's/^\s*//'
#FOO#

Function 'trim'

To reuse it in another place, it is good idea to wrap it as a function.
# Function to trim leading and trailing spaces
trim() {
  # Accept input from argument or STDIN
  # So you can do both:
  # $ echo '  #FOO#   ' | trim
  # or
  # $ trim '   #FOO#   '
  local STRING=$( [ ! -z "$1" ] && echo $1 || cat ; )
  
  echo "$STRING" | sed -e 's/^\s*//' -e 's/\s*$//'
}
Now it can be used to trim a string both from argument or standard input.
$ echo '   #FOO#   ' | trim
#FOO#

$ trim '   #FOO#   '
#FOO#

Reference

Share on Facebook Twitter