How to Create an Init Like Script

Bash Linux

On this tutorial I will explain how to create a quick init like script to be run in the background. We will not be adding this script to /etc/init or look into how to run it at startup. Instead we will run it manually. If you are looking for a Systemd version of this tutorial, check out my previous post Creating a Simple Systemd User Service.

First let’s create our service script. This is the daemon that will be running in the background. For this example we will create a script that monitors a log file:

tail -fn0 logfile | \
while read line ; do
  echo "$line" | grep "pattern"
  if [ $? = 0 ]
  then
    ... do something ...
  fi
done

Now let’s create a control script. This script is what we will use to start/stop our daemon.

#!/bin/bash

daemon="[path_to_my_daemon_script]"
name="Name for the service/daemon"
desc="Description for the script"
pid_file="/var/run/[daemon_name].pid"

# Check whether the binary is still present:
test -x "$daemon" || exit 0

case "$1" in
start)
  [ -f "$pid_file" ] && { echo "Already running" ; exit 0 ; }
  echo "Starting $name"
  "$daemon" &
  echo $! > "$pid_file"
  ;;
stop)
  [ ! -f "$pid_file" ] && { echo "Not running" ; exit 0 ; }
   echo "Stopping  $name"
   kill "$(cat $pid_file)"
   rm "$pid_file"
   ;;
restart)
   $0 stop
   $0 start
   ;;
status)
   if [ -e "$pid_file" ]; then
      echo "$name is running, pid=$(cat $pid_file)"
   else
      echo "$name is not running"
      exit 1
   fi
   ;;
*)
   echo "Usage: $0 {start|stop|status|restart}"
esac

exit 0

Make sure both files are executable and you are ready to start your daemon.

[controlscript] start

You can check the status, stop, etc…

[controlscript] status

A Simple Powerline Prompt for Bash

bash Linux

Powerline-simple is a simple powerline prompt for Bash, written by yours truly.

The prompt displays the following information:

  • Previous exit code
  • Username
  • Hostname (when connecting via SSH)
  • Battery status
  • sudo cached credentials
  • Current path
  • Git status

You can download it from the GitHub project page.

KDE Win7 Volume Mixer

Linux kde

It’s not often that we see Linux using something inspired by Microsoft, and this is one of those cases. If you are running KDE as your default DE, you should definitely take a look at «Win7 Volume Mixer» as an alternative to the default volume mixer. It adds a great visual and a more user friendly interface.

Here’s a quick visual comparison:

Default Mixer

Imgur

Win7 Volume Mixer

Imgur

How to Create a Timer in Bash

Linux Bash

Following my previous post on «How to Create a Prompt With Timeout in Bash », we will now see how to create a timer (countdown) in Bash using the built-in $SECONDS variable.

The $SECONDS variable holds in the number of seconds the script has been running. So it can easily be used to create a timer inside your script in Bash.

$ bash -c 'while true ; do echo -en "\r${SECONDS}s elapsed" ; sleep 1 ; done'
203s elapsed

Imgur

How to Change config.json for Docker Mattermost Preview

Linux Docker Mattermost

These instructions show you how to change the config.json file for a Docker Mattermost Preview.

a- As root (or if you enabled non root users to manipulate docker), let’s open a shell in the docker container

docker exec -ti mattermost-preview /bin/bash

b. Update the APT db on the docker instance and install vim (needed to edit the file)

apt update
apt install vim

c. Change config_docker.json as needed

cd /mm/mattermost/config

vim config_docker.json

d. Restart the docker container

docker restart mattermost-preview
code with