Skip to content

Instantly share code, notes, and snippets.

@luisdelatorre012
Last active May 31, 2024 02:41
Show Gist options
  • Save luisdelatorre012/ca4f78f84544293a5153a1bc54470222 to your computer and use it in GitHub Desktop.
Save luisdelatorre012/ca4f78f84544293a5153a1bc54470222 to your computer and use it in GitHub Desktop.
Shell script for systemctl
You:
- #!/usr/bin/env bash
set -euo pipefail
SOURCE_DIR="/home/luis/job_files"
DEST_DIR="/etc/systemd/system"
# Function to check if the source directory exists
check_source_dir() {
if [ ! -d "$SOURCE_DIR" ]; then
echo "Error: Source directory $SOURCE_DIR does not exist."
exit 1
fi
}
# Function to check if there are any files in the source directory
check_files_in_source_dir() {
if [ -z "$(ls -A "$SOURCE_DIR")" ]; then
echo "Error: No files found in the source directory $SOURCE_DIR."
exit 1
fi
}
# Function to copy files from source to destination
copy_files() {
echo "Copying files from $SOURCE_DIR to $DEST_DIR"
sudo /bin/cp "$SOURCE_DIR"/* "$DEST_DIR"/
}
# Function to reload systemd manager configuration
reload_systemd() {
echo "Reloading systemd manager configuration"
sudo /bin/systemctl daemon-reload
}
# Function to enable each timer file
enable_timer_files() {
for file in "$DEST_DIR"/*.timer; do
if [ -f "$file" ]; then
echo "Enabling $file"
if ! sudo /bin/systemctl enable "${file##*/}"; then
echo "Error: Failed to enable ${file##*/}"
exit 1
fi
fi
done
echo "All timer files enabled successfully."
}
# Main script execution
check_source_dir
check_files_in_source_dir
copy_files
reload_systemd
enable_timer_files
ChatGPT:
- Your script looks well-structured and robust. Here are a few minor improvements and enhancements to ensure best practices and more user-friendly error handling:
1. Use functions to print error messages and exit.
2. Add a function to print success messages.
Here is the revised script:
```bash
#!/usr/bin/env bash
set -euo pipefail
SOURCE_DIR="/home/luis/job_files"
DEST_DIR="/etc/systemd/system"
# Function to print error messages and exit
print_error_and_exit() {
echo "Error: $1"
exit 1
}
# Function to print success messages
print_success() {
echo "Success: $1"
}
# Function to check if the source directory exists
check_source_dir() {
if [ ! -d "$SOURCE_DIR" ]; then
print_error_and_exit "Source directory $SOURCE_DIR does not exist."
fi
}
# Function to check if there are any files in the source directory
check_files_in_source_dir() {
if [ -z "$(ls -A "$SOURCE_DIR")" ]; then
print_error_and_exit "No files found in the source directory $SOURCE_DIR."
fi
}
# Function to copy files from source to destination
copy_files() {
echo "Copying files from $SOURCE_DIR to $DEST_DIR"
sudo /bin/cp "$SOURCE_DIR"/* "$DEST_DIR"/
print_success "Files copied successfully."
}
# Function to reload systemd manager configuration
reload_systemd() {
echo "Reloading systemd manager configuration"
sudo /bin/systemctl daemon-reload
print_success "Systemd manager configuration reloaded."
}
# Function to enable each timer file
enable_timer_files() {
for file in "$DEST_DIR"/*.timer; do
if [ -f "$file" ]; then
echo "Enabling $file"
if ! sudo /bin/systemctl enable "${file##*/}"; then
print_error_and_exit "Failed to enable ${file##*/}"
fi
fi
done
print_success "All timer files enabled successfully."
}
# Main script execution
check_source_dir
check_files_in_source_dir
copy_files
reload_systemd
enable_timer_files
```
In this revised version, the `print_error_and_exit` function handles error messages and exits, while the `print_success` function prints success messages. This improves readability and maintainability by centralizing message handling.
You:
- does anything look wrong with it?
ChatGPT:
- Your script looks well-written and functional. Here are a few minor points to consider for further refinement:
1. **sudo** : Ensure that the script is run with the necessary privileges. If running the script as a non-root user, the use of `sudo` will prompt for the password, which may be undesirable in automated scenarios.
2. **Checking the existence of destination directory** : While it's almost certain that `/etc/systemd/system` exists, it's good practice to check its existence before copying files.
Here’s the script with these considerations:
```bash
#!/usr/bin/env bash
set -euo pipefail
SOURCE_DIR="/home/luis/job_files"
DEST_DIR="/etc/systemd/system"
# Function to print error messages and exit
print_error_and_exit() {
echo "Error: $1"
exit 1
}
# Function to print success messages
print_success() {
echo "Success: $1"
}
# Function to check if the source directory exists
check_source_dir() {
if [ ! -d "$SOURCE_DIR" ]; then
print_error_and_exit "Source directory $SOURCE_DIR does not exist."
fi
}
# Function to check if there are any files in the source directory
check_files_in_source_dir() {
if [ -z "$(ls -A "$SOURCE_DIR")" ]; then
print_error_and_exit "No files found in the source directory $SOURCE_DIR."
fi
}
# Function to check if the destination directory exists
check_dest_dir() {
if [ ! -d "$DEST_DIR" ]; then
print_error_and_exit "Destination directory $DEST_DIR does not exist."
fi
}
# Function to copy files from source to destination
copy_files() {
echo "Copying files from $SOURCE_DIR to $DEST_DIR"
sudo /bin/cp "$SOURCE_DIR"/* "$DEST_DIR"/
print_success "Files copied successfully."
}
# Function to reload systemd manager configuration
reload_systemd() {
echo "Reloading systemd manager configuration"
sudo /bin/systemctl daemon-reload
print_success "Systemd manager configuration reloaded."
}
# Function to enable each timer file
enable_timer_files() {
for file in "$DEST_DIR"/*.timer; do
if [ -f "$file" ]; then
echo "Enabling $file"
if ! sudo /bin/systemctl enable "${file##*/}"; then
print_error_and_exit "Failed to enable ${file##*/}"
fi
fi
done
print_success "All timer files enabled successfully."
}
# Main script execution
check_source_dir
check_files_in_source_dir
check_dest_dir
copy_files
reload_systemd
enable_timer_files
```
This version includes a function `check_dest_dir` to ensure the destination directory exists before attempting to copy files. This small addition can help prevent errors if the destination directory is missing or incorrectly specified.
You:
- is there any reason why this line wouldn't do what I expect? It's not enabling the jobs sudo /bin/systemctl daemon-reload
ChatGPT:
- The line `sudo /bin/systemctl daemon-reload` should reload the systemd manager configuration. If it's not doing what you expect, there are a few potential reasons to consider:
1. **Permissions Issue** : If the script is not being run with sufficient privileges, it might fail silently or not perform the action correctly. Ensure the script is being executed with `sudo` or by a user with the necessary permissions.
2. **Systemd Path** : Ensure that `/bin/systemctl` is the correct path for `systemctl` on your system. Typically, it should be either `/bin/systemctl` or `/usr/bin/systemctl`.
3. **Error Handling** : Add error checking for the `daemon-reload` command to capture any errors.
4. **Environment Variables** : Ensure no environment variables are interfering with the execution of `systemctl`.
5. **Output Redirection** : Sometimes, output might not be visible if it’s being redirected or suppressed. Ensure you are not missing any error messages or output.
Here’s a revised script with added error handling for `daemon-reload`:
```bash
#!/usr/bin/env bash
set -euo pipefail
SOURCE_DIR="/home/luis/job_files"
DEST_DIR="/etc/systemd/system"
# Function to print error messages and exit
print_error_and_exit() {
echo "Error: $1"
exit 1
}
# Function to print success messages
print_success() {
echo "Success: $1"
}
# Function to check if the source directory exists
check_source_dir() {
if [ ! -d "$SOURCE_DIR" ]; then
print_error_and_exit "Source directory $SOURCE_DIR does not exist."
fi
}
# Function to check if there are any files in the source directory
check_files_in_source_dir() {
if [ -z "$(ls -A "$SOURCE_DIR")" ]; then
print_error_and_exit "No files found in the source directory $SOURCE_DIR."
fi
}
# Function to check if the destination directory exists
check_dest_dir() {
if [ ! -d "$DEST_DIR" ]; then
print_error_and_exit "Destination directory $DEST_DIR does not exist."
fi
}
# Function to copy files from source to destination
copy_files() {
echo "Copying files from $SOURCE_DIR to $DEST_DIR"
sudo /bin/cp "$SOURCE_DIR"/* "$DEST_DIR"/
print_success "Files copied successfully."
}
# Function to reload systemd manager configuration
reload_systemd() {
echo "Reloading systemd manager configuration"
if ! sudo /bin/systemctl daemon-reload; then
print_error_and_exit "Failed to reload systemd manager configuration."
fi
print_success "Systemd manager configuration reloaded."
}
# Function to enable each timer file
enable_timer_files() {
for file in "$DEST_DIR"/*.timer; do
if [ -f "$file" ]; then
echo "Enabling $file"
if ! sudo /bin/systemctl enable "${file##*/}"; then
print_error_and_exit "Failed to enable ${file##*/}"
fi
fi
done
print_success "All timer files enabled successfully."
}
# Main script execution
check_source_dir
check_files_in_source_dir
check_dest_dir
copy_files
reload_systemd
enable_timer_files
```
### Additional Steps for Debugging:
1. ** Command Manually** : Execute `sudo /bin/systemctl daemon-reload` manually in your terminal to check if it works outside the script.
2. **Check Logs** : Look at the systemd logs using `journalctl` to see if there are any error messages or warnings related to `systemctl daemon-reload`.
3. **Verbose Output** : Add `set -x` at the top of your script for more verbose output to see what exactly is being executed.
By incorporating these checks, you can better understand why the `daemon-reload` command might not be behaving as expected in your script.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment