安装了一个amoeba,这个并不是系统服务,要设置开机启动,于是就去修改/etc/rc.local文件,用了绝对路径,在命令行下测试是可以成功启动的,但是开机不生效,于是想把这个设置成系统的服务,之前没做过,记录下
1.照格式写shell

vi /etc/init.d/amoeba
#!/bin/bash
#
# auditd        This starts and stops auditd
#
# chkconfig: 2345 11 88
# description: This starts the Linux Auditing System Daemon, \
#              which collects security related events in a dedicated \
#              audit log. If this daemon is turned off, audit events \
#              will be sent to syslog.
#
#
######forjava
export JAVA_HOME=/usr/java/jdk1.6.0_45
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/jre/lib:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar
#######

###amoeba
export AMOEBA_HOME=/usr/local/amoeba/
export PATH=$PATH:$AMOEBA_HOME/bin
#######
case "$1" in
start)
        echo "Starting amoeba"
        /usr/local/amoeba/bin/amoeba start &
        ;;

stop)
        echo "Stop amoeba"
        /usr/local/amoeba/bin/amoeba stop
        ;;
restart)
        echo "Stop amoeba..."
        /usr/local/amoeba/bin/amoeba stop
        echo "Starting amoeba"
        /usr/local/amoeba/bin/amoeba start &
        ;;
esac

ok

chmod +x amoeba #给权限
chkconfig --add amoeba #添加到系统服务




或者将kibana做成服务后用命令service kibana start启动

Kibana 服务

#!/bin/bash
### BEGIN INIT INFO
# Provides:          kibana
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Runs kibana daemon
# Description: Runs the kibana daemon as a non-root user
### END INIT INFO
# Process name
NAME=kibana
DESC="Kibana4"
PROG="/etc/init.d/kibana"
# Configure location of Kibana bin
KIBANA_BIN=/usr/local/kibana/bin
# PID Info
PID_FOLDER=/var/run/kibana/
PID_FILE=/var/run/kibana/$NAME.pid
LOCK_FILE=/var/lock/subsys/$NAME
PATH=/bin:/usr/bin:/sbin:/usr/sbin:$KIBANA_BIN
DAEMON=$KIBANA_BIN/$NAME
# Configure User to run daemon process
DAEMON_USER=root
# Configure logging location
KIBANA_LOG=/var/log/kibana.log
# Begin Script
RETVAL=0
if [ `id -u` -ne 0 ]; then
        echo "You need root privileges to run this script"
        exit 1
fi
# Function library
. /etc/init.d/functions
 
start() {
        echo -n "Starting $DESC : "
pid=`pidofproc -p $PID_FILE kibana`
        if [ -n "$pid" ] ; then
                echo "Already running."
                exit 0
        else
        # Start Daemon
if [ ! -d "$PID_FOLDER" ] ; then
                        mkdir $PID_FOLDER
                fi
daemon --user=$DAEMON_USER --pidfile=$PID_FILE $DAEMON 1>"$KIBANA_LOG" 2>&1 &
                sleep 2
                pidofproc node > $PID_FILE
                RETVAL=$?
                [[ $? -eq 0 ]] && success || failure
echo
                [ $RETVAL = 0 ] && touch $LOCK_FILE
                return $RETVAL
        fi
}
reload()
{
    echo "Reload command is not implemented for this service."
    return $RETVAL
}
stop() {
        echo -n "Stopping $DESC : "
        killproc -p $PID_FILE $DAEMON
        RETVAL=$?
echo
        [ $RETVAL = 0 ] && rm -f $PID_FILE $LOCK_FILE
}
 
case "$1" in
  start)
        start
;;
  stop)
        stop
        ;;
  status)
        status -p $PID_FILE $DAEMON
        RETVAL=$?
        ;;
  restart)
        stop
        start
        ;;
  reload)
reload
;;
  *)
# Invalid Arguments, print the following message.
        echo "Usage: $0 {start|stop|status|restart}" >&2
exit 2
        ;;
esac
chmod +x kibana #给权限
chkconfig --add kibana #添加到系统服务





近期由于做嵌入式项目需要,要求将编写的程序在板载系统开机时自启动。这里做个笔记,备忘。

1 概念

通过查资料发现linux启动服务是用SERVICE +COMMAND。这里的command命令本身也是一个脚本。比如说:service networking start;中networking就是一个shell脚本。注意:这里的脚本没有文件后缀.sh。

接下来,将介绍如何做一个属于自己的service命令。

首先可以了解一下service这个命令的原理。这里我就偷一下懒,你们自己去问度娘或找一下“man”(在终端上man service下)。

接下来我简单说下service的运行过程。以networking为例:service networking start。

首先,sevice 会去/etc/init.d下寻找networking脚本,start是networking脚本里的一个参数(你可以去查看networking这个脚本支持的参数),然后告诉系统运行networking这个脚本,剩下的事情就交给networking脚本去坐了,事实就是这么简单。

至此,你们应该知道如何添加一个service命令了吧。

编写一个脚本,然后把它放在/etc/init.d这个目录下,再用service + 脚本名字 运行即可。如果是要开机自动启动那就得用chkconfig命令了。


注意:

A、service这个命令往往是即时生效,不用开关机,但是重启后服务会回到默认状态。



B、chkconfig是用于把服务加到开机自动启动列表里,只要启动它,就能自动启动,重启后永久生效即:

chkconfig --add COMMAND 

chkconfig COMMAND on/off    重启后永久生效

如果你们的机器不支持chkconfig命令,下面提供另一种开机自动启动系统服务的方法。

首先确保脚本已在/etc/init.d/目录下,然后用:update-rc.d xxx defaults NN命令(NN为启动顺序),将脚本添加到初始化执行的队列中去。

注意如果脚本需要用到网络,则NN需设置一个比较大的数字,如99。

2 实例

接下来我带大家做一下试验,帮助大家更好的理解如何在Linux中利用Service命令添加系统服务

2.1 首先编写demo程序:hello.c



[cpp] view plain copy


1. #include <stdio.h>
2.   
3. main()  
4. {  
5. FILE
6. char a[] = "Hello world!";   
7. "hhh.txt","a+");   
8.     fputs(a,fp);  
9. return
10. }

2.2 编译hello.c

[plain] view plain copy



  1. gcc -g hello.c -o hello  


2.3 在/etc/init.d目录下添加脚本test

[plain] view plain copy


1. #!/bin/bash  
2.   
3. start(){  
4.     echo "------------------test----------------"  
5.     cd /home/xxx   //hello的所在文件夹的绝对路径  
6.     ./hello  
7. }  
8.   
9. case $1 in  
10. start):  
11. start  
12. ;;  
13. stop):  
14. echo "-----------------stop------------------"  
15. ;;  
16. esac  
17.   
18. exit 0



2.4 设置权限



[plain] view plain copy


1. chmod 777 /etc/init.d/test  
 
  
2.5 利用service启动hello
 
  
 
     [plain] 
     view plain 
      copy 
      
 
   
1. service test start  
 
  2.6 设置开机自动启动 
  
 
     [plain] 
     view plain 
      copy 
      
 
   
1. chkconfig --add test   
2. chkconfig test on/off    //重启后永久生效

如果2.6不起作用,按2.7提供的方法执行

2.7 通过update-rc.d 命名设置开机自启动



[plain] view plain copy


1. cd /etc/init.d  
2. sudo update-rc.d test defaults 95

 注:其中数字95是脚本启动的顺序号,按照自己的需要相应修改即可。在你有多个启动脚本,而它们之间又有先后启动的依赖关系时你就知道这个数字的具体作用了。该命令的输出信息参考如下:

[plain] view plain copy


1. update-rc.d: warning: /etc/init.d/test missing LSB information  
2. update-rc.d: see <http://wiki.debian.org/LSBInitScripts>  
3. Adding system startup for /etc/init.d/test ...  
4. /etc/rc0.d/K95test -> ../init.d/test  
5. /etc/rc1.d/K95test -> ../init.d/test  
6. /etc/rc6.d/K95test -> ../init.d/test  
7. /etc/rc2.d/S95test -> ../init.d/test  
8. /etc/rc3.d/S95test -> ../init.d/test  
9. /etc/rc4.d/S95test -> ../init.d/test  
10. /etc/rc5.d/S95test -> ../init.d/test

卸载启动脚本的方法:

[plain] view plain copy


1. cd /etc/init.d  
2. sudo update-rc.d -f test remove


命令输出的信息参考如下:

[plain] view plain copy

1. Removing any system startup links for /etc/init.d/test ...  
2. /etc/rc0.d/K95test  
3. /etc/rc1.d/K95test  
4. /etc/rc2.d/S95test  
5. /etc/rc3.d/S95test  
6. /etc/rc4.d/S95test  
7. /etc/rc5.d/S95test  
8. /etc/rc6.d/K95test


3 Ubuntu设置开机启动脚本的方法



如果2.7提供的方法不行,这里提供另一种方法。



/etc/rc.local脚本



rc.local脚本是一个ubuntu开机后会自动执行的脚本,我们可以在该脚本内添加命令行指令。该脚本位于/etc/路径下,需要root权限才能修改。



该脚本具体格式如下:



[plain] view plain copy


1. #!/bin/sh -e  
2. #  
3. # rc.local  
4. #  
5. # This script is executed at the end of each multiuser runlevel.  
6. # Make sure that the script will "exit 0" on success or any other  
7. # value on error.  
8. #  
9. # In order to enable or disable this script just change the execution  
10. # bits.  
11. #  
12. # By default this script does nothing.  
13.     
14. exit 0


注意: 



一定要将命令添加在 exit 0之前,即在第13行添加;



命令即为脚本文件所在的绝对路径,如在第13行添加 /usr/local/bin/test;



如果脚本中有死循环,需要在该脚本路径后加上 & ,让其后台执行;



一定要给脚本文件赋可执行权限。



[plain] view plain copy



  1. sudo chmod 777 test   



或者在 /etc/init.d/rc.local 中在最后加上脚本的绝对路径也可以开机自启动。






另外还有一点要说的就是,脚本test在后台执行后,我尝试使用



[plain] view plain copy


1. ps -ef | grep test | grep -v grep


去查看脚本test的进程以确定它是否被执行,但是查不到。后来一想,可能是shell执行了该脚本,所以无法查到该脚本的进程。



在linux系统中,安装完一个软件或应用后,有时候需要手动启动该应用,也需要收到将该应用添加到开机启动项中,让其可以能够在linux一开机后就加载该应用

启动应用的方法

CentOS 6 :

service SERVICE start|stop|restart|reload|status

CentOS 7 :

systemctl start|stop|restart|reload|status SERVICE

添加到开机启动项的方法

CentOS 6 :

chkconfig SERVICE on|off

CentOS 7 :

systemctl enable|disable SERVICE