Bash As System Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash As System Script
# 1  
Old 08-04-2010
Bash As System Script

My main machine is set-up so that I configure the network details (IP etc) by command line. However I'm trying to create an bash script to run those commands upon system startup. Problem I have is that the commands require root access and I want it to be fully automatic.

Now I figure I need to add the bash script into system script so the system runs it as root but not sure how to do that? I though of maybe it adding it to a directory where other start-up scripts are run like /etc or /init.d but it can't be that simple can it?

On a side note I'm also trying to make it actually test that the connection is running before saying Network Is Up. I can do this with Ping, but how do I access the return so that it can report if the ping failed (and network is down).

The script is

Code:
#!/bin/bash
ifconfig eth0 inet 10.13.23.201
route add default gw 10.13.23.1
clear
echo "Network Is Up"

# 2  
Old 08-04-2010
can you utilize sudo? or where are you trying to start it? you can create an init script that does it and make sure you do a beroot.

you can test ping and just get the output

Code:
ping something >/dev/null
if [ $? -gt 0 ]; then 
 echo "blah didn't work"
else
 echo "yay it worked"
fi

also just do >/dev/null instead of doing the clear..

---------- Post updated at 09:38 PM ---------- Previous update was at 09:38 PM ----------

can you utilize sudo? or where are you trying to start it? you can create an init script that does it and make sure you do a beroot.

you can test ping and just get the output

Code:
ping something >/dev/null
if [ $? -gt 0 ]; then 
 echo "blah didn't work"
else
 echo "yay it worked"
fi

also just do >/dev/null instead of doing the clear..

Last edited by pludi; 08-04-2010 at 02:50 AM..
This User Gave Thanks to eclipseagent For This Post:
# 3  
Old 08-04-2010
Can't really utilize sudo as that would still require the password to be entered at some point. I want it to start and run automatically (without any user input needed) upon system boot.

Why is >/dev/null better than simply clear?

I'll give the ping thing a try.
# 4  
Old 08-04-2010
You could add your script to /etc/init.d and then add symlinks in runlevel directories (/etc/rc?.d). Or you could append your script to rc.local if your system uses one.
This User Gave Thanks to agn For This Post:
# 5  
Old 08-04-2010
I've left out the ping section for the moment until I can get it working first.

I attempted to follow your advise agn but it doesn't seem to work. Here is what I got so far.

Named my script network-launch. Copied it to /etc/init.d.

I then used the command

Code:
ln -s /etc/init.d/network-launch ./K07network-launch

in both /etc/init.d/rc3.d and /etc/init.d/rc5.d (command line with networking and gnome with networking respectively). I tried also changing the start to S03 are a variety of other combinations of S and K with different numbers putting it before and after the K and S network symlink.

After some quick diagnostics I discovered that upon system boot the eth0 (my network card) was not being actived. So I changed the script to add the following line to the start

Code:
ifconfig eth0 up

And then re-run the symlink creation commands to create new links after remove the old ones.

However this is still not working. It doesn't appear the script is being run at all since even with the addition of the new line to bring eth0 up; eth0 is still not up after system boot.

Oh and I'm running OpenSuse 11.2 with network manager disabled (hence the use of command line).
# 6  
Old 08-04-2010
First of all init.d scripts have a fairly specific format if you want the script to play well with the operating system. Here is an outline of the format. Look at other files in init.d to get a fuller understanding.
Code:
#! /bin/sh
#
# name of script
#
### BEGIN INIT INFO
# Provides: 
# Required-Start:
# Required-Stop:
# Default-Start: 2
# Default-Stop:
# Description: 
### END INIT INFO

. /etc/rc.status

rc_reset

case "$1" in
start)
    your code to start service
;;
stop)
    your code to stop service
;;
restart)
stop;
start;
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
;;
esac
rc_exit

Next, you need to decide what run level your init script is started at and what run level your init script is stopped at. They may be different.

I suggest you start your script at runlevel 2. This means that you need symbolic link from your init.d script to a file named SxxYourScriptName in the rc2.d directory.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed ? Is there any way to get the script names for the process command ? --- Post updated at 08:39 AM --- in KSH (Korn Shell), my command output shows the script names but when run in the Bash Shell... (3 Replies)
Discussion started by: i4ismail
3 Replies

2. Shell Programming and Scripting

How to block first bash script until second bash script script launches web server/site?

I'm new to utilities like socat and netcat and I'm not clear if they will do what I need. I have a "compileDeployStartWebServer.sh" script and a "StartBrowser.sh" script that are started by emacs/elisp at the same time in two different processes. I'm using Cygwin bash on Windows 10. My... (3 Replies)
Discussion started by: siegfried
3 Replies

3. Shell Programming and Scripting

Student system BASH script

Hi guys, i'm beginner with Unix, I tried my best, but I Really don't know how to finish it :D. here's my problem : I have to do something like Student system - student's name | subjects | mark |credits. after starting script it will ask you for Student's name, then which subject, then the... (2 Replies)
Discussion started by: PrincMehi
2 Replies

4. Shell Programming and Scripting

Overwrite bash system command

Hello, The problem I met is the conflict between the default command /usr/bin/sometools, which is an very old version at system setup, and an updated one I have installed in $HOME/download-software/sometools How do I tell a third program to use the customized sometools instead of the default... (6 Replies)
Discussion started by: yifangt
6 Replies

5. Shell Programming and Scripting

Find all bash scripts on system

I want to find all bash scripts on my system. What I have so far is this: find / -type f \( -perm -u=x -o -perm -g=x -o -perm -o=x \) | xargs -I{} head -1 {} |grep bashThis will find all executables, read the first line and diplay it, if it contains "bash". Almost done, but I also need the... (8 Replies)
Discussion started by: Cochise
8 Replies

6. UNIX for Dummies Questions & Answers

Im new to bash scriping and i found this expression on a bash script what does this mean.

# check host value regex='^(||1|2|25)(\.(||1|2|25)){3}$' if ')" != "" ]; then if ]; then echo host $host not found exit 4 fi elif ]; then echo $host is an invalid host address exit 5 fi espeacailly the top regex part? ---------- Post updated at 06:58 PM ---------- Previous update was... (1 Reply)
Discussion started by: kevin298
1 Replies

7. Programming

System Calls using C w/BASH

Hello all, I'm attempting to write a basic application that appends an arbitrary list of txt files to one txt file. So, for example, if the application is run like so: appendFileToFile file1.txt file2.txt file3.txt the system command should, hopefully be, cat >> testfile.txt I'm... (4 Replies)
Discussion started by: whyte_rhyno
4 Replies

8. Shell Programming and Scripting

Bash script show Kill system output

Hi we are calling kill -9 $pid command from bash script it gives below output, but we need to hide the output. i tried /dev/null but ni luck. is there any alternate way to schive this. ../kill_scr.sh: line 42: 1891 Killed /tmp/anr_rest_mul_wc.sh Soalris 10. ... (2 Replies)
Discussion started by: sachinbutala
2 Replies

9. Shell Programming and Scripting

Bash Help: users who are not logged into the system to display

A Newbie here, I am working on a script and am having problems with the else part of the script. I can't get the users who are not logged into the system to display on the screen with their username and the text "The user is not logged in". I am sure it is something simple and stupid, but I... (5 Replies)
Discussion started by: rchirico
5 Replies

10. Shell Programming and Scripting

how to make your bash script run on a machine with csh and bash

hi, i have a script that runs on bash and would like to run it on a machine that has csh and bash. the default setting on that machine is csh. i dont want to change my code to run it with a csh shell. is there any way i can run the script (written in bash) on this machine? in other words is there... (3 Replies)
Discussion started by: npatwardhan
3 Replies
Login or Register to Ask a Question