-
Notifications
You must be signed in to change notification settings - Fork 238
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
Comments
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
<#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: ***@***.***>
|
I'm using systemd services for my bots. In order to make it work you need to create a service file in
The service will automatically restart after crashing. Make it start automatically on system (re)boot: Start/Restart/Stop the service: |
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 |
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); |
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 |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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!
The text was updated successfully, but these errors were encountered: