Linux – Cannot run perl script with sudo

linuxperlredhat

I have a perl script (test.pl). If I run this cript by command perl test.pl, it work as normal. However, when I run command sudo perl test.pl, the error below occurs:

Can't locate Log/Log4perl.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .)

I think it is strange, because I run script by root. Do you know why?

Best Answer

You may get such an error if the module you are using is not installed which seems not our case here.

The other possibility is to have a different @INC array between the two users you are running as. You can verify this using:

$ perl -V
$ sudo perl -V

Compare the output to see the different paths in @INC. You can then fix the @INC by adding the missing path or install the needed module in one of the already defined paths.

Related Topic