Linux下建立服務

來源:互聯網
上載者:User

標籤:time   stop   div   load   proc   功能   line   desc   linux   

1 首先在/etc/rc.d/init.d/下添加指令碼 asr_cron

#!/bin/bash# $Id: rc.redhat.asterisk 67061 2007-06-04 17:11:43Z tilghman $## asterisk    Starts, Stops and Reloads Asterisk.## chkconfig: 345 95 65# description: Asterisk PBX and telephony daemon.AST_SBIN=/var/www/html/test.php. /etc/rc.d/init.d/functionsif ! [ -x $AST_SBIN ] ; thenecho "ERROR: test not found"exit 0fiDAEMON=$AST_SBINstart() {# Start daemons.    echo -n $"Starting test: "        $AST_SBIN >> /var/www/html/test.log &        sleep 1        proc=$(ps -fe | grep $AST_SBIN|grep -v grep|awk ‘{print $2}‘)        if [ -z $proc ]            then                echo_failure        else            echo_success                fi                RETVAL=$?                echo                return $RETVAL}stop() {# Stop daemons.    RETVAL=1        echo -n $"Shutting down test: "        ps -fe | grep $AST_SBIN|grep -v grep|awk ‘{print $2}‘|while read line        do            kill $line                RETVAL=$?                echo -n "kill $line "                done                sleep 1                proc=$(ps -fe | grep $AST_SBIN|grep -v grep|awk ‘{print $2}‘)                if [ -z $proc ]                    then                        echo_success                else                    echo_failure                        fi                        echo                        return $RETVAL}restart() {    stop        start}status() {    proc=$(ps -fe | grep $AST_SBIN|grep -v grep|awk ‘{print $2}‘)        if [ -z $proc ]            then                echo "teset: test is stoped"        else            echo "test: test is starting"                fi                RETVAL=$?                return RETVAL}# See how we were called.case "$1" instart)start;;stop)stop;;restart)restart;;status)status;;*)echo "Usage: service test {start|stop|restart|status}"exit 1esacexit $?

這個指令碼執行一個PHP指令碼,功能有啟動、停止、重啟等功能。

test.php

 

#!/usr/bin/php -q<?phpset_time_limit(0);$file = ‘test.txt‘;while(true){@file_put_contents($file,date(‘Y-m-d H:i:s‘,time())."\n",FILE_APPEND);sleep(10);}

 

 

 

 

2 把上面的指令碼加到服務裡:

chkconfig --add asr_cron

3 需要把asr_cron指令碼、以及PHP指令碼賦予可執行許可權

然後就可以如下的命令控制服務的啟動、停止、重啟了:

/etc/rc.d/init.d/asr_cron start

/etc/rc.d/init.d/asr_cron stop

/etc/rc.d/init.d/asr_cron restart

Linux下建立服務

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.