I'm trying to create my own script that gets executed on startup and stopped on shutdown (referring to the classic System-V init style), but it won't work!
I've created this script (/etc/init.d/mlogscript
):
#!/bin/bash
### BEGIN INIT INFO
# Provides: logging down time and date of your start/shutdown behaviour
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: logs down your start/shutdown behaviour
# Description: script for logging when you start or shutdown the operating system
### END INIT INFO
# Author: x
# log-file info
FILENAME="logscript.log"
FILEPATH="$HOME/myScripts/"
# other files
UPTIMEFILE=${FILEPATH}"LASTUPTIME.DONOTDELETE"
# fetch specific data
DATE=`date +%m.%d.%Y`
CLOCK=`date +%X`
case "$1" in
start)
if [ ! -e "${FILEPATH}${FILENAME}" ]; then
echo "creating log-file"
echo -ne "mLog: start/shutdown behaviour\n" >> "${FILEPATH}${FILENAME}"
echo -e "-------------------------------\n" >> "${FILEPATH}${FILENAME}"
fi
echo -e '[' $DATE "]\nuser:\t" $USER "\nstart:\t" $CLOCK >> "${FILEPATH}${FILENAME}"
# create/update uptime-file
UPTIME=$(</proc/uptime)
UPTIME=${UPTIME%%.*}
echo -ne $UPTIME > $UPTIMEFILE
echo "log-script started at" $DATE "."
;;
stop)
if [ -e "${FILEPATH}${FILENAME}" ]; then
# get uptime from /proc/uptime
UPTIME=$(</proc/uptime)
UPTIME=${UPTIME%%.*}
#
if [ -e "${UPTIMEFILE}" ]; then
LASTUPTIME=$(head -n 1 "${UPTIMEFILE}")
UPTIME=`expr $UPTIME - $LASTUPTIME`
echo $UPTIME
fi
SECONDS=$((UPTIME%60))
MINUTES=$((UPTIME/60%60))
HOURS=$((UPTIME/60/60%24))
DAYS=$((UPTIME/60/60/24%365))
YEAR=$((UPTIME/60/60/24/365))
# write to file
echo -ne "end:\t" $CLOCK "\nuptime:\tY" $YEAR ' D' $DAYS ' H' $HOURS ' M' $MINUTES ' S' $SECONDS "\n\n" >> "${FILEPATH}${FILENAME}"
echo "log-script stopped"
else
echo "log-file doesn't exist"
fi
;;
restart)
if [ -e "${FILEPATH}${FILENAME}" ]; then
exec $(readlink -f "$0") stop
$(readlink -f "$0") start
echo -e "log-script restarted"
fi
;;
reset)
echo -e "log-script reset"
if [ -e "${FILEPATH}${FILENAME}" ]; then
rm "${FILEPATH}${FILENAME}"
exec $(readlink -f "$0") start
fi
;;
esac
exit 0
After that I set the owner and group of the file to "root" by executing
sudo chown root:root /etc/init.d/mlogscript
Last but not least I created the runtime-specific symlinks within the rc[0-6].d folders:
sudo update-rc.d mlogscript defaults 98
After I rebooted my system nothing happens, why?