安装了一个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
- #include <stdio.h>
- main()
- {
- FILE *fp;
- char a[] = "Hello world!";
- fp=fopen("hhh.txt","a+");
- fputs(a,fp);
- return 0;
- }
- gcc -g hello.c -o hello
- #!/bin/bash
- start(){
- echo "------------------test----------------"
- cd /home/xxx //hello的所在文件夹的绝对路径
- ./hello
- }
- case $1 in
- start):
- start
- ;;
- stop):
- echo "-----------------stop------------------"
- ;;
- esac
- exit 0
2.4 设置权限
- chmod 777 /etc/init.d/test
2.5 利用service启动hello
- service test start
- chkconfig --add test
- chkconfig test on/off //重启后永久生效
如果2.6不起作用,按2.7提供的方法执行
2.7 通过update-rc.d 命名设置开机自启动
- cd /etc/init.d
- sudo update-rc.d test defaults 95
- update-rc.d: warning: /etc/init.d/test missing LSB information
- update-rc.d: see <http://wiki.debian.org/LSBInitScripts>
- Adding system startup for /etc/init.d/test ...
- /etc/rc0.d/K95test -> ../init.d/test
- /etc/rc1.d/K95test -> ../init.d/test
- /etc/rc6.d/K95test -> ../init.d/test
- /etc/rc2.d/S95test -> ../init.d/test
- /etc/rc3.d/S95test -> ../init.d/test
- /etc/rc4.d/S95test -> ../init.d/test
- /etc/rc5.d/S95test -> ../init.d/test
- cd /etc/init.d
- sudo update-rc.d -f test remove
- Removing any system startup links for /etc/init.d/test ...
- /etc/rc0.d/K95test
- /etc/rc1.d/K95test
- /etc/rc2.d/S95test
- /etc/rc3.d/S95test
- /etc/rc4.d/S95test
- /etc/rc5.d/S95test
- /etc/rc6.d/K95test
3 Ubuntu设置开机启动脚本的方法
- #!/bin/sh -e
- #
- # rc.local
- #
- # This script is executed at the end of each multiuser runlevel.
- # Make sure that the script will "exit 0" on success or any other
- # value on error.
- #
- # In order to enable or disable this script just change the execution
- # bits.
- #
- # By default this script does nothing.
- exit 0
- sudo chmod 777 test
- ps -ef | grep test | grep -v grep
在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
以上所述是小编给大家介绍的Centos 6和Centos 7下服务启动方法及添加到开机启动项的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
出处:https://blog.csdn.net/u013554213/article/details/78792686