0

I am trying to run a php script to restore a state after the server crashed, got restarted or smth. Because the php script needs the database to run I first tried running it by creating a file in init.d, which did not work, it just started whenever it wantend. So right now I think it is the easiest way to run the script on apache2 startup like discribed here. So currently I have added php -q /var/www/scripts/testing.php & ;; to do_start() in /etc/init.d/apache2 like this:

do_start()
{
        # Return
        #   0 if daemon has been started
        #   1 if daemon was already running
        #   2 if daemon could not be started

        if pidofproc -p $PIDFILE "$DAEMON" > /dev/null 2>&1 ; then
                return 1
        fi

        if apache_conftest ; then
                $APACHE2CTL start
                php -q /var/www/scripts/testing.php &
                ;;
                apache_wait_start $?
                return $?
        else
                APACHE2_INIT_MESSAGE="The apache2$DIR_SUFFIX configtest failed."
                return 2
        fi
}

But because this didn't work at all, I have also added this php execution to the restart) part as mentioned in the link. This looks like this:

 restart)
        log_daemon_msg "Restarting $DESC" "$NAME"
        do_stop stop
        case "$?" in
                0|1)
                        do_start
                        case "$?" in
                                0)
                                        log_end_msg 0
                                        ;;
                                1|*)
                                        log_end_msg 1 # Old process is still or failed to running
                                        print_error_msg
                                        exit 1
                                        ;;
                        esac
                        ;;
                *)
                        # Failed to stop
                        log_end_msg 1
                        print_error_msg
                        exit 1
                        ;;
        php -q /var/www/scripts/testing.php &
        ;;
        esac
        ;;

But still the script is not run. The php script looks like this:

<?php
file_put_contents('/var/www/html/log', "301,$timestamp,Recreating all connections after restart,N/A\n",FILE_APPEND);
?>

Because i wanted it to be as simple as possible, but the log file is still empty. I am open to any idea solving my problem.

p.s.: I have already tried to do this by a service in /etc/systemd/system/ but since I am starting a connection that is supposed to be persistent, I have to use either screen, nohup or disown. I have tried those three, but no of this worked, they just didn't start the script. (was bash back then, I switched to php to be able to run it from the apache2 file)

1
  • Syntax is for the php script or a calling script that executes the php? Doubt a closing round bracket is valid php syntax after a command Commented Oct 30, 2020 at 10:44

1 Answer 1

1

You should not use apache to start your script, but follow your first idea of using an own init-script unless your php script depends on the existence of apache.

Just place a shell script callmyphp into /etc/init.d that calls the php interpreter and passes your php script as an argument like:

#!/bin/sh
/usr/bin/php -q /path/to/myphp.php

Don't forget to make your calling script executabel with chmod 755 /etc/init.d/callmyphp.

Then add your calling script via symbolic links to the desired run levels, i.e. by running update-rc.d callmyphp defaults

See also https://debian-administration.org/article/28/Making_scripts_run_at_boot_time_with_Debian

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.