Stop and Start Services under RedHat Linux

If you’re used to:

/etc/init.d/named restart
…you may find that:
service named restart
…is an easier way of doing the same thing.

Comments

Lauren's picture
Lauren on June 23, 2004 - 18:47

I am using RedHat Linux version 7.2 and for some reason the Named service keeps stoppin itself. What would cause this and how do I go about fixing the problem so that it won’t continually do that?

Thanks
Lauren

Rob Fletcher's picture
Rob Fletcher on June 24, 2004 - 16:10

I think you made a typo, it is ‘service named restart’ not ‘service restart named’, in my experience the daemon is the second argument.

Also, if you are ‘service httpd restart’ing frequently and you only want to refresh the config files, most daemons allow for ‘service httpd reload’ which is a bit quicker.

(I learned this last week, so now I’m an expert.)

Peter Rukavina's picture
Peter Rukavina on June 24, 2004 - 16:15

Thanks for pointing that out, Rob. Original post has been corrected.

Steven Garrity's picture
Steven Garrity on June 24, 2004 - 17:18

In Fedora Core 2, /sbin is not in the default path for non root users, so you might have to type “sudo /sbin/service named restart”, or “su -” before you “service named restart”

Peter Rukavina's picture
Peter Rukavina on June 24, 2004 - 18:10

Of course if you were a real Linux geek, you would already be ‘root,’ and therefore the need to sudo would be abrogated.

Prajyot Neve's picture
Prajyot Neve on June 28, 2004 - 07:10

Hi, i am prajyot from india. I am using Red Hat Linux 9. What i wannna know is how particular service is started? & what is Loopback concept?
If u know it please mail me at prajyot_neve@yahoo.co.uk.

titi's picture
titi on December 16, 2008 - 05:46

I want creat a Script for Star and Stop the Service in linux.how are you hepl me.thank

Maneesh Joseph's picture
Maneesh Joseph on December 9, 2009 - 14:37

I want to know about ….cron service………I wat to change my iptables rules at specific times…..So how can i add it into my cron services..i want to do it daily 1.00 pm….
I have created 2 iptable files….with two different rules….am trying to restore the two files using cron service at two different times……
but its not working „„,
I am adding my crontab configuration below…

0 13 * * * iptables-restore /etc/sysconfig/iptables
30 13 * * * iptables-restore /etc/sysconfig/iptables2

I am using Fedora Core9….I have checked everything. Crone service is started……
Anybody can find a soultion…….please help me…….

kumar007's picture
kumar007 on May 3, 2013 - 22:19

I have tried my best and this is pretty much you want it. However, single error I am trying to fixLast

#!/bin/sh
##======================================================================================================
# Script : Process Monitoring
# Author : Randy Michael
# Date : 02/14/2007
# Rev : 4.1.P
#Platform: Not Platform dependent

# Purpose : This script is used to monitor a process and restart
# the service if stops. This script also logs all service
# stops and starts for system admin purposes. If the process is
# not running when this script is executed, it will start the
# process at that time.
#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# REV LIST:.4.1.P
# Date: 05/03/2013
# By: PrafulKumar Busa
# Modification:Fixed the error and made this sript runing
#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# set -n : Uncomment to check script syntax, without execution.
# Note: Do not forget to put the comment back in or
# the shell script will not execute!
# set -x : Uncomment to debug this shell script

###==================================================================================================
#
# DEFINE VARIABLES HERE
#
##===================================================================================================

PROC_MON=`basename $0` # Defines the script_name variable as the file name of this script

LOGFILE=”/Users/pdbusa/Desktop/procmon.log” # Shows log file and where located
[ ! -s $LOGFILE ] && touch $LOGFILE # This checks to see if the file exists
# if not it creates one.

TTY=$(tty) # Current tty or pty

PROCESS=”sshd” # This will define which process to monitor
target=”ssh”
SLEEP_TIME=”5” # This is the sleep time in second between monitoring

txtred=$(tput setaf 1) # Red: will indicate a failed process and the information
txtgrn=$(tput setaf 2) # Green: this is successful process information
txtylw=$(tput setaf 3) # Yellow: this is used to show cautionary information
txtrst=$(tput sgr0) # resets text

##===================================================================================================
# *
# DEFINE FUNCTION HERE *
# *
##===================================================================================================

exit_trap() # this is the behavior of the trap signal
{
# Log an ending time for process monitoring
DATE=$(date +%D)
TIME=$(date +%T) # Get a new timestamp…
echo “$DATE @ $TIME: Monitoring for $PROCESS terminated” » $LOGFILE & # this will create an entry in the logfile
echo “$DATE @ $TIME: ${txtred}Monitoring for $PROCESS terminated${txtrst}”

#kill all functions
kill -9 $(jobs -p) 2>/dev/null
}
##===================================================================================================
# *
# START OF THE MAIN *
# *
##===================================================================================================

#=================
# set a trap…***
#=================

trap ‘exit_trap; exit 0’ 1 2 3 15

# this will see if process is running if not will start it

ps aux | grep “$target” | grep -v “grep $target” \
| grep -v $PROC_MON >/dev/null

if (($? != 0))
then
DATE=$(date +%D)
TIME=$(date +%T)
echo
echo “$DATE @ $TIME: $PROCESS is NOT active…starting $PROCESS..” » $LOGFILE & # creates
# an entry in the logfile
echo “$DATE @ $TIME: ${txtylw}$PROCESS is NOT active…starting $PROCESS..${txtrst}”
echo
sleep 1
service $PROCESS start &
echo “$DATE @ $TIME: $PROCESS has been started…” » $LOGFILE & #puts an enrty in logfile
else # this will say what to do if process is already running
echo “\n” # a blank line
DATE=$(date +%D)
TIME=$(date +%T)
echo “$DATE @ $TIME: $PROCESS is currnetly RUNNING…” » $LOGFILE & # puts entry in logfile
echo “$DATE @ $TIME: ${txtgrn}$PROCESS is currently RUNNING…${txtrst}”
fi

##===================================================================================================
# loop until the $process stops and fails to start
# the first part of this section creates a loop that checks to see if the specified
# process is still running and will restart the process
RC=1
while [ $RC = 0 ] # this will loop until the return code is not zero
do
ps aux | grep $PROCESS | grep -v “grep $PROCESS” \
| grep -v $PROC_MON >/dev/null 2>&1
if (( $? != 0 )) # check the return code
then
echo
DATE=$(date +%D)
TIME=$(date +%T)
echo “$DATE @ $TIME: $PROCESS has STOPPED…” » $LOGFILE & # entry in logfile
echo “$DATE @ $TIME: ${txtred}$PROCESS has STOPPED…${txtrst}”
echo
service $PROCESS start &
echo “$DATE @ $TIME: $PROCESS has RESTARTED…” » $LOGFILE & # ENTRY IN LOGFILE
echo “$DATE @ $TIME: ${txtgrn}$PROCESS has RESTARTED…${txtrst}”
sleep 1

# This section will verify that the service did restart and if the process failed to restart,
# exits the monitoring script.
# This will be code to email the admin that the server has gone down

ps aux | grep $PROCESS | grep -v “grep $PROCESS” \
| grep -v $PROC_MON >/dev/null 2>&1
if (( $? != 0 )) # This will check the return code
then
echo
DATE=$(date +%D) # New time stamp
TIME=$(date +%T)
echo “$DATE @ $TIME: $PROCESS failed to restart…” » $LOGFILE & #entry in logfile
echo “$DATE @ $TIME: ${txtred}$PROCESS failed to restart…${txtrst}”
exit 0
fi
fi
sleep $SLEEP_TIME # This is needed to reduce CPU Load!!!
done

##===================================================================================================
#End of script

Add new comment