120 lines
3.8 KiB
Bash
120 lines
3.8 KiB
Bash
|
#!/bin/bash
|
||
|
# daemonize.sh
|
||
|
# Installs Skier233's NSFW AI Model Server as a daemon on systemd-based systems
|
||
|
# https://github.com/skier233/nsfw_ai_model_server
|
||
|
#
|
||
|
# Author: voidf1sh (Skylar Grant)
|
||
|
# This script is unofficial and not endorsed, developed, or supported by Skier233
|
||
|
#
|
||
|
# Usage: source daemonize.sh
|
||
|
|
||
|
# Check if the script is being run as root
|
||
|
if [ "$EUID" -eq 0 ]; then
|
||
|
echo "Please run as yourself, not root, we need environment variables from your user session."
|
||
|
exit
|
||
|
fi
|
||
|
|
||
|
# Check if the script is being run on a systemd-based system
|
||
|
if [ ! -d "/etc/systemd" ]; then
|
||
|
echo "This script only works on systemd-based systems"
|
||
|
exit
|
||
|
fi
|
||
|
|
||
|
# Variables
|
||
|
activate_name="ai_model_server"
|
||
|
miniconda_dir="$HOME/miniconda3"
|
||
|
ai_server_dir="$HOME/nsfw_ai_model_server"
|
||
|
service_name="nsfw_ai_model_server"
|
||
|
|
||
|
# Activate the Conda environment
|
||
|
conda activate $activate_name
|
||
|
if [ $? -ne 0 ]; then
|
||
|
echo "Error activating the Conda environment, please activate it manually."
|
||
|
exit
|
||
|
fi
|
||
|
|
||
|
|
||
|
# Print env vars for debugging, user, group, conda
|
||
|
echo "This script installs Skier233's NSFW AI Model Server as a daemon on systemd-based systems."
|
||
|
echo "https://github.com/skier233/nsfw_ai_model_server"
|
||
|
echo "Author: voidf1sh (Skylar Grant)"
|
||
|
echo "This script is unofficial and not endorsed, developed, or supported by Skier233"
|
||
|
echo
|
||
|
# DEBUG:
|
||
|
# echo "Environment Variables:"
|
||
|
# echo "USER: $USER"
|
||
|
# echo "Conda Environment: $CONDA_DEFAULT_ENV"
|
||
|
# echo "Miniconda Directory: $miniconda_dir"
|
||
|
# echo "AI Server Directory: $ai_server_dir"
|
||
|
# echo "Service Name: $service_name"
|
||
|
# echo
|
||
|
|
||
|
# Check if the MiniConda directory actually exists
|
||
|
if [ ! -d "$miniconda_dir" ]; then
|
||
|
echo "Miniconda directory not found, please install Miniconda first or modify the script to point to the correct directory (\$miniconda_dir)."
|
||
|
exit
|
||
|
fi
|
||
|
|
||
|
# Check if the Conda environment matches the expected environment
|
||
|
if [ "$CONDA_DEFAULT_ENV" != "$activate_name" ]; then
|
||
|
echo "Conda environment $activate_name not found, please activate the correct environment or modify the script to point to the correct environment (\$activate_name)."
|
||
|
exit
|
||
|
fi
|
||
|
|
||
|
# Check if the server.py file exists
|
||
|
if [ ! -f "$ai_server_dir/server.py" ]; then
|
||
|
echo "server.py not found, please install Skier233's NSFW AI Model Server first or modify the script to point to the correct directory (\$ai_server_dir)."
|
||
|
exit
|
||
|
fi
|
||
|
|
||
|
# Let the user know why we need sudo
|
||
|
echo "Creating systemd file at /etc/systemd/system/$service_name.service..."
|
||
|
|
||
|
# Create the systemd service file
|
||
|
cat <<EOF | sudo tee /etc/systemd/system/$service_name.service
|
||
|
[Unit]
|
||
|
Description=Skier233's NSFW AI Model Server
|
||
|
After=network.target
|
||
|
|
||
|
[Service]
|
||
|
Type=simple
|
||
|
User=$USER
|
||
|
Group=$USER
|
||
|
WorkingDirectory=$ai_server_dir
|
||
|
ExecStart=/bin/bash -c "source $miniconda_dir/etc/profile.d/conda.sh && conda activate $activate_name && python server.py"
|
||
|
Restart=on-failure
|
||
|
Environment="PATH=$miniconda_dir/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
|
||
|
|
||
|
[Install]
|
||
|
WantedBy=multi-user.target
|
||
|
EOF
|
||
|
|
||
|
echo "============================================================"
|
||
|
|
||
|
# Make sure there were no errors
|
||
|
if [ $? -ne 0 ]; then
|
||
|
echo "Error creating the service file, please check the output above."
|
||
|
exit
|
||
|
fi
|
||
|
|
||
|
# Deactivate the environment
|
||
|
conda deactivate
|
||
|
if [ $? -ne 0 ]; then
|
||
|
echo "Error deactivating the Conda environment, please deactivate it manually."
|
||
|
fi
|
||
|
|
||
|
# Reload the systemd daemon and enable the service
|
||
|
echo "Service file created, reloading systemd daemon and enabling the service..."
|
||
|
sudo systemctl daemon-reload
|
||
|
if [ $? -ne 0 ]; then
|
||
|
echo "Error reloading the systemd daemon, please check the output above."
|
||
|
exit
|
||
|
fi
|
||
|
|
||
|
sudo systemctl enable --now $service_name
|
||
|
if [ $? -ne 0 ]; then
|
||
|
echo "Error enabling the service, please check the output above."
|
||
|
exit
|
||
|
fi
|
||
|
|
||
|
echo "Service enabled, you can check the status with 'systemctl status $service_name'"
|