Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to restart php script when it breaks #762

Closed
m00nm00nm00n opened this issue Mar 18, 2022 · 5 comments
Closed

How to restart php script when it breaks #762

m00nm00nm00n opened this issue Mar 18, 2022 · 5 comments
Labels

Comments

@m00nm00nm00n
Copy link

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!

@shehi
Copy link
Contributor

shehi commented Mar 18, 2022 via email

@oliverschloebe
Copy link
Contributor

oliverschloebe commented Mar 18, 2022

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

@SQKo
Copy link
Member

SQKo commented Mar 19, 2022

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 SQKo added the question label Mar 19, 2022
@key2peace key2peace pinned this issue Apr 6, 2022
@alexandre433
Copy link

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);

@valzargaming
Copy link
Member

valzargaming commented Sep 8, 2022

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

@discord-php discord-php locked and limited conversation to collaborators Feb 1, 2023
@key2peace key2peace converted this issue into discussion #1053 Feb 1, 2023
@key2peace key2peace unpinned this issue Feb 1, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Projects
None yet
Development

No branches or pull requests

6 participants