1

I want to call a script on machine shutdown and restart. I have written code like

#! /bin/bash
#chkconfig: 0 6 1 1
#description: Description of the script
# processname: killfoo
# this script starts and stops foo

# Some things that run always
LOG_FILE="/tmp/killfoo.log"

log() { while IFS='' read -r line; do echo "$(date) $line" >> "$LOG_FILE"; done; };
exec > >(tee >(log))
# Carry out specific functions when asked to by the system
case "$1" in
  start)
    echo "Nothing to done"
    ;;

  stop)
    echo "Stopping script killfoo"
    pkill   -9 foo >> $LOG_FILE 2>&1

    ;;
  *)
    echo "Usage: /etc/init.d/killfoo {stop}"
    exit 1
    ;;
esac

exit 0

I created file /etc/init.d/killfoo having executable permission. I have enable this script for run level 0,6.

chkconfig --add killfoo
chkconfig --level 6 killfoo on

But script is not getting executed on machine restart. What could be the problem ?

Vivek Goel
  • 1,273
  • 1
  • 13
  • 11

2 Answers2

1

I can't verify, but if chkconfig(8) is to be believed, you have a syntax error on your #chkconfig: line. Apparently, it should be:

# chkconfig: 06 1 1

for a start and stop priority of both 1 in runlevels 0 and 6.

Running chkconfig --list killfoo should tell you whether the script will actually be executed when entering/exiting the relevant runlevels. Note that a priority of 1 may clash with other scripts that perform low-level actions; I'd try 50 for a starting point unless there were specific reasons to go with other values.

user
  • 28,161
  • 13
  • 75
  • 138
  • chkconfig output 0:on 1:off 2:off 3:off 4:off 5:off 6:on I can see file having S prefix in rc6.d and rc0.d. But there is no file with K prefix. – Vivek Goel Apr 10 '13 at 12:30
  • In that case, you can probably work around your problem by moving the actual logic from the `stop)` block to the `start)` block, and redefining this script to be start only. – user Apr 10 '13 at 12:50
-1
  1. Remove the space after "!"
  2. Use /bin/sh instead of /bin/bash
Nils
  • 18,202
  • 11
  • 46
  • 82
  • 1
    @Nilis corrected the 1 one. Second one will not work as I am using bash specific functions. One more thing if I do chkconfig --level 6 off killfoo It is creating file as name K if I am giving option with S It is creating file as name K – Vivek Goel Apr 10 '13 at 14:29
  • `#! /bin/bash` is fine, a space or tab is permitted after the shebang. There's no reason to use `sh` here in preference to bash, it won't make a difference. – Gilles 'SO- stop being evil' Apr 10 '13 at 23:07