The error message you're encountering, "pre-shutdown.service: Unable to locate executable '/usr/local/bin/pre-shutdown.sh': Permission denied," might seem a bit confusing because it combines two concepts: the inability to locate an executable and a permission denied error. However, the key issue here is the permission problem, not the location.
When systemd tries to execute a script or program, it runs under the context of the root user or another specified user (if configured to do so). The error you're seeing suggests that while systemd can see or "locate" the script /usr/local/bin/pre-shutdown.sh, it cannot execute it due to a permission issue.
Here are the steps you can take to resolve this:
1. Check Permissions
First, check the permissions of the /usr/local/bin/pre-shutdown.sh script:
bash
ls -l /usr/local/bin/pre-shutdown.sh
You will see something like:
plaintext
-rwxr--r-- 1 root root 123 Jan 1 12:00 /usr/local/bin/pre-shutdown.sh
The permissions in this example are rwx for the owner (root), r-- for the group (root), and r-- for others. This means the owner (root) has read, write, and execute permissions, but the group and others only have read permission.
2. Adjust Permissions
To fix the permission denied error, you need to ensure that the script has execute permissions for the user that systemd is attempting to run it as (usually root). You can add execute permissions for the owner, group, or others with the chmod command. For example, to add execute permissions for the owner (which is usually sufficient), you can run:
bash
chmod u+x /usr/local/bin/pre-shutdown.sh
Or, to add execute permissions for everyone:
bash
chmod a+x /usr/local/bin/pre-shutdown.sh
Verify the permissions have changed:
bash
ls -l /usr/local/bin/pre-shutdown.sh
3. Verify Script Header
Make sure the script itself is correctly formatted and starts with a proper shebang line (like #!/bin/bash or #!/usr/bin/env bash) to indicate which interpreter should be used to run the script.
4. Check for SELinux or AppArmor Restrictions
If you're using SELinux or AppArmor, there might be additional policy restrictions preventing the execution of the script. You can check the system logs (e.g., /var/log/audit/audit.log for SELinux) for denials. However, adjusting these policies is more complex and should be approached with caution.
5. Reload systemd Daemon and Try Again
After making these changes, reload the systemd daemon and attempt to start or restart your service:
If you're still encountering issues, inspect the journald logs for more detailed error messages:
bash
journalctl -u pre-shutdown.service
By following these steps, you should be able to resolve the permission denied error and successfully execute your pre-shutdown script.
User
How do I create a systemd script that runs on shutdown/reboot and is awaited upon
Assistant
To create a systemd service that runs on shutdown or reboot and is awaited upon, you need to create a service file with specific settings. Here's a step-by-step guide:
1. Create a Service File
Create a new file in /etc/systemd/system/ (or /usr/lib/systemd/system/ for system-wide services provided by packages, but we'll use /etc for custom services):
[Unit]: This section defines the unit and its metadata.
Description: A short description of the service.
DefaultDependencies=no: This line prevents systemd from adding the default dependencies, allowing for more control.
Before: Specifies that this service should be executed before the target services.
[Service]: This section defines the service itself.
Type=oneshot: Indicates that this service runs once and then exits.
ExecStart: Specifies the command to execute.
RemainAfterExit=yes: This option tells systemd to consider the service active even after it exits, which is useful for oneshot services.
[Install]: This section defines how the service should be installed.
WantedBy: Specifies the target that wants this service. By listing shutdown.target, reboot.target, halt.target, and poweroff.target, the service will be started before any of these shutdown/reboot targets.
3. Create the Script
Create the /usr/local/bin/pre-shutdown.sh script (or modify an existing one) and make sure it has execute permissions:
bash
sudo nano /usr/local/bin/pre-shutdown.sh
Example content for the script:
bash
#!/bin/bash
# Your pre-shutdown commands here
echo "Running pre-shutdown script..."
# Add your logic here
Make the script executable:
bash
sudo chmod a+x /usr/local/bin/pre-shutdown.sh
4. Enable and Reload systemd
Enable the service to start at boot (technically, before shutdown/reboot):
bash
sudo systemctl enable pre-shutdown.service
Reload systemd daemon to pick up the new service file:
bash
sudo systemctl daemon-reload
5. Test the Service
You can test the service by simulating a shutdown or reboot:
bash
sudo systemctl start pre-shutdown.service
Or, for a more realistic test, you can use:
bash
sudo systemctl halt
or
bash
sudo systemctl reboot
The pre-shutdown script should run before the system shuts down or reboots.
Note
Ensure that your script exits cleanly (with an exit code of 0) to indicate successful execution.
Be cautious with the commands in your pre-shutdown script, as they will run with elevated privileges.
By following these steps, you can create a systemd service that runs a script on shutdown or reboot and is awaited upon, ensuring that your pre-shutdown tasks are completed before the system goes down.