November 22, 2024, 03:34:24 AM

News : LinuxSolved.com Linux Help Community Forum..


Author Topic: How to run a shell script using service command  (Read 7698 times)

Offline sridip

  • Linux Noob !
  • *
  • Posts: 9
How to run a shell script using service command
« on: August 10, 2005, 10:07:05 AM »
I want to run a shell script using service command
as service <script> start
    service <script> stop
how can i do this ?

Offline misterscorp

  • New Member
  • Posts: 3
using service
« Reply #1 on: October 07, 2005, 04:45:10 PM »
Hi,
  Just write the script to do your specific function & put the shell file in the /etc/rc.d/ directory
  Before writing the script file just get a glance of how the other service deamons are written. For example if you have webmin(Administration tool) installed in your linux box you can go and find its script in /etc/rc.d/rc5.d/
the name of the file would be S99webmin
plz do read that file. You will get the idea how they are invoking a deamon using service command. Actually they will be having a seperate file that can handle the start|stop|restart and how these functionalities are acheived can be better undestood if you read that script file.


I think this would have satisfied your questions,
 :D :D  8)

Offline dragoncity99

  • LST CareTaker
  • Experienced
  • *****
  • Posts: 551
How to run a shell script using service command
« Reply #2 on: May 07, 2006, 01:13:36 PM »
Ok make sure u know what u r doing sridip.

U must make sure ur Linux distro is either using

System V or BSD style of managing runlevel.

System V is normally used, coz it's cool way of handling runlevels.

BSD which is used by Slackware is different.


I suggest u do a reading first on this topic. If ur using distros such as RedHat/SUSE which uses System V,

U need to add ur script into /etc/rc.d/init.d directory.

/etc/rc0.d
/etc/rc1.d
/etc/rc2.d
/etc/rc3.d
/etc/rc4.d
/etc/rc5.d
are all symbolics to that script in /etc/rc.d/init.d

u don't have to manually create it. There are tools that help u to do it, if ur using Redhat

Offline dragoncity99

  • LST CareTaker
  • Experienced
  • *****
  • Posts: 551
How to run a shell script using service command
« Reply #3 on: May 07, 2006, 01:17:10 PM »
Sorry for reposting, but if u want to just get things done and not challenging urself or being curious, a QUICK FIX, will be:

1. Run the script in background
# ./yourShellScript.sh &

2. That's it. If you wanna stop it, u can just find the PID, using:
# ps aux | grep yourShellScript.sh

3. Kill the pid:
# kill -9 PID-OF-YOUR-SHELL-SCRIPT