Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I need to run some bash script only once on first boot (after OS installation) of my CentOS machine This script should run before network service start (because this script will change network configuration) How can I register my script to run before network service start?

share|improve this question

2 Answers 2

up vote 8 down vote accepted

If you look at the "chkconfig" line of /etc/init.d/network you'll see that the network has a start priority of "10".

/etc/init.d/yourscript:
#!/bin/bash
#
# yourscript  short description
# 
# chkconfig: 2345 9 20
# description: long description

case "$1" in
  start)
    Do your thing !!!
    chkconfig yourscript off
  ;;
  stop|status|restart|reload|force-reload)
    # do nothing
  ;;
esac

Then run chkconfig yourscript on to get it to run at boot. The chkconfig yourscript off inside the script should disable it running on any subsequent boots.

Some versions of CentOS/RHEL/Fedora have a "firstboot" program you could try to use, but that seems like a pain.

You sure you can't run your network reconfiguration script inside of a %post in a kickstart? That's what I do.

share|improve this answer
1  
+1 for the more "distribution-correct" methods outlined here. –  voretaq7 Mar 15 '11 at 17:17
    
I cannot use %post section in a kickstart file because I need it for embedded system: I'm creating an OS image with my application and distributing it. My network configuration is depends on MAC addresses as a result I need to do my first time configuration on the first boot. I'm going to try your solution using chkconfig. Thanks –  Dima Mar 16 '11 at 18:27

Simple answer: Stick it in /etc/init.d (symlinked to /etc/rc#.d) & the first thing it does is check for a file's presence: If the file exists the script has already run (so exit). If not, run the script and touch the file.

This also lets you delete the file to force the script to run again later.

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.