1

I'm trying to daemonize a script. I followed a couple tutorials and came up with the following script (never done it before, just filled in a template, not sure what 345 70 30 mean):

#!/bin/bash

# parserservices    Parser Services
#
# chkconfig: 345 70 30
# description: Parser Services
# processname: parserservices

# chkconfig --add parserservices

# Source function library.
. /etc/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

prog='ParserServices for CENTOS/UNIX'

start() {
   # Check that networking is up.
   [ "$NETWORKING" = "no" ] && exit 1

        echo -n $"Starting $prog: "
   daemon --check parserservices nohup /home/centos/parserservices/start_dev_server_centos.sh &
   RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/parserservices
   return $RETVAL
}

stop() {
        echo -n $"Shutting down $prog: "
   killproc java
   RETVAL=$?
   echo
   [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/parserservices
   return $RETVAL
}

# See how we were called.
case "$1" in
  start)
   start
        ;;
  stop)
   stop
        ;;
  status)
   status parserservices
   RETVAL=$?
   ;;
  restart|reload)
   stop
   start
   RETVAL=$?
   ;;
  *)
        echo $"Usage: $0 {start|stop|restart|status}"
        RETVAL=3
esac

exit $RETVAL

This is just a test and there won't be any more java processes running, so killing java shouldn't be a problem (unless someone can provide a better alternative, this is intended to run another script that does some initialization and launches java with the Google App Engine development server).

I copied it to /etc/rc.d/init.d/parserservices and initialized and started as shown below:

$ sudo chmod 755 /etc/rc.d/init.d/parserservices
$ sudo chkconfig --add parserservices
$ sudo systemctl daemon-reload    
$ sudo service parserservices start
Starting parserservices (via systemctl):                   [  OK  ]

But nothing gets started. When I run the script myself it all runs fine:

sudo nohup /home/centos/parserservices/start_dev_server_centos.sh &
root     21510  0.0  0.0 189372  2680 pts/1    S    21:18   0:00 sudo nohup /home/centos/parserservices/start_dev_server_centos.sh
root     21511  0.0  0.0 113116  1184 pts/1    S    21:18   0:00 /bin/bash /home/centos/parserservices/start_dev_server_centos.sh
root     21512 27.0  0.2 6619624 37596 pts/1   Sl   21:18   0:00 java -ea -cp /home/centos/gae/appengine-java-sdk-1.9.27/lib/appengine-tools-api.jar com.google.appengine.tools.KickStart com.google.appengine.tools.development.DevAppServ
root     21560  166  0.5 11041848 89188 pts/1  Sl   21:18   0:01 /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.65-3.b17.el7.x86_64/jre/bin/java -Xmx8184m -javaagent:/home/centos/gae/appengine-java-sdk-1.9.27/lib/agent/appengine-agent.jar -Xboo
centos   21578  0.0  0.0 123356  1384 pts/1    R+   21:18   0:00 ps au

By the way, the script's permissions:

-rwxrwxr-x.  1 centos centos  155 Jan 12 20:06 start_dev_server_centos.sh

This is the output of systemctl status parserservices:

$ sudo systemctl status parserservices
parserservices.service - SYSV: Parser Services
   Loaded: loaded (/etc/rc.d/init.d/parserservices)
   Active: active (exited) since Tue 2016-01-12 21:05:06 UTC; 18h ago

Jan 12 21:05:06 curator.novalocal parserservices[21266]: Starting ParserServices for CENTOS/UNIX:
Jan 12 21:05:06 curator.novalocal parserservices[21266]: /etc/rc.d/init.d/parserservices: Usage: daemon [+/-nicelevel] {program}
Jan 12 21:05:06 curator.novalocal systemd[1]: Started SYSV: Parser Services.
Jan 12 21:10:13 curator.novalocal systemd[1]: Started SYSV: Parser Services.
Josep Valls
  • 5,483
  • 2
  • 33
  • 67
  • Is there any way to debug what's happening when I run `sudo service parserservices start`? I can't find any log anywhere. – Josep Valls Jan 12 '16 at 22:07
  • Should I try getting supervisord instead? – Josep Valls Jan 12 '16 at 22:09
  • What does `systemctl status parserservices` show? – stee1rat Jan 12 '16 at 22:23
  • @stee1rat I added the output to the systemctl command – Josep Valls Jan 13 '16 at 15:46
  • I'm not sure whether the App Engine has a different `daemon`, but Ubuntu's `daemon` doesn't have a `--check` option (see manpage: http://manpages.ubuntu.com/manpages/wily/en/man1/daemon.1.html). Can I ask why you're doing all this instead of creating a systemd unit file? – muru Feb 16 '16 at 19:59
  • @muru, no particular reason. I wanted to know how daemons worked with init.d. Initially systemd seemed like overkill but at this point it's probably equivalent. – Josep Valls Feb 17 '16 at 01:50
  • @JosepValls so, does your `daemon` have a `--check` option? – muru Feb 17 '16 at 01:52
  • Yes, apparently that feature is available in RH/Centos. I just found this question and checked my installation's documentation: http://unix.stackexchange.com/questions/216024/what-is-the-canonical-way-to-start-a-daemon-in-rhel-centos-6-init-script – Josep Valls Feb 17 '16 at 02:04

3 Answers3

2

There is more elegant way to create daemons in CentOS 7.

I see you are mixing classic(SysV) and new(systemd) approach for executing and managing daemons. The classic way, was to create script in /etc/init.d/ as You did, but I see you are using systemctl, which is a part of a systemd. I suggest creating daemon in systemd way.

Remove your script from /etc/init.d and create parserservices.service file in /etc/systemd/system directory, which looks like that:

[Unit]
Description=Parser Services
After=syslog.target

[Service]
ExecStart=/path/to/your/script/start_dev_server_centos.sh

[Install]
WantedBy=multi-user.target

Start it with

systemctl start parserservices

Check it's status with

systemctl status parserservices

If you want it to start at system startup then enable it with

systemctl enable parserservices

Execute systemctl daemon-reload every time you change the .service file.

Read more about daemons in systemd here

1

According to the message, your daemon command seems to be incorrect:

/etc/rc.d/init.d/parserservices: Usage: daemon [+/-nicelevel] {program}

Try to modify (like suggested here)

daemon --check parserservices nohup /home/centos/parserservices/start_dev_server_centos.sh &

to

nohup /home/centos/parserservices/start_dev_server_centos.sh &

If you want to use daemon, try double quotes:

daemon --check parserservices "nohup /home/centos/parserservices/start_dev_server_centos.sh &"

This will move the effect of & to nohup from daemon.

If your daemon does not support --check, remove it. How to check it?

grep -e '--check=' /etc/init.d/functions

--check=?*)

-> supports.

Community
  • 1
  • 1
John_West
  • 2,239
  • 4
  • 24
  • 44
  • worked on my Ubuntu (it has no `chkconfig/systemctl/daemon/functions/sysconfig` and I didn't use the corresponding lines) – John_West Feb 16 '16 at 00:53
  • Thanks, I want to keep the `--check` parameter and it should work according to the documentation and the accepted answer to this question: http://unix.stackexchange.com/questions/216024/what-is-the-canonical-way-to-start-a-daemon-in-rhel-centos-6-init-script – Josep Valls Feb 17 '16 at 02:02
  • Did you check **your** /etc/init.d/function? It should have `--check=?*)` try `grep -e '--check=' /etc/init.d/function`. If yes, try the last command from my answer (updated it) – John_West Feb 17 '16 at 23:34
-1

You can debug the script with the -x option to find out why it is not starting the process as intended.

Instead of calling sudo service parserservices start, try sudo bash -x /etc/rc.d/init.d/parserservices start to see what is going on and where it fails.

neuhaus
  • 3,886
  • 1
  • 10
  • 27
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/11306816) – Toby Speight Feb 18 '16 at 13:45
  • Using `-x` to debug the script should reveal the issue and make solving it possible or even trivial. – neuhaus Feb 18 '16 at 14:30
  • 1
    That still doesn't make this an answer. It's a hint, yes, but not an answer. – Toby Speight Feb 18 '16 at 15:13
  • I prefer to tell people how to learn how to get past the problem instead of giving them an answer and 2 days later they run into the next problem and have to ask again. – neuhaus Feb 18 '16 at 15:21
  • @TobySpeight In his first comment, the original author even asks for help to find out what the script does. Which is exactly what `-x` does. – neuhaus Feb 18 '16 at 15:27
  • 1
    Still better as a comment. – Toby Speight Feb 18 '16 at 15:28
  • https://stackoverflow.com/help/how-to-answer states: "Any answer that gets the asker going in the right direction is helpful". I think we can both agree that my answer was helpful. I don't see why it should be deleted. – neuhaus Feb 18 '16 at 15:35