LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 07-09-2012, 02:10 PM   #1
init-6
LQ Newbie
 
Registered: Jul 2012
Posts: 2

Rep: Reputation: Disabled
Run a Bash script once after first reboot only.


OK, so I have been trying to get this to work for about a week and a half now. I have read countless threads from an endless supply of forums and FAQ pages, and the bottom line is I cannot find the "right" answer.

I apologize in advance if seems like a Noob question or the answer is truely out there some place, but I am looking for simpleton terms on how to do this.

In a nutshell:
  • We do 2 flavors of install distro's where I am working, RHEL & CentOS (both 32 & 64 bit).
  • We use a kickstart.pl script to generate the ks.cfg for each installation.
  • The kickstart is used to collect hostname, ISO image, IP Address, lan or dmz, plus whether or not to download a particular version of Tomcat & JDK.
  • Within the kickstart a handful of files are copied from a management server (also the kickstart server) via FTP to the server being built.
  • Then a Bash Shell script is called and executed.

What the Bash script does:
  • Runs /sbin/chkconfig to turn various services & daemons off.
  • Sets the runlevel.
  • Moves files for sshd, snmp and configures them to run.
  • Adds a couple of symbolic links.
  • Sets the timezone.

Heres the problem:
There is an "if - then - else" statement in the called-up script;

# - put syslog and yum configurations in place
/bin/mv /etc/syslog.conf /etc/syslog.conf.${THEDATE}
/bin/mv /etc/yum.conf /etc/yum.conf.${THEDATE}
# determine if LAN ( xxx.lan.int ) or DMZ ( xxx-dmz.ext )
hostname -f | grep -q xxx.lan.int; RC=$? ;
if [ $RC -eq 0 ]
then
/bin/cp /root/WORK/finish/syslog.lan.conf /etc/syslog.conf
/bin/cp /root/WORK/finish/yum.lan.conf /etc/yum.conf
else
/bin/cp /root/WORK/finish/syslog.dmz.conf /etc/syslog.conf
/bin/cp /root/WORK/finish/yum.dmz.conf /etc/yum.conf
fi


Because the new system is still running in "kickstart" mode when it gets to this part of the script the "hostname-f" will always fail and default to the "else" part of the statement, which works great for DMZ builds.

If I break this piece of code out as it's own script and have it copied to the system with the other files from the management/kickstart server, reboot, chmod the file to 755 and then run the script ./scriptname.sh manually it works 100% everytime.

My question(s):
How do I run this break-out script only once after the kickstart finishes? I have seen numerous posts on copying the script to /etc/init.d/ and create a symbolic link to the script. I have also seen copy it to /etc/rc#.d/init.d/ (where #=boot priority) and again create a symbolic link to it. Where is the "symbolic link" created? In the kickstart? In the bash script? How do you determine the correct boot priority? Does boot priority even matter?

Is there a better way to write this "if" statement in the initial setup script?
Can the hostname be forced to be set in the kickstart or script, to allow the hostname -f command to work?

I don't consider myself a complete Noob, but I am by no means an expert, so again apologize if this is the wrong place to be asking these questions. Oh, and yes all of the builds are server, not workstation.

To answer another question some of you may ask, the sys engineer does not want to only one syslog server for both DMZ and LAN, he would like to keep firewall traffic to specific ports for the server applications and avoid the unnecessary syslog traffic going across the firewall.

Thank you in advance for any help and sugesstions,
 
Old 07-09-2012, 02:45 PM   #2
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Hi,

May I point you to "/etc/rc.local"
Mine does:
Code:
cat /etc/rc.local 
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

/sbin/blockdev --setra 8192 /dev/sda
/sbin/blockdev --setra 8192 /dev/sdb

touch /var/lock/subsys/local

/etc/init.d/iptables restart &
and I presume that in kickstart you can modify the file "rc.local" to be set after reboot, so it then will execute your script.

That is just my thought, I don't know kickstart or anything about it, sorry.

good luck
 
Old 07-09-2012, 03:22 PM   #3
init-6
LQ Newbie
 
Registered: Jul 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks for the sugesstion Lithos. I'll definitely look at that, but will rc.local run every time the sytem is rebooted or am I thinking of some profile file, sorry the exact file name escapes me...I need to init-6 myself!
 
Old 07-09-2012, 03:53 PM   #4
UndiFineD
LQ Newbie
 
Registered: Jun 2004
Location: Netherlands
Distribution: adistro
Posts: 24

Rep: Reputation: 0
to check whether this is your first run, you can do 2 things

install the script as marked executable
and disable the execute bits on the script when finished

or you can check for the existence of another file
and when it does, execute the script
while this may seem not much of an improvement over the first technique
you can use it in your script for partial execution too.
if [ -e /my/execution/lock ]
 
Old 07-10-2012, 01:49 AM   #5
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Quote:
Originally Posted by init-6 View Post
... but will rc.local run every time the sytem is rebooted or am I thinking of some profile file, ...
YES, in RHEL based distros
Code:
/etc/rc.local
is run Every time after boot!

(that's why on my server I'm setting the ReadAhead buffer size to 8MB and restart IPTables -- example above)
From Windows the closest setting is within registry options Run (HKLM\Software\Microsoft\Windows\CurrentVersion\Run), which is also run every time it starts up.

good luck

Last edited by lithos; 07-10-2012 at 01:51 AM.
 
Old 07-10-2012, 07:18 AM   #6
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
Do you want to run the script during installation and have a valid output for hostname there, or only lateron during the first reboot? One thing you can check it the mount counter in the superblock:
Code:
$ tune2fs -l /dev/sda1
...
Mount count:              2
If “Maximum mount count” is reached it will be reset though.
 
Old 07-13-2012, 09:38 PM   #7
pafoo
Member
 
Registered: Jul 2011
Location: Alabama
Distribution: Red Hat/Ubuntu/Solaris
Posts: 37

Rep: Reputation: 11
Save some time. Use a test statement in the script. Something like

Have your script create the file or use a file you push out etc.

Code:
if [! `grep -c "booted" booted.txt` > 0 ] 

then
        echo "Do some code"
else
        echo "Code not needed"
fi
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Run multiple bash and php scripts from a bash script charu Programming 5 07-26-2011 02:40 AM
My BASH script work perfect when i run it manualy, but not when it run in the crontab roqarg Linux - Newbie 30 05-06-2010 01:37 PM
RedHat: Getting my script to run after I reboot eur0dad Programming 13 07-26-2006 11:26 AM
Make a script run at reboot Braynid Linux - Newbie 10 06-26-2006 10:10 AM
Bash Script reboot pazvant Programming 1 05-04-2004 05:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 10:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration