Lesson 7

Dates: 3/5/2014
Start-up and Run Levels. Scheduled jobs (at, cron)
Linux for Engineering and IT Applications


Startup script example

#!/bin/bash
#
# Let's call it sample.sh
# For example, define the current Run Level:

runl=`/sbin/runlevel | awk '{ print $2 }'` 
nowd=`date`


start() {
# start function 
echo "$nowd -- start sample script at Run Level $runl" | tee -a /var/log/sample.log
# ....
}

stop() {
# stop function 
echo "$nowd -- stop sample script at Run Level $runl" | tee -a /var/log/sample.log
# ....
}

status() {
# status function
echo "status sample script at Run Level $runl"
# .....
}

restart(){
        stop
        start
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status 
        ;;
  restart)
        restart
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|restart}"
        exit 1
esac

Make sample.sh executable and add to the startup, specifically S34 at run levels 2 3 4 5 and K42 at run levels 0 1 6:

chmod 755 sample.sh
update-rc.d sample.sh  start 34  2 3 4 5 . stop 66  0 1 6 .



Take me to the Course Website