Simple demon bash script
The goal of this script is to allow to run simple commands as demons.
Usage example to start a demon that will execute 'echo "Hello world" >> /tmp/demon.log' every 5 seconds :
./samis_demon.sh start 5 'echo "Hello world" >> /tmp/demon.log'
Usage example to stop the demon
./samis_demon.sh stop
NOTE:
1. The stop will terminate all running demons
2. In order for the demon to work it has to have read and write permissions to the directory that contains the script
#!/bin/bash
# The script allows to run a command as a demon
# Usage example to start a demon that will execute 'echo "Hello world" >> /tmp/demon.log' every 5 seconds :
# ./samis_demon.sh start 5 'echo "Hello world" >> /tmp/demon.log'
# Usage example to stop the demon
# ./samis_demon.sh stop
# NOTE:
# 1. The stop will terminate all runing demons
# 2. In order for the demon to work it has to have read and write permissions to the directory that contains the script
# @author Samuil Banti
# @copyright (C) 2018 - Samuil Banti
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
demon_dir="$(cd "$(dirname "$0")" && pwd)"
demon_file=$demon_dir"/"$(basename "$0")
demon_active="$demon_dir/samis_demon_active"
action="$1"
timeout="$2"
command="$3"
# Start
if [ "$action" = 'start' ]; then
touch $demon_active
eval "$demon_file exec $timeout '$command' &"
echo 'Samis demon started.'
# Stop
elif [ "$action" = 'stop' ]; then
if [ -f "$demon_active" ]; then
unlink $demon_active
if [ -f "$demon_active" ]; then
echo 'Could NOT disable the demon.'
else
echo 'Samis demon disabled'
fi
else
echo 'No active Samis demon found.'
fi
# Deamon
elif [ "$action" = 'exec' ]; then
while [ -f "$demon_active" ]
do
eval $command
sleep $timeout
done
else
echo "Usage:"
echo "./samis_demon.sh [start|stop] [TIMEOUT] '[COMMAND]'"
fi
Download...