This script automates the creation or updating of systemd
service unit files. It validates user inputs, generates or modifies the service files, validates them, and enables them to start with the system.
- Purpose: Generate or update
systemd
service files. - Author: Lee Bussy
- Version: 1.0.0
- Usage:
-n
/--service-name
: Specify the service name (required).-e
/--exec-start
: Specify the command to run (required).- Additional options allow customization of the service unit (see below).
-
check_root
- Ensures the script is run with root privileges.
- Exits with error code
1
if the user is not root.
-
usage
- Displays usage instructions, including all options:
-n, --service-name
: Name of the service (required).-e, --exec-start
: Command to execute when the service starts (required).-d, --description
: Service description (optional).-u, --user
: User to run the service (optional).-g, --group
: Group to run the service (optional).-w, --working-directory
: Service working directory (optional).-a, --after
: Dependencies for service startup (optional).-U, --update
: Update existing service instead of creating a new one.
- Displays usage instructions, including all options:
-
parse_arguments
- Parses both short and long options using
getopt
. - Validates that required arguments (
-n
,-e
) are present. - Sets values for optional arguments (e.g., user, group).
- Parses both short and long options using
-
validate_inputs
- Ensures the service name only contains valid characters (
[a-zA-Z0-9_-]
). - Checks if the
ExecStart
command exists and is executable.
- Ensures the service name only contains valid characters (
-
create_or_update_service_file
- Generates a new service file or updates an existing one.
- Writes service file configuration to a temporary file:
[Unit]
section:Description
,After
(optional dependencies).[Service]
section:ExecStart
,User
,Group
,WorkingDirectory
,Restart=always
.[Install]
section:WantedBy=multi-user.target
.
- Moves the temporary file to
/etc/systemd/system/<service_name>.service
.
-
validate_service_file
- Uses
systemctl
to validate the syntax and content of the generated service file.
- Uses
-
enable_service
- Reloads the
systemd
configuration. - Enables the service to start at boot.
- Reloads the
-
main
- Orchestrates script execution:
- Ensures root privileges.
- Parses arguments.
- Validates inputs.
- Creates or updates the service file.
- Validates the file.
- Enables the service.
- Orchestrates script execution:
-
Root Privileges:
- The script checks for root permissions since it modifies system-level service files.
-
Argument Parsing:
- Handles all options and sets default values for optional parameters.
-
Validation:
- Ensures correctness of inputs, service name, and executable paths.
-
Service File Management:
- Either creates a new service file or updates an existing one.
-
Validation:
- Uses
systemctl
to ensure the service file is valid and syntactically correct.
- Uses
-
Enable Service:
- Reloads
systemd
and enables the service to start at boot.
- Reloads
# Create a new service
sudo ./script.sh -n myservice -e "/usr/bin/python3 /path/to/script.py" -d "My Python Service" -u myuser -g mygroup -w /home/myuser -a "network.target"
# Update an existing service
sudo ./script.sh -n myservice -e "/usr/bin/python3 /new/path/script.py" -U
-
Dynamic Option Parsing:
- Supports both short and long options via
getopt
.
- Supports both short and long options via
-
Service File Update:
- Allows modification of existing service files with the
-U
flag.
- Allows modification of existing service files with the
-
Error Handling:
- Validates all inputs and commands.
- Ensures service files are correctly formatted and operational.