通过/etc/init.d下的脚本实现开机自起
先编辑文件/etc/init.d/自己文件名
#!/bin/bash ### 中括号表示替换(if除外,连中括号一起替换) 括号表示说明 删除本行和说明后使用 #chkconfig: [这是启动级别,推荐2345] 50 50 #description: [进程描述说明文字] ### BEGIN INIT INFO # Provides: [提供商] # Required-Start: $all # Required-Stop: $all # Default-Start: 2 3 4 5(启动级别加空格) # Default-Stop: 0 1 6(停止级别加空格) # Short-Description: [短版进程描述说明文字] # Description: [完整进程描述说明文字] ### END INIT INFO server_start(){ isStart=`ps aux|grep '[进程名称中包含的字符]'|grep -v grep|awk '{print $2}'` if [ "$isStart" == '' ];then echo "Starting [启动中输出文字 这里推荐写描述说明名]..." nohup [进程完整路径] [进程参数] >/dev/null 2>&1 & sleep 0.2 isStart=`ps aux|grep '[进程名称中包含的字符]'|grep -v grep|awk '{print $2}'` if [ "$isStart" == '' ];then echo -e "Error: Failed To Start [启动失败输出文字]." return; fi echo "[启动成功 这里推荐写描述说明名] (pid $isStart) Started." else echo "Error: [已经启动过输出文字 这里推荐写描述说明名] (pid $isStart) Already Running." fi } server_stop() { echo -e "Stopping [停止中输出文字 这里推荐写描述说明名]..."; pid=`ps aux|grep '[进程名称中包含的字符]'|grep -v grep|awk '{print $2}'` if [ "$pid" == '' ];then echo "[已经停止过输出文字 这里推荐写描述说明名] Not Running." else echo "Killing [停止中输出文字 这里推荐写描述说明名] (pid $pid)..." kill -9 $pid echo "[停止成功输出文字 这里推荐写描述说明名] Stopped." fi } server_status() { isStart=`ps aux|grep '[进程名称中包含的字符]'|grep -v grep|awk '{print $2}'` if [ "$isStart" != '' ];then echo "[状态为运行中输出文字 这里推荐写描述说明名] (pid $isStart) Already Running." else echo "[状态为没有运行输出文字 这里推荐写描述说明名] Not Running." fi } case "$1" in 'start') server_start ;; 'stop') server_stop ;; 'restart') server_stop sleep 0.2 server_start ;; 'status') server_status ;; *) echo "Usage: /etc/init.d/[本文件名] {start|stop|restart}" ;; esac
然后执行
chkconfig --list
如果该文件未在此命令的输出中
则执行
chkconfig --add 自己文件名 #如: chkconfig --add DesmgService
如果该文件已在此命令的输出中
则执行
chkconfig 自己文件名 --level 2345 on #如: chkconfig DesmgService --level 2345 on
进行启用
最后使用
service 文件名 start/stop/status/restart #如: service DesmgService start service DesmgService stop service DesmgService status service DesmgService restart
进行启动/停止/启停状态查询/重启