Merge pull request #3828 from MHSanaei/restartXrayOption
Some checks failed
Release 3X-UI / build (386) (push) Has been cancelled
Release 3X-UI / build (amd64) (push) Has been cancelled
Release 3X-UI / build (arm64) (push) Has been cancelled
Release 3X-UI / build (armv5) (push) Has been cancelled
Release 3X-UI / build (armv6) (push) Has been cancelled
Release 3X-UI / build (armv7) (push) Has been cancelled
Release 3X-UI / build (s390x) (push) Has been cancelled
Release 3X-UI / Build for Windows (push) Has been cancelled

[feat] restart xray-core from cli #3825
This commit is contained in:
Alireza Ahmadi 2026-02-25 21:09:42 +01:00 committed by GitHub
commit 159b85f979
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 71 additions and 26 deletions

View file

@ -16,6 +16,7 @@ import (
"github.com/mhsanaei/3x-ui/v2/logger"
"github.com/mhsanaei/3x-ui/v2/sub"
"github.com/mhsanaei/3x-ui/v2/util/crypto"
"github.com/mhsanaei/3x-ui/v2/util/sys"
"github.com/mhsanaei/3x-ui/v2/web"
"github.com/mhsanaei/3x-ui/v2/web/global"
"github.com/mhsanaei/3x-ui/v2/web/service"
@ -70,7 +71,7 @@ func runWebServer() {
sigCh := make(chan os.Signal, 1)
// Trap shutdown signals
signal.Notify(sigCh, syscall.SIGHUP, syscall.SIGTERM)
signal.Notify(sigCh, syscall.SIGHUP, syscall.SIGTERM, sys.SIGUSR1)
for {
sig := <-sigCh
@ -108,6 +109,12 @@ func runWebServer() {
return
}
log.Println("Sub server restarted successfully.")
case sys.SIGUSR1:
logger.Info("Received USR1 signal, restarting xray-core...")
err := server.RestartXray()
if err != nil {
logger.Error("Failed to restart xray-core:", err)
}
default:
// --- FIX FOR TELEGRAM BOT CONFLICT (409) on full shutdown ---

View file

@ -7,11 +7,14 @@ import (
"encoding/binary"
"fmt"
"sync"
"syscall"
"github.com/shirou/gopsutil/v4/net"
"golang.org/x/sys/unix"
)
var SIGUSR1 = syscall.SIGUSR1
func GetTCPCount() (int, error) {
stats, err := net.Connections("tcp")
if err != nil {

View file

@ -12,8 +12,11 @@ import (
"strconv"
"strings"
"sync"
"syscall"
)
var SIGUSR1 = syscall.SIGUSR1
func getLinesNum(filename string) (int, error) {
file, err := os.Open(filename)
if err != nil {

View file

@ -12,6 +12,8 @@ import (
"github.com/shirou/gopsutil/v4/net"
)
var SIGUSR1 = syscall.Signal(0)
// GetConnectionCount returns the number of active connections for the specified protocol ("tcp" or "udp").
func GetConnectionCount(proto string) (int, error) {
if proto != "tcp" && proto != "udp" {

View file

@ -490,3 +490,7 @@ func (s *Server) GetCron() *cron.Cron {
func (s *Server) GetWSHub() any {
return s.wsHub
}
func (s *Server) RestartXray() error {
return s.xrayService.RestartXray(true)
}

View file

@ -10,4 +10,9 @@ depend() {
}
start_pre(){
cd /usr/local/x-ui
}
reload() {
ebegin "Reloading ${RC_SVCNAME}"
kill -USR1 $pidfile
eend $?
}

View file

@ -9,6 +9,7 @@ Environment="XRAY_VMESS_AEAD_FORCED=false"
Type=simple
WorkingDirectory=/usr/lib/x-ui/
ExecStart=/usr/lib/x-ui/x-ui
ExecReload=kill -USR1 $MAINPID
Restart=on-failure
RestartSec=5s

View file

@ -9,6 +9,7 @@ Environment="XRAY_VMESS_AEAD_FORCED=false"
Type=simple
WorkingDirectory=/usr/local/x-ui/
ExecStart=/usr/local/x-ui/x-ui
ExecReload=kill -USR1 $MAINPID
Restart=on-failure
RestartSec=5s

View file

@ -9,6 +9,7 @@ Environment="XRAY_VMESS_AEAD_FORCED=false"
Type=simple
WorkingDirectory=/usr/local/x-ui/
ExecStart=/usr/local/x-ui/x-ui
ExecReload=kill -USR1 $MAINPID
Restart=on-failure
RestartSec=5s

68
x-ui.sh
View file

@ -408,6 +408,16 @@ restart() {
fi
}
restart_xray() {
systemctl reload x-ui
LOGI "xray-core Restart signal sent successfully, Please check the log information to confirm whether xray restarted successfully"
sleep 2
show_xray_status
if [[ $# == 0 ]]; then
before_show_menu
fi
}
status() {
if [[ $release == "alpine" ]]; then
rc-service x-ui status
@ -2154,6 +2164,7 @@ show_usage() {
${blue}x-ui start${plain} - Start │
${blue}x-ui stop${plain} - Stop │
${blue}x-ui restart${plain} - Restart │
| ${blue}x-ui restart-xray${plain} - Restart Xray │
${blue}x-ui status${plain} - Current Status │
${blue}x-ui settings${plain} - Current Settings │
${blue}x-ui enable${plain} - Enable Autostart on OS Startup │
@ -2189,25 +2200,26 @@ show_menu() {
${green}11.${plain} Start │
${green}12.${plain} Stop │
${green}13.${plain} Restart │
${green}14.${plain} Check Status │
${green}15.${plain} Logs Management │
| ${green}14.${plain} Restart Xray │
${green}15.${plain} Check Status │
${green}16.${plain} Logs Management │
│────────────────────────────────────────────────│
${green}16.${plain} Enable Autostart │
${green}17.${plain} Disable Autostart │
${green}17.${plain} Enable Autostart │
${green}18.${plain} Disable Autostart │
│────────────────────────────────────────────────│
${green}18.${plain} SSL Certificate Management │
${green}19.${plain} Cloudflare SSL Certificate │
${green}20.${plain} IP Limit Management │
${green}21.${plain} Firewall Management │
${green}22.${plain} SSH Port Forwarding Management │
${green}19.${plain} SSL Certificate Management │
${green}20.${plain} Cloudflare SSL Certificate │
${green}21.${plain} IP Limit Management │
${green}22.${plain} Firewall Management │
${green}23.${plain} SSH Port Forwarding Management │
│────────────────────────────────────────────────│
${green}23.${plain} Enable BBR │
${green}24.${plain} Update Geo Files │
${green}25.${plain} Speedtest by Ookla │
${green}24.${plain} Enable BBR │
${green}25.${plain} Update Geo Files │
${green}26.${plain} Speedtest by Ookla │
╚────────────────────────────────────────────────╝
"
show_status
echo && read -rp "Please enter your selection [0-25]: " num
echo && read -rp "Please enter your selection [0-26]: " num
case "${num}" in
0)
@ -2253,43 +2265,46 @@ show_menu() {
check_install && restart
;;
14)
check_install && status
check_install && restart_xray
;;
15)
check_install && show_log
check_install && status
;;
16)
check_install && enable
check_install && show_log
;;
17)
check_install && disable
check_install && enable
;;
18)
ssl_cert_issue_main
check_install && disable
;;
19)
ssl_cert_issue_CF
ssl_cert_issue_main
;;
20)
iplimit_main
ssl_cert_issue_CF
;;
21)
firewall_menu
iplimit_main
;;
22)
SSH_port_forwarding
firewall_menu
;;
23)
bbr_menu
SSH_port_forwarding
;;
24)
update_geo
bbr_menu
;;
25)
update_geo
;;
26)
run_speedtest
;;
*)
LOGE "Please enter the correct number [0-25]"
LOGE "Please enter the correct number [0-26]"
;;
esac
}
@ -2305,6 +2320,9 @@ if [[ $# > 0 ]]; then
"restart")
check_install 0 && restart 0
;;
"restart-xray")
check_install 0 && restart_xray 0
;;
"status")
check_install 0 && status 0
;;