LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 08-03-2016, 01:57 PM   #1
efetzer
LQ Newbie
 
Registered: Apr 2015
Posts: 25

Rep: Reputation: Disabled
RedHat Service


My RedHat Service throws errors stopping and uninstalling. I end up having to kill it manually. Here's the /etc/init.d script:

Code:
#!/bin/sh 
### BEGIN INIT INFO 
# Provides:          MYAPP 
# Required-Start:    $local_fs $network $named $time $syslog 
# Required-Stop:     $local_fs $network $named $time $syslog 
# Default-Start:     2 3 4 5 
# Default-Stop:      0 1 6 
# Description:       MYAPP Service 
### END INIT INFO 

 
SCRIPT=/opt/MYAPP/startMyApp.sh 
RUNAS=me

 
PIDFILE=/var/log/wmlogs/MYAPP/MYAPP.pid 
LOGFILE=/var/log/wmlogs/MYAPP/MYAPP.log 

 
start() { 
  if [ -f /var/run/$PIDNAME ] && kill -0 $(cat /var/run/$PIDNAME); then 
    echo 'Service already running' >&2 
    return 1 
  fi 
  echo 'Starting service<85>' >&2 
  local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!" 
  su -c "$CMD" $RUNAS > "$PIDFILE" 
  echo 'Service started' >&2 
} 

 
stop() { 
  if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then 
    echo 'Service not running' >&2 
    return 1 
  fi 
  echo 'Stopping service<85>' >&2 
  kill -$(cat "$PIDFILE") && rm -f "$PIDFILE" 
  echo 'Service stopped' >&2 
} 

 
uninstall() { 
  echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] " 
  local SURE 
  read SURE 
  if [ "$SURE" = "yes" ]; then 
    stop 
    rm -f "$PIDFILE" 
    echo "Notice: log file is not be removed: '$LOGFILE'" >&2 
    update-rc.d -f MYAPP remove 
    rm -fv "$0" 
  fi 
} 

 
case "$1" in 
  start) 
    start 
    ;; 
  stop) 
    stop 
    ;; 
  uninstall) 
    uninstall 
    ;; 
  retart) 
    stop 
    start 
    ;; 
  *) 
    echo "Usage: $0 {start|stop|restart|uninstall}" 
esac
I grabbed it and modified it from a "how to" site...

Thanks,
Eric
 
Old 08-03-2016, 02:13 PM   #2
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

Rep: Reputation: Disabled
Maybe I missed something, but should PIDNAME in your start() func, actually be PIDFILE ???
 
Old 08-03-2016, 02:32 PM   #3
efetzer
LQ Newbie
 
Registered: Apr 2015
Posts: 25

Original Poster
Rep: Reputation: Disabled
Yeah, that's funny. Of course start works, lol...
 
Old 08-03-2016, 03:05 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Just saying throws errors does not help much.

update-rc.d is a debian command not Redhat. The equivalent is chkconfig.

Just for starters...

Does the contents of your pidfile actually contain the pid number?

Looks like a syntax error in this command
kill -$(cat "$PIDFILE") && rm -f "$PIDFILE"

Last edited by michaelk; 08-03-2016 at 03:07 PM.
 
Old 08-03-2016, 05:30 PM   #5
efetzer
LQ Newbie
 
Registered: Apr 2015
Posts: 25

Original Poster
Rep: Reputation: Disabled
Hmmm, seems it may be easier to start over. Is there a better template you could suggest?
 
Old 08-03-2016, 05:34 PM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
What version of RH are you running?

You should be able to take an existing script on your system and modify it as desired.

Last edited by michaelk; 08-03-2016 at 05:36 PM.
 
Old 08-03-2016, 10:07 PM   #7
efetzer
LQ Newbie
 
Registered: Apr 2015
Posts: 25

Original Poster
Rep: Reputation: Disabled
Rhel 6.8
 
Old 08-03-2016, 10:27 PM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
This might help
http://www.thegeekstuff.com/2012/03/lsbinit-script
 
Old 08-03-2016, 10:30 PM   #9
efetzer
LQ Newbie
 
Registered: Apr 2015
Posts: 25

Original Poster
Rep: Reputation: Disabled
Thanks! I'll get that going when I get back to work Monday! Time for fishing now... ;0)
 
Old 08-12-2016, 11:02 AM   #10
efetzer
LQ Newbie
 
Registered: Apr 2015
Posts: 25

Original Poster
Rep: Reputation: Disabled
OK, I've got it coded up, but one thing is puzzling me. It seems to only run as root. Is there a way to add a "runas" option? Also, that article has me running:

update-rc.d filename defaults

in order to create the service. I'm more familiar to doing it this way:

>chkconfig --add MyService
>chkconfig --level 2345 MyService on

Are there advantages to using update-rc.d? Thanks!
 
Old 08-12-2016, 11:19 AM   #11
efetzer
LQ Newbie
 
Registered: Apr 2015
Posts: 25

Original Poster
Rep: Reputation: Disabled
I guess I found it:

http://stackoverflow.com/questions/7...-specific-user

So: --chuid someuser:somegroup

And I also see by looking around that this script is based on using update-rc.d rather than chkconfig. So is update-rc.d the Linux "best practice"? Sorry for all the questions, trying to get my head around this for future consideration... Thanks!
 
Old 08-12-2016, 03:18 PM   #12
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
It depends on the distribution. In the referenced thread the OP is running Ubuntu which is debian based which uses update-rc.d. Redhat based distributions use chkconfig but with the lastest using systemd it is completely different.
 
Old 08-13-2016, 06:45 AM   #13
efetzer
LQ Newbie
 
Registered: Apr 2015
Posts: 25

Original Poster
Rep: Reputation: Disabled
So this isn't going to work at all when I try to implement it in RHEL 6.8?
 
Old 08-13-2016, 06:51 AM   #14
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
not as is. it can be "converted"
 
Old 08-13-2016, 04:00 PM   #15
efetzer
LQ Newbie
 
Registered: Apr 2015
Posts: 25

Original Poster
Rep: Reputation: Disabled
How? By making it look like my original script with a few fixes?
 
  


Reply



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 Add A New Service In Redhat 7.1 redhatwannabe Linux - Newbie 1 01-19-2016 11:23 PM
MySQL service is not starting in Redhat AS 4.0 mandla008 Linux - Networking 2 04-29-2006 08:58 AM
Redhat Directory Service shparker0624 Linux - Enterprise 0 02-06-2006 07:15 AM
Adding Service RedHat 9 green123 Linux - Newbie 4 08-26-2003 01:27 PM
redhat service config Henk Linux - General 0 07-31-2003 05:08 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 05:03 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
Open Source Consulting | Domain Registration