Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

init 4: need to change "chkconfig" entry

Status
Not open for further replies.

daFranze

Technical User
Dec 29, 2003
1,334
DE
Hi there,

we decided to change the default runlevel to 4, in runlevel 4 starts an additional software

since some scripts in /etc/init.d do stop software in Runlevel 4 I need to change the configuration in the files using the chkconfig entry from
# chkconfig: 235 77 99
to
# chkconfig: 2345 77 99
and re-register the scripts with chkconfig --del and chkconfig --add.

Therefore I intend to write a script, which checks the field 2, if it contains a 3 it would substitue it by 34.
Well I could write a very dowdy script, which heads and tails the file and cut the chkconfig into pieces to substitute the entry, BUT, is there an awk/perl/sed wizzard, which may write this in a single line?





Best Regards, Franz
--
System Manager (Solaris, HP-UX, Linux, some networking, some SAN)
 
Any idea to do this more gorgeous?

#!/bin/sh

scriptname=$(basename $0)
theFile=$1

if [ -r $theFile ]; then
# lines = length of file
lines=$(cat $theFile | wc -l)

# chkline = line containing expected string chkconfig:
chkline=$(cat $theFile | sed 's:^:#:' | nl | grep chkconfig: | awk '{ print $1 }')

# headlines = lines in file over chkconfig: line
headlines=$(echo $chkline - 1 | bc)

# taillines = lines in file under chkconfig: line
taillines=$(echo $lines - $chkline | bc)

# make backup of file (we will read from this file furtheron
cp $theFile ${theFile}.old

# copy the lines above the chkconfig: line
head -${headlines} ${theFile}.old > ${theFile}

# this is not nice but it works...
# this is the replacement part: read the chkconfig:, cut into pieces and rebuild the line
newrunlevels=$(grep chkconfig ${theFile}.old | awk '{ print $3 }' | sed 's:3:34:' | sed 's:44:4:')
echo '# runlevels changed by '$myScriptname ' at ' $(isodate) >> ${theFile}
printf "# chkconfig: $newrunlevels " >> ${theFile}
grep chkconfig ${theFile}.old | awk '{ print $4 " " $5 }' >> ${theFile}

# copy the lines beyond the chkconfig: line
tail -$taillines ${theFile}.old >> ${theFile}
else
echo ${myScriptname}: cannot open $theFile
exit 1
fi


Best Regards, Franz
--
System Manager (Solaris, HP-UX, Linux, some networking, some SAN)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top