LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Starting a service at startup. (https://www.linuxquestions.org/questions/linux-newbie-8/starting-a-service-at-startup-807094/)

pinga123 05-11-2010 12:04 AM

Starting a service at startup.
 
Hi guys I want to start a service and a script SiteMonitor.sh at startup.

Here is brief working of SiteMonitor.sh
(This script monitors the ip addresses of machines which are using http service of the host.
This script checks this after every 5 seconds therefore i have run it at background.
)
To start with i have modified /etc/rc.local file.

Here is the content of my /etc/rc.local file.

Code:

#!/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.

touch /var/lock/subsys/local
/etc/init.d/oc4j start
/usr/local/sbin/SiteMonitor.sh &
service httpd start

My problem is after i restart the machine only SiteMonitor.sh script get
executed but the command service httpd start doesnt .Therefore i have to manually login to the system and fire service httpd start to start apache.

I would also like to know whether the script SiteMonitor.sh is running or not?
How would i do that?

chrism01 05-11-2010 12:11 AM

Normally you set it on using the chkconfig cmd http://linux.die.net/man/8/chkconfig or the equiv. You don't need an entry in/etc/rc.local.

pinga123 05-11-2010 12:15 AM

Quote:

Originally Posted by chrism01 (Post 3964192)
Normally you set it on using the chkconfig cmd http://linux.die.net/man/8/chkconfig or the equiv. You don't need an entry in/etc/rc.local.

are you referring script or service or both?
What should be the runlevel for apache service?
What should be start and end priority?

The reason i m using rc.local file is beacause one of our senior member has suggested that in following thread.
http://www.linuxquestions.org/questions/linux-newbie-8/need-to-run-script-at-startup-786850/

ajeetsinghraina 05-11-2010 12:20 AM

Say, SiteMonitor.sh is under /opt/ directory.

Just browse to :

#vi /etc/rc.local

And add:

sh /opt/SiteMonitor.sh at the last line

Save it.

Note: Make sure the script has executable permission.

senthilvael 05-11-2010 12:21 AM

Hi,

Please check the following link. Monit will do exactly what you need.

http://mmonit.com/monit/

To start the http service, at boot time,

PHP Code:

[root@server ~]# chkconfig --list httpd
httpd           0:off   1:off   2:off   3:off   4:off   5:off   6:off 

This shows on my runlevel (3), the http service is off by default.

PHP Code:

[root@server ~]# chkconfig httpd on 

Now i am changing the httpd to be started by default in my runlevel

PHP Code:

[root@server ~]# chkconfig --list httpd
httpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off 

Verifying.








Quote:

Originally Posted by pinga123 (Post 3964185)
Hi guys I want to start a service and a script SiteMonitor.sh at startup.

Here is brief working of SiteMonitor.sh
(This script monitors the ip addresses of machines which are using http service of the host.
This script checks this after every 5 seconds therefore i have run it at background.
)
To start with i have modified /etc/rc.local file.

Here is the content of my /etc/rc.local file.

Code:

#!/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.

touch /var/lock/subsys/local
/etc/init.d/oc4j start
/usr/local/sbin/SiteMonitor.sh &
service httpd start

My problem is after i restart the machine only SiteMonitor.sh script get
executed but the command service httpd start doesnt .Therefore i have to manually login to the system and fire service httpd start to start apache.

I would also like to know whether the script SiteMonitor.sh is running or not?
How would i do that?


pinga123 05-11-2010 12:30 AM

Quote:

Originally Posted by ajeetsinghraina (Post 3964197)
Say, SiteMonitor.sh is under /opt/ directory.
Just browse to :
#vi /etc/rc.local
And add:
sh /opt/SiteMonitor.sh at the last line
Save it.
Note: Make sure the script has executable permission.

That was not my question.
My SiteMonitor.sh Script ran successfully at background I just wanted to know How would i determine whether the script is running or not.
May be using ps -ef (or something like this).


I have copied SiteMonitor.sh to /usr/local/sbin/ which is in my $PATH
I just added following line to /etc/rc.local file
Code:

/usr/local/sbin/SiteMonitor.sh &
Is there anything wrong here ?

ajeetsinghraina 05-11-2010 12:39 AM

If that the condition then it should go fine.
ps -ef can fetch you the process.Thats true.

pinga123 05-11-2010 03:14 AM

That was not my question.

I have ran SiteMonitor.sh in background .
using
Quote:

/usr/local/sbin/SiteMonitor.sh &
command.

Now if i want to see whether the script is running or not ,How would i go about it?

senthilvael 05-12-2010 02:04 AM

Code:

ps -aux|grep SiteMonitor.sh





Quote:

Originally Posted by pinga123 (Post 3964350)
That was not my question.

I have ran SiteMonitor.sh in background .
usingcommand.

Now if i want to see whether the script is running or not ,How would i go about it?


pinga123 05-12-2010 11:27 PM

Quote:

Originally Posted by senthilvael (Post 3965411)
Code:

ps -aux | grep SiteMonitor.sh

It is giving me an error saying bad syntax
# ps -aux | grep SiteMonitor.sh
Code:

Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.3/FAQ
root      1201  0.0  0.0  5412  632 pts/1    S+  09:41  0:00 grep SiteMonitor.sh


chrism01 05-13-2010 02:47 AM

ps -ef | grep SiteMonitor.sh

pinga123 05-13-2010 04:09 AM

Quote:

Originally Posted by chrism01 (Post 3966700)
ps -ef | grep SiteMonitor.sh

Again not what i want to see.
Quote:

# ps -ef | grep SiteMonitor.sh
root 21147 21051 0 14:23 pts/1 00:00:00 grep SiteMonitor.sh
As the process is started with SiteMonitor.sh &
there must be something that will be available to see the status.

alli_yas 05-13-2010 04:20 AM

Quote:

I would also like to know whether the script SiteMonitor.sh is running or not?
How would i do that?
Hi

As per above you want to see if your script is running or not.

The previous command that you ran (ps -ef | grep sitemonitor.sh) basically will show you whether your script has been spawned as a process or not - thus if you don't see your script with a process ID it means that its not running.

Cheers
Yas

pinga123 05-13-2010 11:34 PM

Quote:

Originally Posted by alli_yas (Post 3966754)
Hi

As per above you want to see if your script is running or not.

The previous command that you ran (ps -ef | grep sitemonitor.sh) basically will show you whether your script has been spawned as a process or not - thus if you don't see your script with a process ID it means that its not running.

Cheers
Yas

Script was running When i executed the command .I dont know why it was not showing up in ps -ef.
I think for listing the processes started with & at the end there must be something else .

alli_yas 05-14-2010 12:42 AM

Hi

Yes - you're quite right. When you run the script with the ampersand (&) at the end the script is run in the background in a sub-shell.

Here's how you see whether your script is running or not:

Code:

# echo $!
9261
# ps -ef | grep 9261
root      9261  9150  0 07:37 pts/1    00:00:00 ./myscript.sh
root      9285  9150  0 07:39 pts/1    00:00:00 grep 9261

Basically, when you issue the echo$! it will show you your background process - and then you can use ps for that PID to be able to see the state of your process.

I suggest you read the bash man page which gives a bit of insight into this.

Cheers
Yas


All times are GMT -5. The time now is 04:41 AM.