Add cron service restart functionality in CronUtil

- Introduced a static method `restartCronService` to restart the cron service across various distributions, ensuring immediate application of changes.
- Updated `website.py` to call `restartCronService` after modifying cron jobs, with error handling to return appropriate responses if the restart fails.
- Enhanced overall reliability of cron job management by ensuring the service is restarted after changes are made.
https://github.com/usmannasir/cyberpanel/issues/1589
This commit is contained in:
Master3395
2025-10-29 22:55:38 +01:00
parent e745d59a13
commit ddf9f5a9b3
3 changed files with 93 additions and 56 deletions

View File

@@ -4667,6 +4667,15 @@ context /cyberpanel_suspension_page.html {
CronUtil.CronPrem(0)
if output.find("1,") > -1:
# Restart cron service to apply changes immediately
restart_success, restart_error = CronUtil.restartCronService()
if not restart_success:
# Strict mode: return error response if restart fails
dic = {'getWebsiteCron': 0, 'error_message': f'Cron job modified but service restart failed: {restart_error}. Please manually restart cron service.'}
json_data = json.dumps(dic)
return HttpResponse(json_data)
data_ret = {"getWebsiteCron": 1,
"user": website.externalApp,
"cron": finalCron,
@@ -4708,6 +4717,15 @@ context /cyberpanel_suspension_page.html {
CronUtil.CronPrem(0)
if output.find("1,") > -1:
# Restart cron service to apply changes immediately
restart_success, restart_error = CronUtil.restartCronService()
if not restart_success:
# Strict mode: return error response if restart fails
dic = {'remCronbyLine': 0, 'error_message': f'Cron job removed but service restart failed: {restart_error}. Please manually restart cron service.'}
json_data = json.dumps(dic)
return HttpResponse(json_data)
data_ret = {"remCronbyLine": 1,
"user": website.externalApp,
"removeLine": output.split(',')[1],
@@ -4764,16 +4782,22 @@ context /cyberpanel_suspension_page.html {
execPath = execPath + " addNewCron --externalApp " + website.externalApp + " --finalCron '" + finalCron + "'"
output = ProcessUtilities.outputExecutioner(execPath, website.externalApp)
# Set proper permissions for Ubuntu/Debian
if ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu or ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu20:
command = 'chmod 600 %s' % (cronPath)
ProcessUtilities.executioner(command)
command = 'systemctl restart cron'
ProcessUtilities.executioner(command)
CronUtil.CronPrem(0)
if output.find("1,") > -1:
# Restart cron service to apply changes immediately (all distributions)
restart_success, restart_error = CronUtil.restartCronService()
if not restart_success:
# Strict mode: return error response if restart fails
dic = {'addNewCron': 0, 'error_message': f'Cron job added but service restart failed: {restart_error}. Please manually restart cron service.'}
json_data = json.dumps(dic)
return HttpResponse(json_data)
data_ret = {"addNewCron": 1,
"user": website.externalApp,