Running a php file from init











up vote
0
down vote

favorite












I am trying to run a php script from init.d in centos 6.9, when I exit the terminal from which I started the service the application stops, and always the status of the application shows as stopped, even though it is running.



    #!/bin/sh
#
# arrbot This shell script takes care of starting and stopping
# the arbbot
#
# chkconfig: - 65 20
# description: arbbot trader.
# processname: arbbot
# Required-Start: $network $syslog $mysqld
# Required-Stop: $network $syslog $mysqld
# Short-Description: start and stop arbbot server
# Description: arbbot trader
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

NAME=arbbot
DESC="arbbot trader"
LOGFILE="/var/log/${NAME}.log"
DAEMON="/usr/bin/php5"
WORK_DIRECTORY="/var/www/arbbot/"
DAEMON_OPTS="/var/www/arbbot/main.php"
ARBDIR="/var/www/arbbot"
ARBBOT="exec ${DAEMON} ${DAEMON_OPTS} > ${LOGFILE} 2>&1 > NULL"
LOCK="/var/lock/subsys/arbbot"

start() {
echo -n "Starting ${DESC}: "
cd "${WORK_DIRECTORY}"
$ARBBOT
RETVAL=$?
[ $RETVAL -eq 0 ] && touch -- "$LOCK"
return $RETVAL
}

stop() {
echo -n "Shutting down arbbot: "
killproc $ARBBOT
rm -f $LOCK
return
}

restart() {
stop
start
}

reload() {
restart
}

case "$1" in
start)
start
echo "$NAME."
;;
stop)
stop
echo "$NAME."
;;
status)
status ARBBOT
RETVAL=$?
;;
restart)
restart
;;
reload)
reload
;;
*)
echo "Usage: {start|stop|status|reload|restart[|probe]"
exit 1
;;
esac

exit $RETVAL









share|improve this question
























  • It starts a application and keeps on running, like a server. "does it take an infinitely long time to run? " Yes it keeps on running printing messages to the console.
    – Som
    Feb 13 at 22:17










  • yes, it does run like that. But the result is not different.
    – Som
    Feb 13 at 22:24












  • again no changes. Thanks for your help,
    – Som
    Feb 13 at 22:47










  • Try to use 'nohup' as I suggest. It's protect your script from stopping signals after hangup happened (logout from terminal).
    – Yurij Goncharuk
    Feb 13 at 23:08










  • Yes thanks , I am trying it.
    – Som
    Feb 13 at 23:09















up vote
0
down vote

favorite












I am trying to run a php script from init.d in centos 6.9, when I exit the terminal from which I started the service the application stops, and always the status of the application shows as stopped, even though it is running.



    #!/bin/sh
#
# arrbot This shell script takes care of starting and stopping
# the arbbot
#
# chkconfig: - 65 20
# description: arbbot trader.
# processname: arbbot
# Required-Start: $network $syslog $mysqld
# Required-Stop: $network $syslog $mysqld
# Short-Description: start and stop arbbot server
# Description: arbbot trader
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

NAME=arbbot
DESC="arbbot trader"
LOGFILE="/var/log/${NAME}.log"
DAEMON="/usr/bin/php5"
WORK_DIRECTORY="/var/www/arbbot/"
DAEMON_OPTS="/var/www/arbbot/main.php"
ARBDIR="/var/www/arbbot"
ARBBOT="exec ${DAEMON} ${DAEMON_OPTS} > ${LOGFILE} 2>&1 > NULL"
LOCK="/var/lock/subsys/arbbot"

start() {
echo -n "Starting ${DESC}: "
cd "${WORK_DIRECTORY}"
$ARBBOT
RETVAL=$?
[ $RETVAL -eq 0 ] && touch -- "$LOCK"
return $RETVAL
}

stop() {
echo -n "Shutting down arbbot: "
killproc $ARBBOT
rm -f $LOCK
return
}

restart() {
stop
start
}

reload() {
restart
}

case "$1" in
start)
start
echo "$NAME."
;;
stop)
stop
echo "$NAME."
;;
status)
status ARBBOT
RETVAL=$?
;;
restart)
restart
;;
reload)
reload
;;
*)
echo "Usage: {start|stop|status|reload|restart[|probe]"
exit 1
;;
esac

exit $RETVAL









share|improve this question
























  • It starts a application and keeps on running, like a server. "does it take an infinitely long time to run? " Yes it keeps on running printing messages to the console.
    – Som
    Feb 13 at 22:17










  • yes, it does run like that. But the result is not different.
    – Som
    Feb 13 at 22:24












  • again no changes. Thanks for your help,
    – Som
    Feb 13 at 22:47










  • Try to use 'nohup' as I suggest. It's protect your script from stopping signals after hangup happened (logout from terminal).
    – Yurij Goncharuk
    Feb 13 at 23:08










  • Yes thanks , I am trying it.
    – Som
    Feb 13 at 23:09













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to run a php script from init.d in centos 6.9, when I exit the terminal from which I started the service the application stops, and always the status of the application shows as stopped, even though it is running.



    #!/bin/sh
#
# arrbot This shell script takes care of starting and stopping
# the arbbot
#
# chkconfig: - 65 20
# description: arbbot trader.
# processname: arbbot
# Required-Start: $network $syslog $mysqld
# Required-Stop: $network $syslog $mysqld
# Short-Description: start and stop arbbot server
# Description: arbbot trader
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

NAME=arbbot
DESC="arbbot trader"
LOGFILE="/var/log/${NAME}.log"
DAEMON="/usr/bin/php5"
WORK_DIRECTORY="/var/www/arbbot/"
DAEMON_OPTS="/var/www/arbbot/main.php"
ARBDIR="/var/www/arbbot"
ARBBOT="exec ${DAEMON} ${DAEMON_OPTS} > ${LOGFILE} 2>&1 > NULL"
LOCK="/var/lock/subsys/arbbot"

start() {
echo -n "Starting ${DESC}: "
cd "${WORK_DIRECTORY}"
$ARBBOT
RETVAL=$?
[ $RETVAL -eq 0 ] && touch -- "$LOCK"
return $RETVAL
}

stop() {
echo -n "Shutting down arbbot: "
killproc $ARBBOT
rm -f $LOCK
return
}

restart() {
stop
start
}

reload() {
restart
}

case "$1" in
start)
start
echo "$NAME."
;;
stop)
stop
echo "$NAME."
;;
status)
status ARBBOT
RETVAL=$?
;;
restart)
restart
;;
reload)
reload
;;
*)
echo "Usage: {start|stop|status|reload|restart[|probe]"
exit 1
;;
esac

exit $RETVAL









share|improve this question















I am trying to run a php script from init.d in centos 6.9, when I exit the terminal from which I started the service the application stops, and always the status of the application shows as stopped, even though it is running.



    #!/bin/sh
#
# arrbot This shell script takes care of starting and stopping
# the arbbot
#
# chkconfig: - 65 20
# description: arbbot trader.
# processname: arbbot
# Required-Start: $network $syslog $mysqld
# Required-Stop: $network $syslog $mysqld
# Short-Description: start and stop arbbot server
# Description: arbbot trader
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

NAME=arbbot
DESC="arbbot trader"
LOGFILE="/var/log/${NAME}.log"
DAEMON="/usr/bin/php5"
WORK_DIRECTORY="/var/www/arbbot/"
DAEMON_OPTS="/var/www/arbbot/main.php"
ARBDIR="/var/www/arbbot"
ARBBOT="exec ${DAEMON} ${DAEMON_OPTS} > ${LOGFILE} 2>&1 > NULL"
LOCK="/var/lock/subsys/arbbot"

start() {
echo -n "Starting ${DESC}: "
cd "${WORK_DIRECTORY}"
$ARBBOT
RETVAL=$?
[ $RETVAL -eq 0 ] && touch -- "$LOCK"
return $RETVAL
}

stop() {
echo -n "Shutting down arbbot: "
killproc $ARBBOT
rm -f $LOCK
return
}

restart() {
stop
start
}

reload() {
restart
}

case "$1" in
start)
start
echo "$NAME."
;;
stop)
stop
echo "$NAME."
;;
status)
status ARBBOT
RETVAL=$?
;;
restart)
restart
;;
reload)
reload
;;
*)
echo "Usage: {start|stop|status|reload|restart[|probe]"
exit 1
;;
esac

exit $RETVAL






linux php sysvinit php5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 at 14:58









Rui F Ribeiro

38.3k1475126




38.3k1475126










asked Feb 13 at 21:49









Som

83




83












  • It starts a application and keeps on running, like a server. "does it take an infinitely long time to run? " Yes it keeps on running printing messages to the console.
    – Som
    Feb 13 at 22:17










  • yes, it does run like that. But the result is not different.
    – Som
    Feb 13 at 22:24












  • again no changes. Thanks for your help,
    – Som
    Feb 13 at 22:47










  • Try to use 'nohup' as I suggest. It's protect your script from stopping signals after hangup happened (logout from terminal).
    – Yurij Goncharuk
    Feb 13 at 23:08










  • Yes thanks , I am trying it.
    – Som
    Feb 13 at 23:09


















  • It starts a application and keeps on running, like a server. "does it take an infinitely long time to run? " Yes it keeps on running printing messages to the console.
    – Som
    Feb 13 at 22:17










  • yes, it does run like that. But the result is not different.
    – Som
    Feb 13 at 22:24












  • again no changes. Thanks for your help,
    – Som
    Feb 13 at 22:47










  • Try to use 'nohup' as I suggest. It's protect your script from stopping signals after hangup happened (logout from terminal).
    – Yurij Goncharuk
    Feb 13 at 23:08










  • Yes thanks , I am trying it.
    – Som
    Feb 13 at 23:09
















It starts a application and keeps on running, like a server. "does it take an infinitely long time to run? " Yes it keeps on running printing messages to the console.
– Som
Feb 13 at 22:17




It starts a application and keeps on running, like a server. "does it take an infinitely long time to run? " Yes it keeps on running printing messages to the console.
– Som
Feb 13 at 22:17












yes, it does run like that. But the result is not different.
– Som
Feb 13 at 22:24






yes, it does run like that. But the result is not different.
– Som
Feb 13 at 22:24














again no changes. Thanks for your help,
– Som
Feb 13 at 22:47




again no changes. Thanks for your help,
– Som
Feb 13 at 22:47












Try to use 'nohup' as I suggest. It's protect your script from stopping signals after hangup happened (logout from terminal).
– Yurij Goncharuk
Feb 13 at 23:08




Try to use 'nohup' as I suggest. It's protect your script from stopping signals after hangup happened (logout from terminal).
– Yurij Goncharuk
Feb 13 at 23:08












Yes thanks , I am trying it.
– Som
Feb 13 at 23:09




Yes thanks , I am trying it.
– Som
Feb 13 at 23:09










2 Answers
2






active

oldest

votes

















up vote
-1
down vote



accepted










I suggest you to read Run php script as daemon process
.



Also, more detailed description about writing php daemon like original system daemons - How to write a PHP Daemon.






share|improve this answer




























    up vote
    0
    down vote













    There are a lot of features of daemon which do not apply to other process. It needs to close its stdout and stdin, and it needs to dissociate from the controlling process, and its needs to have its cwd set to root (amongst other things). There are minimal daemons which can act as a controlling process for running normal processes as if they were daemons - e.g. daemonize



    You also may need custom signal handling and (if you want any dianostic/audit capability) logging capability.



    However with the POSIX extensions, all the required functions are exposed - so you can setsid() and fork() from within your own code - or even just download some pre-cooked php code to do all this for you.



    BEWARE: there are a lot of people publishing code and advice on the internet which is wrong - but they found it worked for them - the code I've linked above is one of the better implementations.






    share|improve this answer





















    • … including advice like this answer about what dæmons need to do, which was first noted as wrong advice by IBM in 1995. Ali Erdinç Köroglu that you hyperlink to has a mention in the systemd House of Horror. The idea that one can "daemonize" from an interactive login session to a context outwith it grew to be a fallacy after the 1980s.
      – JdeBP
      Feb 14 at 8:19












    • If the right answer is systemd then we're all in trouble.
      – symcbean
      Feb 14 at 13:23











    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "106"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f423991%2frunning-a-php-file-from-init%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    -1
    down vote



    accepted










    I suggest you to read Run php script as daemon process
    .



    Also, more detailed description about writing php daemon like original system daemons - How to write a PHP Daemon.






    share|improve this answer

























      up vote
      -1
      down vote



      accepted










      I suggest you to read Run php script as daemon process
      .



      Also, more detailed description about writing php daemon like original system daemons - How to write a PHP Daemon.






      share|improve this answer























        up vote
        -1
        down vote



        accepted







        up vote
        -1
        down vote



        accepted






        I suggest you to read Run php script as daemon process
        .



        Also, more detailed description about writing php daemon like original system daemons - How to write a PHP Daemon.






        share|improve this answer












        I suggest you to read Run php script as daemon process
        .



        Also, more detailed description about writing php daemon like original system daemons - How to write a PHP Daemon.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 13 at 22:33









        Yurij Goncharuk

        2,3132521




        2,3132521
























            up vote
            0
            down vote













            There are a lot of features of daemon which do not apply to other process. It needs to close its stdout and stdin, and it needs to dissociate from the controlling process, and its needs to have its cwd set to root (amongst other things). There are minimal daemons which can act as a controlling process for running normal processes as if they were daemons - e.g. daemonize



            You also may need custom signal handling and (if you want any dianostic/audit capability) logging capability.



            However with the POSIX extensions, all the required functions are exposed - so you can setsid() and fork() from within your own code - or even just download some pre-cooked php code to do all this for you.



            BEWARE: there are a lot of people publishing code and advice on the internet which is wrong - but they found it worked for them - the code I've linked above is one of the better implementations.






            share|improve this answer





















            • … including advice like this answer about what dæmons need to do, which was first noted as wrong advice by IBM in 1995. Ali Erdinç Köroglu that you hyperlink to has a mention in the systemd House of Horror. The idea that one can "daemonize" from an interactive login session to a context outwith it grew to be a fallacy after the 1980s.
              – JdeBP
              Feb 14 at 8:19












            • If the right answer is systemd then we're all in trouble.
              – symcbean
              Feb 14 at 13:23















            up vote
            0
            down vote













            There are a lot of features of daemon which do not apply to other process. It needs to close its stdout and stdin, and it needs to dissociate from the controlling process, and its needs to have its cwd set to root (amongst other things). There are minimal daemons which can act as a controlling process for running normal processes as if they were daemons - e.g. daemonize



            You also may need custom signal handling and (if you want any dianostic/audit capability) logging capability.



            However with the POSIX extensions, all the required functions are exposed - so you can setsid() and fork() from within your own code - or even just download some pre-cooked php code to do all this for you.



            BEWARE: there are a lot of people publishing code and advice on the internet which is wrong - but they found it worked for them - the code I've linked above is one of the better implementations.






            share|improve this answer





















            • … including advice like this answer about what dæmons need to do, which was first noted as wrong advice by IBM in 1995. Ali Erdinç Köroglu that you hyperlink to has a mention in the systemd House of Horror. The idea that one can "daemonize" from an interactive login session to a context outwith it grew to be a fallacy after the 1980s.
              – JdeBP
              Feb 14 at 8:19












            • If the right answer is systemd then we're all in trouble.
              – symcbean
              Feb 14 at 13:23













            up vote
            0
            down vote










            up vote
            0
            down vote









            There are a lot of features of daemon which do not apply to other process. It needs to close its stdout and stdin, and it needs to dissociate from the controlling process, and its needs to have its cwd set to root (amongst other things). There are minimal daemons which can act as a controlling process for running normal processes as if they were daemons - e.g. daemonize



            You also may need custom signal handling and (if you want any dianostic/audit capability) logging capability.



            However with the POSIX extensions, all the required functions are exposed - so you can setsid() and fork() from within your own code - or even just download some pre-cooked php code to do all this for you.



            BEWARE: there are a lot of people publishing code and advice on the internet which is wrong - but they found it worked for them - the code I've linked above is one of the better implementations.






            share|improve this answer












            There are a lot of features of daemon which do not apply to other process. It needs to close its stdout and stdin, and it needs to dissociate from the controlling process, and its needs to have its cwd set to root (amongst other things). There are minimal daemons which can act as a controlling process for running normal processes as if they were daemons - e.g. daemonize



            You also may need custom signal handling and (if you want any dianostic/audit capability) logging capability.



            However with the POSIX extensions, all the required functions are exposed - so you can setsid() and fork() from within your own code - or even just download some pre-cooked php code to do all this for you.



            BEWARE: there are a lot of people publishing code and advice on the internet which is wrong - but they found it worked for them - the code I've linked above is one of the better implementations.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Feb 13 at 23:11









            symcbean

            2,26411121




            2,26411121












            • … including advice like this answer about what dæmons need to do, which was first noted as wrong advice by IBM in 1995. Ali Erdinç Köroglu that you hyperlink to has a mention in the systemd House of Horror. The idea that one can "daemonize" from an interactive login session to a context outwith it grew to be a fallacy after the 1980s.
              – JdeBP
              Feb 14 at 8:19












            • If the right answer is systemd then we're all in trouble.
              – symcbean
              Feb 14 at 13:23


















            • … including advice like this answer about what dæmons need to do, which was first noted as wrong advice by IBM in 1995. Ali Erdinç Köroglu that you hyperlink to has a mention in the systemd House of Horror. The idea that one can "daemonize" from an interactive login session to a context outwith it grew to be a fallacy after the 1980s.
              – JdeBP
              Feb 14 at 8:19












            • If the right answer is systemd then we're all in trouble.
              – symcbean
              Feb 14 at 13:23
















            … including advice like this answer about what dæmons need to do, which was first noted as wrong advice by IBM in 1995. Ali Erdinç Köroglu that you hyperlink to has a mention in the systemd House of Horror. The idea that one can "daemonize" from an interactive login session to a context outwith it grew to be a fallacy after the 1980s.
            – JdeBP
            Feb 14 at 8:19






            … including advice like this answer about what dæmons need to do, which was first noted as wrong advice by IBM in 1995. Ali Erdinç Köroglu that you hyperlink to has a mention in the systemd House of Horror. The idea that one can "daemonize" from an interactive login session to a context outwith it grew to be a fallacy after the 1980s.
            – JdeBP
            Feb 14 at 8:19














            If the right answer is systemd then we're all in trouble.
            – symcbean
            Feb 14 at 13:23




            If the right answer is systemd then we're all in trouble.
            – symcbean
            Feb 14 at 13:23


















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f423991%2frunning-a-php-file-from-init%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            サソリ

            広島県道265号伴広島線

            Accessing regular linux commands in Huawei's Dopra Linux