Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
    
Did you make it executable? –  Venki Apr 21 at 10:10
    
yeah, forgot to mention that part sry.! –  neuronal.bit Apr 21 at 10:12
    
Check out this question here : askubuntu.com/questions/442246/… –  Venki Apr 21 at 10:25
    
But I simply want to make my script get executed on startup and no application out of a script @venki! –  neuronal.bit Apr 21 at 11:01

1 Answer 1

  1. Open Dash
  2. Search for "Startup Applications" and open it
  3. Click on "Add" button.
  4. The dialog lets you set a name, the command to be executed, and comments.
  5. Click on "Add" to save that command to the list.

Ubuntu will then run all the commands on this list during start-up. I use this for running a wallpaper changing script of my own on start-up, and it has proved reliable.

I think you must be knowing this but still, remember to add an & at the end of the command to keep running your script in background, and prevent unnecessary debugging and frustration.

Sorry but I don't know a way to do this from the terminal.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.