DiscordPHP icon indicating copy to clipboard operation
DiscordPHP copied to clipboard

How to restart php script when it breaks

Open m00nm00nm00n opened this issue 3 years ago • 5 comments

Hi,

so I often have my 2way bot script crashing just due to bad commands or something... my question is how can I put it on the server and have it reboot automatically if it crashes?

Any advice is appreciated! thank you!

m00nm00nm00n avatar Mar 18 '22 04:03 m00nm00nm00n

Fırst you should fix it. Then you can use something like supervisor to run it indefinitely.

On Fri, 18 Mar 2022, 05:26 m00nm00nm00n, @.***> wrote:

Hi,

so I often have my 2way bot script crashing just due to bad commands or something... my question is how can I put it on the server and have it reboot automatically if it crashes?

Any advice is appreciated! thank you!

— Reply to this email directly, view it on GitHub https://github.com/discord-php/DiscordPHP/issues/762, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAE7SAL6HQTWCBGQ5LDTTGDVAQAXNANCNFSM5RAYMZXQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you are subscribed to this thread.Message ID: @.***>

shehi avatar Mar 18 '22 08:03 shehi

I'm using systemd services for my bots. In order to make it work you need to create a service file in /etc/systemd/system, e.g. touch /etc/systemd/system/discordbot.system:

[Unit]
Description=Discord Bot
After=multi-user.target
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/path/to/your/php -f /absolute/path/to/your/bot.php
User=<system-user-to-run-the-script-as>
Group=<system-group-to-run-the-script-as>
Type=idle
Restart=always
RestartSec=15
RestartPreventExitStatus=0
TimeoutStopSec=10

[Install]
WantedBy=multi-user.target

The service will automatically restart after crashing.


Make it start automatically on system (re)boot: sudo systemctl enable discordbot

Start/Restart/Stop the service: sudo systemctl start discordbot sudo systemctl restart discordbot sudo systemctl stop discordbot

oliverschloebe avatar Mar 18 '22 10:03 oliverschloebe

classic shell approach

#!/bin/bash
while :
do
	date
	echo "Starting DiscordPHP Bot..."
	php -f "bot.php"
	phpexitstatus=$?
	if [ $phpexitstatus -eq 255 ]; then
		echo "Script crashed, restarting in 2 minutes..."
		sleep 120
		continue
	fi
	echo "Script terminated with exit status $phpexitstatus"
	date
	break
done

2 minutes is a good time to wait until the bot session turns offline, which is recommended if the bot was crashed. But you can change it as you wish

With that script, you can combine with screen, nohup and also add it in crontab Example screen: screen -dmS DiscordPHP_Bot discordphpbot.sh Example crontab + screen: @reboot screen -dmS DiscordPHP_Bot /path/to/discordphpbot.sh

SQKo avatar Mar 19 '22 16:03 SQKo

This might help, inside the php script this can be added (from stackoverflow - https://stackoverflow.com/questions/9798438/automatically-restart-php-script-on-exit):

<?php
    echo ++$argv[1];    
    $_ = $_SERVER['_']; 

    register_shutdown_function(function () {
        global $_, $argv; 
        // restart myself
        pcntl_exec($_, $argv);
    });

    echo "\n======== start =========\n";
    // do a lot of stuff
    $cnt = 0;
    while( $cnt++ < 10000000 ){}
    echo "\n===== what if? =========\n";
    require 'OOPS! I dont exist.'; // FATAL Error:

    // we can't reach here
    echo "\n========== end =========\n";        
    die; // exited properly
    // we can't reach here 
    pcntl_exec($_, $argv);

alexandre433 avatar Apr 29 '22 19:04 alexandre433

I currently use this script for my bots running on Windows. You will need to adjust the php parameters as needed.

chcp 65001
@echo off
cls
set watch=Discord
title %watch% Watchdog
:watchdog
echo (%time%) %watch% started.
php -dopcache.enable_cli=1 -dopcache.jit_buffer_size=264M "run.php" > botlog.txt
echo (%time%) %watch% closed or crashed, restarting.
goto watchdog

valzargaming avatar Sep 08 '22 16:09 valzargaming