PythonServiceManager is a robust and versatile Bash script designed to simplify the process of managing Python scripts as system services on Linux. It provides an easy-to-use interface for setting up virtual environments, creating systemd service files, and managing the lifecycle of Python-based services.
- Set up Python virtual environments with customizable Python versions
- Create systemd service files automatically
- Manage service lifecycle (start, stop, restart, enable, disable)
- View service logs
- Update services and their dependencies
- Configure service settings via a central configuration file
- Support for multiple Python versions and virtual environments
- Linux operating system with systemd
- Bash shell
- sudo privileges
- Python (version 3.6 or later recommended)
- Clone this repository or download the
psm.sh
script. - Make the script executable:
chmod +x psm.sh
- Optionally, move the script to a directory in your PATH for easier access:
sudo mv psm.sh /usr/local/bin/pythonservicemanager
The basic syntax for using PythonServiceManager is:
sudo ./psm.sh <action> <script_name.py> [python_version] [venv_name]
Available actions:
setup
: Set up a new Python servicestart
: Start the servicestop
: Stop the servicerestart
: Restart the servicestatus
: Check the status of the serviceenable
: Enable the service to start on bootdisable
: Disable the service from starting on bootlogs
: View service logsupdate
: Update the service (reinstall dependencies and restart)config
: Edit the configuration file
Examples:
-
Set up a new service:
sudo ./psm.sh setup my_script.py 3.9 my_venv
-
Start a service:
sudo ./psm.sh start my_script.py
-
View service logs:
sudo ./psm.sh logs my_script.py
The python_service_manager.conf
file contains various settings to customize the behavior of PythonServiceManager. Key options include:
DEFAULT_PYTHON_VERSION
: The default Python version for new services.DEFAULT_VENV_NAME
: The default name for virtual environments.LOG_DIR
: Directory for storing service logs.DEFAULT_RESTART_POLICY
: Default restart policy for services.GLOBAL_ENV_VARS
: Global environment variables for all services.
For a complete list of options and their descriptions, please refer to the comments in the configuration file.
Contributions to PythonServiceManager are welcome! Please feel free to submit pull requests, create issues, or suggest new features.
This project is licensed under the MIT License - see the LICENSE file for details.
- Inspired by the need for a simple, yet powerful tool to manage Python services on Linux systems.
- Thanks to the open-source community for providing the tools and libraries that make this project possible.