項目中配置一些開機自啟的軟件,方便我們后期的維護。
腳本一般我這里名稱起的就是 redis nginx rabbitmq 不加后綴了。
我們的這個腳本文件放到 /etc/init.d/ 下就可以了,
這里上傳到這里后可能還是一個普通文件,我們需要給他們添加執行權限,讓他們可以被執行
chmod +x redis
chmod +x nginx
chmod +x rabbitmq
好了權限賦予完成,我們就可以執行命令了。
接下來我們就是添加服務和啟動了
chkconfig --add redis
chkconfig --add nginx
chkconfig --add rabbitmq
服務已經添加了我們可以來看一下
chkconfig --list
這里有的已經開啟了,所以顯示的是開。123456全部關,就是沒有開啟了。
開啟服務命令
chkconfig redis on
好了,然后啟動redis,之后看看是不是啟動了
ps -ef|grep redis 查看redis進程
然后我們重啟虛擬機,看看啟動后是不是可以啟動。
# 1.將(腳本)啟動文件移動到 /etc/init.d/或者/etc/rc.d/init.d/目錄下。(前者是后者的軟連接) mv test.sh /etc/rc.d/init.d # 2.啟動文件前面務必添加如下三行代碼,否側會提示chkconfig不支持。 #!/bin/sh 告訴系統使用的shell,所以的shell腳本都是這樣 #chkconfig: 35 20 80 分別代表運行級別,啟動優先權,關閉優先權,此行代碼必須 #description: http server 自己隨便發揮!!!,此行代碼必須 /bin/echo $(/bin/date +%F_%T) >> /tmp/test.log # 3.增加腳本的可執行權限 chmod +x /etc/rc.d/init.d/test.sh # 4.添加腳本到開機自動啟動項目中。添加到chkconfig,開機自啟動。 [root@localhost ~]# cd /etc/rc.d/init.d [root@localhost ~]# chkconfig --add test.sh [root@localhost ~]# chkconfig test.sh on # 5.關閉開機啟動 [root@localhost ~]# chkconfig test.sh off # 6.從chkconfig管理中刪除test.sh [root@localhost ~]# chkconfig --del test.sh # 7.查看chkconfig管理 [root@localhost ~]# chkconfig --list test.sh
1.redis
#!/bin/sh # chkconfig: 2345 56 26 # description: Redis Service ### BEGIN INIT INFO # Provides: Redis # Required-Start: $all # Required-Stop: $all # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: starts Redis # Description: starts the BT-Web ### END INIT INFO # Simple Redis init.d script conceived to work on Linux systems # as it does use of the /proc filesystem. CONF="/usr/local/redis/redis-5.0.5/redis.conf" REDIS_PORT=$(cat $CONF |grep port|grep -v '#'|awk '{print $2}') REDIS_PASS=$(cat $CONF |grep requirepass|grep -v '#'|awk '{print $2}') REDIS_HOST=$(cat $CONF |grep bind|grep -v '#'|awk '{print $2}') if [ "$REDIS_PASS" != "" ];then REDIS_PASS=" -a $REDIS_PASS" fi #if [ -f "/www/server/redis/start.pl" ];then # STAR_PORT=$(cat /www/server/redis/start.pl) #else STAR_PORT="6379" #fi EXEC=/usr/local/redis/redis-5.0.5/src/redis-server CLIEXEC="/usr/local/redis/redis-5.0.5/src/redis-cli -h ${REDIS_HOST} -p ${STAR_PORT}${REDIS_PASS}" PIDFILE=$(cat $CONF |grep pidfile|grep -v '#'|awk '{print $2}') # pidfile /var/run/redis_6379.pid redis_start(){ if [ -f ${PIDFILE} ]; then ps -p $(cat ${PIDFILE}) > /dev/null 2>&1 if [ $? -ne "0" ]; then echo Redis is not running, buy pid file is exits ${PIDFILE} exit 1 else echo "redis is running! ($(cat ${PIDFILE}))" exit 0 fi fi echo "Starting redis server..." #sudo -u redis $EXEC $CONF sudo $EXEC $CONF #echo ${REDIS_PORT} > /www/server/redis/start.pl echo "Starting redis success!" } redis_status(){ if [ -f ${PIDFILE} ]; then ps -p $(cat ${PIDFILE}) > /dev/null 2>&1 if [ $? -ne "0" ]; then echo "Redis is not running, buy pid file is exits ${PIDFILE}" exit 1 else echo "redis is running! ($(cat ${PIDFILE}))" exit 0 fi else echo "redis is stopped" exit 0 fi } redis_stop(){ echo "Stopping ..." $CLIEXEC shutdown sleep 1 pkill -9 redis-server rm -f ${PIDFILE} echo "Redis stopped" } case "$1" in start) redis_start ;; stop) redis_stop ;; status) redis_status ;; restart|reload) redis_stop sleep 0.3 redis_start ;; *) echo "Please use start or stop as first argument" ;; esac
粗體三條路徑需要自己配置:
1,配置文件路徑
2,服務器啟動文件路徑
3,客戶端啟動程序的路徑
這個配置文件是重寶塔安裝的文件中拷貝出來的,改了該,注釋的部分就是改動的,所以很好的利用上了。
2,nginx
#!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: NGINX is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # config: /etc/sysconfig/nginx # pidfile: /var/run/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/local/nginx/sbin/nginx" #nginx啟動文件位置 prog=$(basename $nginx) NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" #nginx配置文件位置 [ -f /usr/local/nginx/sbin/nginx ] && /usr/local/nginx/sbin/nginx lockfile=/var/lock/subsys/nginx make_dirs() { # make required directories user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` if [ -n "$user" ]; then if [ -z "`grep $user /etc/passwd`" ]; then useradd -M -s /bin/nologin $user fi options=`$nginx -V 2>&1 | grep 'configure arguments:'` for opt in $options; do if [ `echo $opt | grep '.*-temp-path'` ]; then value=`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then # echo "creating" $value mkdir -p $value && chown -R $user $value fi fi done fi } start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop sleep 1 start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $prog -HUP retval=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac
粗體文字的四個地方:
1,啟動文件位置
2,配置文件位置
3,rabbitmq
這里的腳本只可以使用啟動停止,但是自啟動,我試過還是不行,不知道哪里設置錯誤。網上好多的都是這個問題,可以手動使用,但是自啟動就是不行,使用rpm安裝后的也就是自動生成的也是這個文件,哪個拿過來
就不可以開機自啟動,不知道為什么。哪位懂得可以告訴一下。上邊兩個我都試過了,可以使用的。
#!/bin/sh # # rabbitmq-server RabbitMQ broker # # chkconfig: - 80 05 # description: Enable AMQP service provided by RabbitMQ # ### BEGIN INIT INFO # Provides: rabbitmq-server # Required-Start: $remote_fs $network # Required-Stop: $remote_fs $network # Description: RabbitMQ broker # Short-Description: Enable AMQP service provided by RabbitMQ broker ### END INIT INFO # Source function library. . /etc/init.d/functions export HOME=/root PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/erlang/bin NAME=rabbitmq-server DAEMON=/usr/sbin/${NAME} CONTROL=/usr/sbin/rabbitmqctl DESC=rabbitmq-server USER=root ROTATE_SUFFIX= INIT_LOG_DIR=/var/log/rabbitmq PID_FILE=/var/run/rabbitmq/pid START_PROG="daemon" LOCK_FILE=/var/lock/subsys/$NAME test -x $DAEMON || exit 0 test -x $CONTROL || exit 0 RETVAL=0 set -e [ -f /etc/default/${NAME} ] && . /etc/default/${NAME} ensure_pid_dir () { PID_DIR=`dirname ${PID_FILE}` if [ ! -d ${PID_DIR} ] ; then mkdir -p ${PID_DIR} chown -R ${USER}:${USER} ${PID_DIR} chmod 755 ${PID_DIR} fi } remove_pid () { rm -f ${PID_FILE} rmdir `dirname ${PID_FILE}` || : } start_rabbitmq () { status_rabbitmq quiet if [ $RETVAL = 0 ] ; then echo RabbitMQ is currently running else RETVAL=0 ensure_pid_dir set +e RABBITMQ_PID_FILE=$PID_FILE $START_PROG $DAEMON \ > "${INIT_LOG_DIR}/startup_log" \ 2> "${INIT_LOG_DIR}/startup_err" \ 0<&- & $CONTROL wait $PID_FILE >/dev/null 2>&1 RETVAL=$? set -e case "$RETVAL" in 0) echo SUCCESS if [ -n "$LOCK_FILE" ] ; then touch $LOCK_FILE fi ;; *) remove_pid echo FAILED - check ${INIT_LOG_DIR}/startup_\{log, _err\} RETVAL=1 ;; esac fi } stop_rabbitmq () { status_rabbitmq quiet if [ $RETVAL = 0 ] ; then set +e $CONTROL stop ${PID_FILE} > ${INIT_LOG_DIR}/shutdown_log 2> ${INIT_LOG_DIR}/shutdown_err RETVAL=$? set -e if [ $RETVAL = 0 ] ; then remove_pid if [ -n "$LOCK_FILE" ] ; then rm -f $LOCK_FILE fi else echo FAILED - check ${INIT_LOG_DIR}/shutdown_log, _err fi else echo RabbitMQ is not running RETVAL=0 fi } status_rabbitmq() { set +e if [ "$1" != "quiet" ] ; then $CONTROL status 2>&1 else $CONTROL status > /dev/null 2>&1 fi if [ $? != 0 ] ; then RETVAL=3 fi set -e } rotate_logs_rabbitmq() { set +e $CONTROL rotate_logs ${ROTATE_SUFFIX} if [ $? != 0 ] ; then RETVAL=1 fi set -e } restart_running_rabbitmq () { status_rabbitmq quiet if [ $RETVAL = 0 ] ; then restart_rabbitmq else echo RabbitMQ is not runnning RETVAL=0 fi } restart_rabbitmq() { stop_rabbitmq start_rabbitmq } case "$1" in start) echo -n "Starting $DESC: " start_rabbitmq echo "$NAME." ;; stop) echo -n "Stopping $DESC: " stop_rabbitmq echo "$NAME." ;; status) status_rabbitmq ;; rotate-logs) echo -n "Rotating log files for $DESC: " rotate_logs_rabbitmq ;; force-reload|reload|restart) echo -n "Restarting $DESC: " restart_rabbitmq echo "$NAME." ;; try-restart) echo -n "Restarting $DESC: " restart_running_rabbitmq echo "$NAME." ;; *) echo "Usage: $0 {start|stop|status|rotate-logs|restart|condrestart|try-restart|reload|force-reload}" >&2 RETVAL=1 ;; esac exit $RETVAL
只需要改動粗體部分就可以。然后使用命令 賦權, 執行就可以
2,以服務的方式【 https://blog.csdn.net/liangpingguo/article/details/106098907?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-9.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-9.nonecase 】 添加服務nginx.server 文件保存在目錄下:以754的權限。目錄路徑:/usr/lib/systemd/system。如上面的supervisord.service文件放在這個目錄下面。 [root@localhost ~]# cat /usr/lib/systemd/system/nginx.service [root@localhost ~]# cat /usr/lib/systemd/system/supervisord.service 設置開機自啟動 [root@localhost ~]# systemctl enable nginx.service [root@localhost ~]# systemctl enable supervisord 停止開機自啟動 [root@localhost ~]# systemctl disable nginx.service [root@localhost ~]# systemctl disable supervisord 驗證一下是否為開機啟動 [root@localhost ~]# systemctl is-enabled nginx [root@localhost ~]# systemctl is-enabled supervisord # nginx.service服務文件 [Unit] Description=nginx - high performance web server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop [Install] WantedBy=multi-user.target