0

I have Linux 5.3 redhat I want to add the script add_dhcp.ksh to my Linux machine in order to update ifcfg-eth1 automatically after reboot ( this script enable dhcp and startup eth1 )

my question: please advice the steps that needed in order to activate automatically the add_dhcp.ksh script after Linux reboot ,

    add_dhcp.ksh script

    #!/bin/ksh


    perl -i -pe "s/none/dhcp/"  /etc/sysconfig/network-scripts/ifcfg-eth1 
    perl -i -pe "s/no/yes/"  /etc/sysconfig/network-scripts/ifcfg-eth1 
    ifdown eth1
    ifup   eth1
4
  • Is eth1's config not saved after rebooting? Moreover, why did you edit eth1 and restart eth0?
    – quanta
    Nov 8, 2011 at 10:09
  • sorry my mistake see update
    – Diana
    Nov 8, 2011 at 10:48
  • What is your final goal? Why did you need to change it at boot?
    – quanta
    Nov 8, 2011 at 10:49
  • actually this script is example of change some conf in Linux after reboot , my final script will be different with allot of configuration include network configuration , so I dont want to show here complicated script ,
    – Diana
    Nov 8, 2011 at 10:57

3 Answers 3

1

run this script by adding it to /etc/rc.local file. whatever is present in rc.local file, it is run by linux at every startup.the order is fine, its just that you can use "service network restart" instead of ifup ifdown. . also, what is the need to do this manually at every reboot? why not permantally enable DHCP on this interface once?

5
  • hi Frank , I prefer to add the script under directory and not to edit any file , did you have other solution
    – Diana
    Nov 8, 2011 at 10:53
  • There is no directory in linux, which is confugred this way to run commands atr startup, but just files.
    – Farhan
    Nov 8, 2011 at 10:56
  • Adding a file in /etc/cron.d with an appropriate entry would do this
    – user9517
    Nov 8, 2011 at 11:35
  • lain: Cron only runs scripts at specific time, it does not recognized startup, shutodwn or reboots.
    – Farhan
    Nov 8, 2011 at 11:51
  • @Frank: I suggest you go look at the man page for a modern cron/crontab(5) @reboot : Run once, at startup.
    – user9517
    Nov 8, 2011 at 22:44
1

You can do this using cron and the @reboot 'time' in /etc/crontab e.g.

@reboot root /path/to/add_dhcp.ksh 

This will run your command once at startup.

EDIT:

If you don't want to edit any existing system files then you can create a file in /etc/cron.d. This directory is like an extension to the /etc/crontab file and the files use the same format. Note that files in this directory should be writeable only by root.

Create file (/etc/cron.d/rebootfixup for example) and add a line like this

@reboot root /path/to/add_dhcp.ksh

Now when your system boot the file will be added to the list of cron jobs and actioned appropriately.

2
  • crontab -l no crontab for root ( I cant use crontab -:( )
    – Diana
    Nov 8, 2011 at 11:16
  • @Diana See the edit to my answer.
    – user9517
    Nov 8, 2011 at 11:35
0

I think the usual way to do this is to drop it into rc.local, but if you don't want to change files and want a pure directory approach, you can put your script into the /etc/init.d directory. Configure it to accept an argument of 'start' or 'stop' (the stop arg may not be relevant to this particular use case though). Then add a symlink in /etc/rc3.d/ to point to your script with a name of, for example, S99adddhcp.

These 'rc' directories are read as part of the startup process, and you can modify startup behaviors here if you like this method over rc.local for some reason.

Here are a couple of many, many references to this type of configuration - well worth reading:

http://www.notesbit.com/index.php/scripts-unix/customizing-system-startup-in-redhat-linux/ http://www.ghacks.net/2009/04/04/get-to-know-linux-the-etcinitd-directory/

--Matt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .