LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-08-2009, 01:57 PM   #1
niravshah
Member
 
Registered: Jul 2009
Posts: 83

Rep: Reputation: 16
Bash script to execute on startup


Hi all,

I want to write a script to run at startup after every service is started.
But this script will also reboot the machine after some time interval and should again execute the script when the machine reboots

Can you tell me how to go about it.
 
Old 09-08-2009, 02:23 PM   #2
nuwen52
Member
 
Registered: Feb 2009
Distribution: Debian, CentOS 5, Gentoo, FreeBSD, Fedora, Mint, Slackware64
Posts: 208

Rep: Reputation: 46
A lot depends on the distro you are using. Many now have a local.start (or something like that) which runs specifically after all other services have been started. You would place a call to your script into that file.

As for restarting. You could set a sleep in your script so that after the sleep is over, it spawns a restart of the system. Or, on boot, you could add a cron entry to spawn a reboot at a pre determined time after boot. Ex. system boots at 08:30 and your reboot time is 30 minutes after, you create a cron entry which causes reboot at 09:00.

It might be easier to help if we knew exactly what distro you were using.
 
Old 09-08-2009, 02:32 PM   #3
niravshah
Member
 
Registered: Jul 2009
Posts: 83

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by nuwen52 View Post
A lot depends on the distro you are using. Many now have a local.start (or something like that) which runs specifically after all other services have been started. You would place a call to your script into that file.

As for restarting. You could set a sleep in your script so that after the sleep is over, it spawns a restart of the system. Or, on boot, you could add a cron entry to spawn a reboot at a pre determined time after boot. Ex. system boots at 08:30 and your reboot time is 30 minutes after, you create a cron entry which causes reboot at 09:00.

It might be easier to help if we knew exactly what distro you were using.
I am using Red Hat 5 distro and i want to do it repeatedly
So here is the complete sequence
the script reboots the server
waits for all the services to start
sleep for about 6 minutes
monitor some log files(look for specific things)
sleep for few minutes
reboot

This would go on for like the whole day i am basically doing stress test
 
Old 09-08-2009, 02:40 PM   #4
jstephens84
Senior Member
 
Registered: Sep 2004
Location: Nashville
Distribution: Manjaro, RHEL, CentOS
Posts: 2,098

Rep: Reputation: 102Reputation: 102
I would create the script then make a symlink under /etc/rcX.d with the following S99myscript. replace the x with whatever your default startup level is. If you are not sure what it is look under /etc/inittab
 
Old 09-08-2009, 04:25 PM   #5
nuwen52
Member
 
Registered: Feb 2009
Distribution: Debian, CentOS 5, Gentoo, FreeBSD, Fedora, Mint, Slackware64
Posts: 208

Rep: Reputation: 46
After looking at my CentOS 5.3 (which is very similar to Redhat 5), I agree with jstephens84's suggestion, except that I would create the name as S99zz_myscript. On my machine, I have a S99 script whose name starts with a z. So, by naming this script as zz_, it should start your script absolutely last. At boot, the script would take your current time and add 6 minutes, and write a cron entry to shutdown at that time. Then, kick off the monitoring software (as required) in a background call to another script.

Last edited by nuwen52; 09-08-2009 at 04:26 PM.
 
Old 09-08-2009, 04:30 PM   #6
jstephens84
Senior Member
 
Registered: Sep 2004
Location: Nashville
Distribution: Manjaro, RHEL, CentOS
Posts: 2,098

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by nuwen52 View Post
After looking at my CentOS 5.3 (which is very similar to Redhat 5), I agree with jstephens84's suggestion, except that I would create the name as S99zz_myscript. On my machine, I have a S99 script whose name starts with a z. So, by naming this script as zz_, it should start your script absolutely last. At boot, the script would take your current time and add 6 minutes, and write a cron entry to shutdown at that time. Then, kick off the monitoring software (as required) in a background call to another script.
I don't think the z matters. it should only be S or K (in capitals also) and the number is the execution order with 99 being last. off the top of my head I can't remember the difference between S and K.
 
Old 09-08-2009, 05:29 PM   #7
niravshah
Member
 
Registered: Jul 2009
Posts: 83

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by jstephens84 View Post
I don't think the z matters. it should only be S or K (in capitals also) and the number is the execution order with 99 being last. off the top of my head I can't remember the difference between S and K.
Could tell me where can i find a step by step instructions on this
 
Old 09-08-2009, 05:35 PM   #8
jstephens84
Senior Member
 
Registered: Sep 2004
Location: Nashville
Distribution: Manjaro, RHEL, CentOS
Posts: 2,098

Rep: Reputation: 102Reputation: 102
All you would do is go to /etc/rcX.d. then type in vim S99myscript
Code:
/<path to script/script.sh
then save and exit. course you will probably want to add more error checking. Again if you don't know what level you start in by default then just look for the following

# The default runlevel.
id:2:initdefault:


Yours may say 2 or 3 or 4 or 5. This came from a debian machine so this is the default for debian.

If this doesn't answer your question do you care to elaborate on your question as I was a little lost on what you where asking when you said

Quote:
Could tell me where can i find a step by step instructions on this
 
Old 09-08-2009, 08:05 PM   #9
r3sistance
Senior Member
 
Registered: Mar 2004
Location: UK
Distribution: CentOS 6/7
Posts: 1,179

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by jstephens84 View Post
I don't think the z matters. it should only be S or K (in capitals also) and the number is the execution order with 99 being last. off the top of my head I can't remember the difference between S and K.
I would hazard a guess at S = Start and K = Kill. Any starting script you wished Started at a certain run level should start with an S what will run the start script, and any script that kills or stops something should be run with a K and runs the stop script, if I remember it correctly. As these are suppose to be symlinks to a script that contains both start and stop sections. Of course I could have got that wrong...

Last edited by r3sistance; 09-08-2009 at 08:08 PM.
 
Old 09-08-2009, 08:12 PM   #10
jstephens84
Senior Member
 
Registered: Sep 2004
Location: Nashville
Distribution: Manjaro, RHEL, CentOS
Posts: 2,098

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by r3sistance View Post
I would hazard a guess at S = Start and K = Kill. Any starting script you wished Started at a certain run level should start with an S what will run the start script, and any script that kills or stops something should be run with a K and runs the stop script, if I remember it correctly. As these are suppose to be symlinks to a script that contains both start and stop sections. Of course I could have got that wrong...
That would be correct in that they are symlinks. Most of them will point to something like /etc/init.d/<service name> however on some systems. it will just be an actuall startup script. I think and example would be arch linux which makes use of /etc/rc.d/<script name>.
 
Old 09-09-2009, 09:07 AM   #11
nuwen52
Member
 
Registered: Feb 2009
Distribution: Debian, CentOS 5, Gentoo, FreeBSD, Fedora, Mint, Slackware64
Posts: 208

Rep: Reputation: 46
I must admit that I'm not sure that the "z" part matters. I was just thinking that because S##<script> is the normal naming convention, and the ## is always 2 digit. Then it follows that the init system might use a simple alphanumeric sort to determine order. So, the "zz" would place it at the very end. I don't know that this is true. It just seemed logical.

Also, so that you get the syntax of the script right, I would just use one of the simple scripts in /etc/init.d and copy it to your new script as a template.

Good Luck

Last edited by nuwen52; 09-09-2009 at 09:09 AM.
 
Old 09-09-2009, 08:16 PM   #12
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,260

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Personally, for a (presumably temp) situation like this, a full-blown init script is overkill. I'd just add it to /etc/rc.local.
That'd be sufficient & much simpler.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
How to execute bash script in C waqasdaar Programming 15 10-27-2015 03:54 PM
Can't execute Bash script. zbe Linux - Software 4 10-17-2008 09:05 AM
Auto execute shell script on startup, right before use sgware Fedora 2 11-13-2007 01:10 AM
auto execute shell script on startup slackwbas Slackware 9 02-27-2005 11:26 AM
IceWM doesn't execute my startup script martinpi Linux - Software 1 05-28-2004 03:24 AM


All times are GMT -5. The time now is 03:51 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration