wlsstart script with an absolute path versus a relative path in RHEL8 produces distinct execution environments that affect process ownership, working directory resolution, and signal handling. The absolute path typically invokes the script via systemctl, ensuring it runs as a managed service unit with strict isolation and logging. In contrast, running ./wlsstart executes the script directly in the current user’s shell, inheriting immediate environment variables but lacking persistent service management features.
Understanding Path Resolution Differences
When you execute a script using its full location, such as/opt/wls/bin/wlsstart, the shell resolves the path immediately before launching the process. This method is explicit and leaves no ambiguity about which binary is being executed. It is particularly useful when debugging or performing manual interventions because the command line clearly states the target file. However, this approach bypasses any aliasing or PATH-based lookups that might be configured for convenience.
Conversely, using a relative path like ./wlsstart relies on the current working directory to locate the executable. This method is more flexible for interactive sessions where you navigate frequently between directories. The key distinction lies in how the operating system interprets the context of the execution. Relative paths are dynamic; they change meaning if your current directory changes. Absolute paths remain constant regardless of where you are in the filesystem hierarchy.
Systemctl Service Execution Context
In RHEL8, scripts associated with services are often managed bysystemd. When systemctl start wlsstart.service is used, the system does not simply run the script in your terminal. Instead, it spawns a new process under the control of the service manager. This means the working directory for the script is usually set to / or a specific directory defined in the .service file, not necessarily the directory where the script resides.
This isolation is critical for production stability. Environment variables from your login session are not automatically inherited. You must explicitly define them within the service unit file or use EnvironmentFile= directives. If your wlsstart script depends on local variables like $HOME or custom application settings, running it via systemctl may fail silently or behave unexpectedly if those variables are not properly scoped in the service definition.
Direct Execution and Shell Inheritance
Running./wlsstart directly executes the script in the current shell instance or as a child process of the current shell. This method inherits all exported environment variables from your current session. This is often why direct execution seems to "work" while the service fails; the script finds the libraries or configuration files it expects because they are present in your interactive environment.
However, this convenience comes at the cost of manageability. Signals sent to the terminal, such as closing the SSH session or pressing Ctrl+C, will terminate the process immediately. There is no automatic restart policy, no journalctl logging integration, and no dependency management. If the script crashes, it stays down until a human intervenes. This makes direct execution suitable for testing but risky for any workload requiring uptime.
Working Directory Implications
One of the most common sources of error when switching between these two methods is the working directory. When you run./wlsstart, the script’s internal calls to other files (like reading a config file in the same folder) work because the relative path resolves correctly against your current position. When systemctl runs the script, the working directory is likely root (/). A relative path inside the script, such as read ./config.txt, will fail because it looks for config.txt in the root directory.
To fix this, scripts intended for systemd should either use absolute paths for all internal references or include a command at the start to cd into their own directory. For example, adding cd "$(dirname "$0")" ensures the script operates relative to its own location, regardless of how it was invoked. This practice bridges the gap between interactive ease-of-use and service robustness.
Signal Handling and Process Lifecycle
The lifecycle of a process started viasystemctl is governed by systemd directives like Restart=on-failure or TimeoutStopSec. These ensure that transient errors do not lead to permanent downtime. Direct execution offers no such protections. Furthermore, systemd handles standard output and error streams, redirecting them to journalctl. Direct execution sends output to your terminal screen, which can clutter logs or be lost if the session disconnects.
Understanding these differences allows administrators to choose the right tool for the job. Use direct execution for quick tests and debugging. Use systemctl for anything that needs to survive reboots, handle errors gracefully, or integrate with system monitoring tools. Always verify the working directory and environment scope when troubleshooting discrepancies between manual and service-based starts.
For more details on managing script execution across different operating systems, see Determining Script Execution on Windows, Linux, and MacOS. Additionally, understanding how to handle output in bash scripts can help diagnose logging issues seen here, as discussed in Understanding Window Specific URL Curl Output with Bash.
Frequently Asked Questions
Why does my script work manually but fail as a service?
This usually happens because the service runs in a restricted environment without your user’s environment variables. Check your service file to ensure all necessary paths and variables are explicitly defined.
How do I find the working directory of a running systemd service?
You can check the status of the service using systemctl status <service-name> and look for the WorkingDirectory= directive in the loaded unit file configuration.
Can I use relative paths inside a systemd service script?
Yes, but only if the script explicitly changes to its own directory first. Without a cd command, relative paths will resolve against the service’s default working directory, which is often /.
What is the best way to debug a failing systemctl start command?
Use journalctl -u <service-name> --since "5 minutes ago" to view the logs generated by the service manager. This shows stderr and stdout captured during the execution attempt.
Does running ./script.sh inherit aliases from my bashrc?
No, non-interactive shells do not source .bashrc by default. Aliases are generally not available unless explicitly expanded or replaced with functions in the script.