To debug, I changed the exec command to
$command = $cmd . ' > debug.log 2>&1';
Now, the log output showed that a perl module required for starting the script was not found. I missed this, because in my shell I was running as root user while testing. The www-data user did not have access to these modules.
To fix this, I had to create shell and sudo rights for www-data. I then logged in as www-data, used cpan minus to install the required modules, and then logged out. Now the script started running properly.