How to run this init.d script to restart the server?

I follow the instructions for installing Redis on a production machine (CentOS using chkconfig).

For the sample script, I need the start argument to run it, which seems that init.d does not (passes arguments).

The real command that needs to be run is /etc/init.d/redis_6379 start , but what it actually calls /etc/inti.d/redis_6379 , which simply says use start or stop as an argument

Therefore, when my server reboots, it does not actually start redis. What should I do here?

Here is the initial configuration

 #!/bin/sh # # Simple Redis init.d script conceived to work on Linux systems # as it does use of the /proc filesystem. # # chkconfig: - 85 15 # description: Redis is a persistent key-value database # processname: redis_6379 REDISPORT=6379 EXEC=/usr/local/bin/redis-server CLIEXEC=/usr/local/bin/redis-cli PIDFILE=/var/run/redis_${REDISPORT}.pid CONF="/etc/redis/${REDISPORT}.conf" case "$1" in start) if [ -f $PIDFILE ] then echo "$PIDFILE exists, process is already running or crashed" else echo "Starting Redis server..." $EXEC $CONF fi ;; stop) if [ ! -f $PIDFILE ] then echo "$PIDFILE does not exist, process is not running" else PID=$(cat $PIDFILE) echo "Stopping ..." $CLIEXEC -p $REDISPORT shutdown while [ -x /proc/${PID} ] do echo "Waiting for Redis to shutdown ..." sleep 1 done echo "Redis stopped" fi ;; *) echo "Please use start or stop as first argument" ;; esac 
+4
source share
6 answers

Make sure your script is added to control the service on chkconfig . Use chkconfig --list to view the list, and use chkconfig --add scriptname if it does not exist. After that, configure the runlevels you want them to call. I would suggest that these are 3, 4, and 5 like this: chkconfig --level 345 scriptname on .

+3
source

You must tell us exactly how you run the script from init.d

But here is a dirty workaround:

Change the line

 start) 

to

 start|'') 

This will start it if the parameters are not passed.

+1
source

If you want to start the service through the command line, you can simply add it to /etc/rc.d/rc.local for it too, and not to create a service file in init.d.

+1
source

Centos redis has an init script with a chkconfig title bar that says it will start at all run levels, which is very bad. chkconfig is used to manage symbolic links in /etc/rc.d

 # chkconfig: - 85 15 

I suggest that redis be a service to run at level 3 after starting critical services (e.g. sshd). In the test case, restart your server before proceeding with production. If redis cannot start (just happened), you cannot load it at another runlevel to fix it.

If you use the appropriate headers, you can use init as well as systemd (Fedora)

0
source

Add the code below to the script /etc/inti.d/redis_6379 . The status argument is used by the service --status-all command.

 # processname: redis_6379 # Source function library. . /etc/init.d/functions 

...

 case "$1" in status) status -p $PIDFILE redis script_result=$? ;; 
0
source

Ignored days are Init.d, wtf are you still reading this? There is no more sudo service , all new children are floating down syscrtl

Currently, for example, on my ubuntu 17.04 production server, /etc/rc.local does not even exist

Just write a new one!

rc.local is amazing, especially when combined with the unix demonize program style ... these two, I can pretty much call it a day.

However, if you want to take rc.local to the next level, I will talk about the basic ideas of my own personal redis init.d script - the same one that we use on production servers in my company:

  • pre-empt redis complaint about system sockets / file limits

  • slap on some linux perf and mess with sysconf in constant mode

  • autopilot redis while i go nap

 #!/bin/sh ### BEGIN INIT INFO # Provides: redis # Required-Start: $syslog # Required-Stop: $syslog # Should-Start: $all # Should-Stop: $all # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # X-Interactive: true # Short-Description: start and stop redis # Description: persistent key-value db ### END INIT INFO NAME=redis PATH=/opt/bin:/opt/sbin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin EXEC=/opt/sbin/redis-server CLIEXEC=/opt/sbin/redis-cli CONF=/etc/$NAME/$NAME.conf PIDFILE=/var/run/$NAME.pid SOCKET=/var/run/$NAME.sock PERF=/tmp/redis.sysctl KERNELPG=/sys/kernel/mm/transparent_hugepage/enabled [ -x /opt/sbin/redis-server ] || exit 0 set -e # tune system for better redis performance if [ ! -f $PERF ]; then echo "tunning redis..." &>> $PERF echo never > $KERNELPG && cat $KERNELPG &>> $PERF sysctl -w net.core.somaxconn=65535 &>> $PERF sysctl -w vm.overcommit_memory=1 &>> $PERF echo "tuned." &>> $PERF && cat $PERF fi 

next if we do it right:

  1. let them have good idiomatic cases of $ money numbers, oriented to start and stop without sorting through excessive tracking PID shenanigans

  2. use the start-stop daemon (i.e. the death of the parent process cannot be interrupted if there is no parent process)

 case $1 in start) if [ ! -f $PIDFILE ]; then echo -n "Starting $NAME: " start-stop-daemon --start --pidfile $PIDFILE --exec $EXEC -- $CONF echo "waiting for redis db to start..." while [ ! -f $PIDFILE ]; do sleep 0.1; done fi PID=$(cat $PIDFILE) echo "running with pid: $PID" ;; stop) if [ ! -f $PIDFILE ]; then echo "redis is already stopped" else PID=$(cat $PIDFILE) echo -n "Stopping $NAME: " $CLIEXEC -s $SOCKET shutdown echo "waiting for shutdown..." while [ -x /proc/${PID} ]; do sleep 0.1 done echo "db stopped." fi ;; status) if [ -f $PIDFILE ]; then PID=$(cat $PIDFILE) echo "running with pid: $PID" else echo "stopped." fi ;; restart|force-reload) $0 stop && $0 start ;; *) echo "Argument \"$1\" not implemented." exit 2 ;; esac exit 0 
  1. modify redis.conf to indicate daemonize yes . Make redis the main responsible party for the administrative state of the PID file (in case you are wondering why we did not need to do anything with it in the script, except for reading from it, if any)
 mkdir /etc/redis echo 'daemonize yes' >> /etc/redis/redis.conf echo 'pidfile /var/run/redis.pid' >> /etc/redis/redis.conf 
  1. update rc record by name after copying and setting executable bits:
 mkdir /etc/redis vim /etc/redis/redis # keep it traditional, no .sh extensions here # saving buffers from root all damn day... chmod a+x /etc/init.d/redis update-rc.d redis defaults 
  1. Here is a complete example of a link with a service installer. Again, be sure to edit conf and install for you. Most people will probably want to remove the listening file path in favor of the TCP stack w / redis the port number open to clients (s),
0
source

Source: https://habr.com/ru/post/1501133/


All Articles