How to run command in Perl without sudo?

697 Views Asked by At

I have problematic use case:

I run Perl script with sudo (from Jenkins):

sudo /usr/bin/perl /Users/snaggs/scripts/build_ios.pl ${BUILD_NUMBER} $Version $isPublish

The script runs several commands from CLI.

one of them is: $ pod spec lint that fails with error: Cannot run pod as root.

my $command = "/usr/local/bin/pod spec lint $POD_SPEC_FILE_ABS --verbose";      
my $podResults = `$command`; 

I don't want to remove sudo.

How can I run specific command into the script without super user?

2

There are 2 best solutions below

0
On

From man sudo:

sudo -v [-AknS] [-g group] [-h host] [-p prompt] [-u user]

Use the -u flag to specify a user who isn't the superuser but has the permissions needed to do what you want to do.


I wouldn't want Jenkins running test code as a regular user though (nor as the super user).

A better approach would be to set the ownership and permissions of the files and directories that Jenkins needs to access to grant permission using a group, make Jenkins run as a member of that group, and then remove sudo.

0
On

You should use sudo my $command = "sudo -u $username /usr/local/bin/pod spec lint $POD_SPEC_FILE_ABS --verbose";