How to restart php script when it breaks #1053
Replies: 5 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: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
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: |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
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); |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
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!
Beta Was this translation helpful? Give feedback.
All reactions