When I try to execute my Perl script, which generates an Excel file as output, on a Jenkins agent running as a service on Windows Server 2019, it fails to create or launch Excel. However, the same script works fine when the Jenkins agent runs as a process or when I execute it manually on the server.
The Jenkins agent service is running with an admin account, so there are no permission issues.
Please share your valuable ideas to resolve this issue.
Hello and welcome to this community, @sankarpraveen02.
The issue you’re encountering is likely due to the fact that services on Windows, including the Jenkins agent running as a service, do not have access to the desktop environment. This prevents the service from launching GUI applications like Excel.
To resolve this issue, you may try the following steps:
- Allow the service to interact with the desktop:
- Open the
Services
management console (services.msc
). - Locate the Jenkins agent service.
- Right-click on the service and select
Properties
. - Go to the
Log On
tab. - Check the box that says
Allow service to interact with desktop
. - Restart the Jenkins agent service.
- Use a headless mode for Excel: If your script can be modified to use a headless mode for Excel (if available), this would avoid the need to launch the Excel GUI.
- Run the Jenkins agent as a scheduled task: Instead of running the Jenkins agent as a service, you can configure it to run as a scheduled task with the option to run only when the user is logged on. This will allow the agent to interact with the desktop.
- Use a different method to generate Excel files: Consider using a Perl module that does not require launching Excel, such as
Spreadsheet::WriteExcel
orExcel::Writer::XLSX
, which can create Excel files directly.
Here is an example of how you could use Spreadsheet::WriteExcel
to create an Excel file without launching Excel:
use strict;
use warnings;
use Spreadsheet::WriteExcel;
# Create a new Excel workbook
my $workbook = Spreadsheet::WriteExcel->new('output.xls');
# Add a worksheet
my $worksheet = $workbook->add_worksheet();
# Write some data
$worksheet->write(0, 0, 'Hello, Excel!');
# Close the workbook
$workbook->close();