Creating a few scripts showcasing the use case for sysadmins and providing deployment and automation steps on this great idea. Here’s a template for your ideal Shell script.
In shared hosting environments, managing user accounts efficiently is crucial. Whether you’re a web hosting provider or a system administrator, automating tasks like account termination can save you time and effort. In this article, we’ll introduce you to a Bash script that automates account termination in cPanel/WHM. We’ll also walk you through the steps to deploy and automate this script using cron jobs.
Introduction
The provided Bash script automates the process of account termination in cPanel/WHM servers. It’s designed to target root-owned accounts, ensuring that reseller-owned accounts remain untouched. This script is particularly useful if you need to clean up unused or overdue accounts efficiently.
Prerequisites
Before you proceed, ensure the following prerequisites are met:
- You have SSH access to your cPanel/WHM server.
- You have root or sudo access to execute commands.
- The script only deletes suspended cPanel accounts by default.
Script type 1: Auto delete all suspended cPanel accounts in ‘Overdue on Payment’ reason that failed to be deleted by WHMCS cron run (Excludes reseller and their accounts)
#!/bin/bash
# Set the suspension reason
SUSPENSION_REASON="Overdue on Payment"
# Create a log file with the current date and time
current_datetime=$(date +"%Y%m%d%H%M%S")
log_file="auto-terminate-log-$current_datetime.txt"
# Initialize counters
root_owned_deleted=0
failed_to_delete=0
# Start timestamp
start_time=$(date +%s)
# Interrupt handler
function cleanup() {
end_time=$(date +%s)
execution_time=$((end_time - start_time))
echo ""
echo "Deletion process completed."
echo "$root_owned_deleted root-owned accounts deleted."
echo "$failed_to_delete accounts failed to delete."
echo "Execution time: $execution_time seconds."
echo "Log saved in $log_file."
exit
}
trap cleanup INT
# Create or append to the log file
touch "$log_file"
# Display a header
echo "Auto-Terminate Script"
echo "Suspension Reason: $SUSPENSION_REASON"
echo "----------------------------------"
# List all suspended accounts and show ownership type
echo "Suspended Accounts:"
for account_file in /var/cpanel/suspended/*; do
# Extract the account username from the file name
account=$(basename "$account_file")
# Use 'whmapi1' to get account ownership information
account_info=$(whmapi1 accountsummary user="$account")
# Check if the line 'owner: root' is present in the response
if [[ "$account_info" =~ 'owner: root' ]]; then
# Display a real-time message
echo "Deleting root-owned account: $account..."
# Use 'yes' command to provide a default answer of 'y' (yes)
yes | /usr/local/cpanel/scripts/removeacct "$account"
# Check if the deletion was successful
if [ $? -eq 0 ]; then
# Increment the deleted count for root-owned accounts
((root_owned_deleted++))
# Log the deleted account
echo "Root-owned account $account deleted due to suspension reason '$SUSPENSION_REASON'." >> "$log_file"
else
# Increment the failed count
((failed_to_delete++))
echo "Failed to delete root-owned account: $account" >> "$log_file"
fi
else
# Account is reseller-owned, do not delete
echo "Skipping reseller-owned account: $account" >> "$log_file"
fi
done
# Cleanup and exit
cleanup
Script Overview
The Bash script performs the following tasks:
- Lists all suspended accounts and categorizes them as either root-owned or reseller-owned.
- Waits for 10 seconds before proceeding to account termination.
- Deletes root-owned accounts only if the suspension reason is “Overdue on Payment.”
- Logs the deletion process, including successful deletions and failures.
Deployment Steps
Follow these steps to deploy the script on your cPanel/WHM server:
- Create a Bash Script File: Create a new file on your server
nano auto-cp-terminate.sh
- Edit the Script: Copy and paste the script content from the provided Bash script into
auto-cp-terminate.sh
- Make the Script Executable: Run the following command to make the script executable:
chmod +x auto-cp-terminate.sh
- Run the Script: Execute the script using the following command:
./auto-cp-terminate.sh
The script will list suspended accounts, determine their ownership, and delete root-owned accounts with the “Overdue on Payment” suspension reason.
Automating Account Termination
To automate the account termination process, you can use a cron job. Here’s how to set it up:
- Edit the crontab file: Run the following command to edit your user’s crontab:
crontab -e
- Add a Cron Job: To run the script daily at a specific time, add the following line to your crontab:
0 0 * * * /path/to/auto-cp-terminate.sh
Replace/path/to/auto-cp-terminate.sh
with the actual path to your script file. - Save and Exit: Save the changes and exit the text editor.
Now, the script will run automatically at the specified time every day, helping you maintain your server’s account hygiene.
Script type 2: Auto delete all suspended cPanel accounts with Account Owner Name and Suspension Reason as parameter
#!/bin/bash
# Function to check the owner of a cPanel account and suspend if the reason matches
function check_account_owner_and_suspend() {
local username="$1"
local owner_to_check="$2"
local suspension_reason="$3"
local account_info=$(whmapi1 accountsummary user="$username")
# Check if the line 'owner: $owner_to_check' is present in the response
if [[ "$account_info" =~ "owner: $owner_to_check" ]]; then
echo "Processing account: $username, Owner: $owner_to_check, Suspension Reason: $suspension_reason"
# Check if the suspension reason matches the specified reason
if grep -q "$suspension_reason" "/var/cpanel/suspended/$username"; then
echo "Deleting account: $username..."
yes | /usr/local/cpanel/scripts/removeacct "$username"
# Check if the deletion was successful
if [ $? -eq 0 ]; then
echo "Account $username deleted due to suspension reason '$suspension_reason'." >> "$log_file"
((deleted_count++))
else
echo "Failed to delete account: $username"
echo "Failed to delete account: $username, Owner: $owner_to_check, Suspension Reason: $suspension_reason" >> "$log_file"
fi
fi
fi
}
# Create a log file with the current date and time
current_datetime=$(date +"%Y%m%d%H%M%S")
log_file="auto-terminate-log-$current_datetime.txt"
# Initialize a counter for deleted accounts
deleted_count=0
# Prompt the user for the owner's name
echo -n "Enter the owner's name: "
read owner_name
# Prompt the user for the suspension reason
echo -n "Enter the suspension reason: "
read suspension_reason
# Loop through the suspended account files
for account_file in /var/cpanel/suspended/*; do
# Extract the account username from the file name
account=$(basename "$account_file")
# Call the function to check the account owner and suspend if needed
check_account_owner_and_suspend "$account" "$owner_name" "$suspension_reason"
done
# Display the total number of deleted accounts and a completion message
echo "Deletion process completed. $deleted_count accounts deleted. Log saved in $log_file."
Above mentioned script will delete all the Owner’s accounts for the given Reason.
In this script:
- It prompts the user to enter the owner’s name and suspension reason.
- It checks if the suspension reason and owner’s name match before deleting an account.
- It logs the deletion process, including successful deletions and failures, with the owner’s name and suspension reason.
Script type 3: Auto delete all suspended cPanel accounts (Also, root and reseller)
#!/bin/bash
# Set the suspension reason
SUSPENSION_REASON="Overdue on Payment"
# Create a log file with the current date and time
current_datetime=$(date +"%Y%m%d%H%M%S")
log_file="auto-terminate-log-$current_datetime.txt"
# Initialize a counter for deleted accounts
deleted_count=0
# Loop through the suspended account files
for account_file in /var/cpanel/suspended/*; do
# Extract the account username from the file name
account=$(basename "$account_file")
# Check if the suspension reason matches the specified reason
if grep -q "$SUSPENSION_REASON" "$account_file"; then
# Display a real-time message
echo "Deleting account: $account..."
# Use 'yes' command to provide a default answer of 'y' (yes)
yes | /usr/local/cpanel/scripts/removeacct "$account"
# Check if the deletion was successful
if [ $? -eq 0 ]; then
# Increment the deleted count
((deleted_count++))
# Log the deleted account
echo "Account $account deleted due to suspension reason '$SUSPENSION_REASON'." >> "$log_file"
else
echo "Failed to delete account: $account"
fi
fi
done
# Display the total number of deleted accounts and a completion message
echo "Deletion process completed. $deleted_count accounts deleted. Log saved in $log_file."
Above mentioned script, type 2 will delete all suspended cPanel accounts once started.
Conclusion
Automating account termination in cPanel/WHM can streamline your server management tasks. With the Bash script provided in this article, you can efficiently remove root-owned accounts with overdue payments while leaving reseller-owned accounts untouched. By setting up a cron job, you can automate this process, saving time and ensuring a well-maintained hosting environment.
Feel free to customize the script and cron job schedule to fit your specific needs. Managing your server has never been easier with this automated solution.
You can publish this article on your WordPress website, making sure to format it as needed and add any additional details or images you think would be helpful.
Share Your Comments & Feedback