如何让这个init.d脚本启动服务器重新启动?

我遵循在生产机器上安装Redis的指导(使用chkconfig的CentOS)。

我给出的示例脚本需要参数start来实际启动它,似乎init.d不会(传递参数)。

必须运行的真正的命令是/etc/init.d/redis_6379 start ,但是实际调用的是/etc/inti.d/redis_6379 ,它简单的说就是use start or stop as an argument

因此,当我的服务器重新启动它实际上并没有启动redis。 我应该在这里做什么?

这是最初的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 

如果你想通过命令行启动一个服务,你可以直接在/etc/rc.d/rc.local添加,而不是在init.d中创建一个服务文件。

确保通过chkconfig为脚本添加了服务管理。 使用chkconfig --list查看列表并使用chkconfig --add scriptname如果不存在)。 之后配置运行级别,你希望它被调用。 我猜想这是3,4和5所以: chkconfig --level 345 scriptname on

你应该告诉我们你是如何从init.d运行脚本的

但是这是一个肮脏的解决方法:

改变线

 start) 

 start|'') 

这将使它开始,如果没有参数传递。

Centos redis有一个init脚本和一个chkconfig标题行,声明它将在所有运行级别启动,这是非常糟糕的。 chkconfig用于管理/etc/rc.d中的符号链接

 # chkconfig: - 85 15 

我建议redis是在关键服务启动之后运行在3级的服务(例如sshd)。 在您的测试场景中,在开始生产之前重新启动服务器。 如果redis无法启动(只是在这里发生),你不能启动它在另一个运行级别来解决它。

如果你实现了正确的头文件,你可以使用init和systemd(Fedora)

您应该将以下代码添加到脚本/etc/inti.d/redis_6379status参数由命令service --status-all

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

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

Init.d的日子是编号的,wtf你还在读这个吗? 没有更多的sudo service ,所有新的孩子正在削减syscrtl

现在当然,在我的Ubuntu 17.04服务器上,/ /etc/rc.local根本就不存在

只要写一个新的!

rc.local非常棒,特别是与unix风格的守护进程程序结合在一起…这两个人,我几乎可以称它为一天。

但是,如果您想将rc.local提升到一个新的水平,我将介绍我个人的redis init.d脚本背后的基本思路 – 我们在整个公司的生产服务器上使用的一个脚本:

  1. 抢先关于系统套接字/文件限制的redis投诉

  2. 在一些linux perf中打了一会儿,并以持久的方式使用了sysconf

  3. 自动驾驶仪redis,而我去睡午觉

 #!/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 

接下来,如果我们做的是正确的:

  1. 让我们有很好的习惯案例$金钱数字,集中于开始和停止没有排序通过过度的PID跟踪shenanigans

  2. 利用启动 – 停止守护进程(即,如果没有父进程,则不能由父进程死亡缩短)

 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. 编辑redis.conf来指定daemonize yes 。 使redis成为管理PID文件状态的主要责任方(如果您想知道为什么我们不必在脚本中对它做任何事情,除非从脚本中读取它)
 mkdir /etc/redis echo 'daemonize yes' >> /etc/redis/redis.conf echo 'pidfile /var/run/redis.pid' >> /etc/redis/redis.conf 
  1. 复制并设置执行位后,按名称更新您的rc条目:
 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. 这是完整的示例链接 w /服务安装程序。 再次,一定要编辑confinstall以适合你。 大多数人可能想要删除侦听文件路径,以支持为客户端打开的TCP堆栈w / redis端口号,