1

I have a Jenkins triggered project which has the following command wrapped in a PHP exec -

$dos2unix = exec("dos2unix ".$filePath);

It shows the following unwanted command output in the Jenkins console output and clutters the screen -

dos2unix: converting file /home/jenkins/deployment_beta/phing/scratchpad/adserver/app/webroot/openx/lib/OA/Dal/Delivery/mysql.php to UNIX format ...
dos2unix: converting file /home/jenkins/deployment_beta/phing/scratchpad/adserver/app/webroot/openx/lib/OA/Dal/Maintenance/Statistics/Common.php to UNIX format ...

I tried wrapping the command in an ob_start() and ob_end_clean() and also directed the output to /dev/null -

ob_start();
$dos2unix = exec("dos2unix ".$filePath." > /dev/null");
ob_end_clean();

But, I am still getting the same in console output.

Note - If I trigger the build manually in command line, I do not see this undesired output, regardless of whether I have directed the output to /dev/null or wrapped in output buffer statements.

I have read Suppressing output from exec() calls in PHP but could not solve the problem.

Community
  • 1
  • 1
Sandeepan Nath
  • 9,966
  • 17
  • 86
  • 144

1 Answers1

2

dos2unix prints its messages to stderr. To redirect them you need to use 2> instead of >.

$dos2unix = exec("dos2unix ".$filePath." 2> /dev/null");

Another option is to use option -q. With -q dos2unix will not print messages and it will not return an error code when something goes wrong.

$dos2unix = exec("dos2unix -q ".$filePath.");
  • Is there some way I can collect the output in either case (regardless of error), but suppress the console output at the same time, or am I contradicting myself? – Sandeepan Nath Mar 16 '17 at 06:47
  • I would like to collect the output in $dos2unix and be able to handle error/success. – Sandeepan Nath Mar 16 '17 at 07:25
  • I did this finally - `$returnVal = exec("sudo dos2unix 2>&1 ".$filePath,$outputArray,$result); if(count($outputArray)>0) { $finalLine = $outputArray[count($outputArray)-1]; $success = $result==0 && ($finalLine == "dos2unix: converting file ".$filePath." to UNIX format ..."); }` May be I should rely only on the $result. – Sandeepan Nath Mar 16 '17 at 09:39
  • I never used Jenkins. – Erwin Waterlander Mar 16 '17 at 21:24