问题答案 12026年6月18日 02:03
How to redirect output of systemd service to a file
When you want to redirect the output of a systemd service to a file, you can achieve this by modifying the service's Unit file. Here are the specific steps and examples:Step 1: Create or modify the service configuration fileFirst, ensure you have permissions to edit or create the system's Unit file. These files are typically located in the directory.Step 2: Configure log outputIn the service's configuration file, you can redirect output by configuring and . By default, this output is sent to the journal (systemd's logging system), but you can modify it to redirect output to a specified file.ExampleSuppose we have a service named , and we want to redirect its standard output and error output to different files.Open or create the service's Unit file:Add or modify the following lines:Save and close the file.Step 3: Reload systemd and restart the serviceAfter modifying the systemd configuration file, you need to reload systemd to apply the changes.Step 4: VerifyCheck the log files you specified to ensure the output is correctly redirected.Using this method, you can conveniently manage and view the service's runtime output, which aids in debugging and monitoring the service status. Remember that choosing the appropriate log file path and managing file permissions are also very important.