Jenkins unable to execute .sh script on ubuntu permission denied? - shell

Currently attempting to execute a shell script via Jenkins using a pipeline job but receiving the following error:
/var/lib/jenkins/workspace/Warehouse_Tests/src/test/java/runners/sql.sh: Permission denied
I have configured:
visudo -f /etc/sudoers
to contain:
# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL
jenkins ALL= NOPASSWD: ALL
Any ideas?

Resolved by executing: sh chmod +x against the .sh script prior to triggering the scripts.

change the permission of files to jenkins:jenkins & 755 .
in the build only you can mentioned it .
also try to run with sudo
sudo sh /var/lib/jenkins/workspace/Warehouse_Tests/src/test/java/runners/sql.sh
suggestion - use Jenkins environment variable rather giving an actual path of your script.

Related

unable to excute command without sudo root

remote user:ab
escalated user: UNIX
when i am doing copy module to /etc/profile.d/.its throwing error permission denied.
but with shell and command module.
sudo cp myscript.sh /etc/profile.d/
its working from UNIX user.i want to use ansible module rather than shell or command.here issue with sudo from UNIX user to execute command with sudo privileged.Become user i can't use root directly. Dont have access through unix user i can use sudo.
already used below details.
become=yes
become_method=sudo
become_user=unix
become_ask_pass=false
sudo cp means that it is running as root, not the user unix.
Try removing the line
become_user=unix

Running Docker commands included in a shell script alongside other Linux commands and switching users

Using the Linux terminal, I run bash scripts (.sh files) containing sequences of commands I want to execute.
The issue is that I am unable to run a Docker command from within my shell script. I can run this Docker command when it's typed directly at the terminal with root privileges but not when I include it in the shell script file.
My script executed as a general user from command line, looks like this:
#!/usr/bin/env bash
cd /home/user/docker_backup
# remove /home/user/docker_backup/data
rm -rf data
# Switch to root privileges. my system is set to only run Docker as root
su
# Copy a folder from Docker container to host OS
docker cp <container-name>:/home/user/data /home/user/docker_backup
# More general user commands
cd ..
My code only runs until the su line above. After i enter the root password, nothing happens. if i type exit, i get permission errors, meaning the docker cp command failed.
**
This is my desired solution
**After thorough research, as I wanted to run my script as a general user, and only run certain commands as Root when necessary, I came up with a solution that works.
My script now looks like this (run with
$ sh script_name.sh):
#!/usr/bin/env bash
cd /home/user/docker_backup
# remove /home/user/docker_backup/data
rm -rf data
# Switch to root privileges. my system is set to only run Docker as root
su - root -c "docker cp <container-name>:/home/user/data /home/user/docker_backup"
# More general user commands
cd ..
Run shell script as general user. For commands that require root privileges, I use su - root -c "<command>". Terminal prompts for root password and executes command in quotes as root, then shell proceeds as general user.
Actually posting this as an answer:
You switch your current user to root during the script, but the script was executed by your own user.
So the docker cp command will also be executed as your own user, but you will be logged into the root account.
This results in you not seeing the output of docker cp (which might give you insight to not working - I think insufficient privilege).
A solution to this is either using sudo before docker cp, starting the script as root or adding your user to the group "docker", which authorizes your user to use the docker commands
I had the similar issue where the docker commands were running fine on the Terminal but the same commands were not running when I compiled them into a bash script and the issue was basically because of two reasons.
The docker commands need to be run with uplifted privileges that is with the sudo command ( Eg: sudo docker ps works but docker ps won't work). One could add the current user to docker group so that we need not use sudo with each docker command. Please visit this link and follow the section 2 to do the same.
Run the script in the correct way
One should have #! bin/bash at the starting of the script. It is a shebang that is required by each script.
One should save the file without .sh extension
One should provide the execution permission to the script by giving command chmod 777 script_name
run the script with bash script_name

Run commands via Jenkins?

I have a script on remote Ubuntu server. I trying to execute the script after the jenkins build is succeeded, But the error says like this:
sudo: no tty present and no askpass program specified
The configuration is given below,
Can anyone help me?
Thank You.
The problem is that your script uses sudo at some point. The usual way around is to add the script that requires you to use sudo to the sudoers.
Example: in your script you use sudo service apache2 reload, now create a bash script containing that line and add that script to the sudoers file.
New script name: /home/quaser/restart-apache.sh
Use: visudo
Add at bottom of the file:
jenkins ALL=(ALL) NOPASSWD: /home/quaser/restart-apache.sh
Now, in your script change: sudo service apache restart to sudo /home/quaser/restart-apache.sh and you should not be asked for a password.
I had the same problem, I solved that by commenting Defaults requiretty on /etc/sudoers
cat /etc/sudoers| grep tty
#Defaults requiretty
From the man page:
man sudoers | grep requiretty -A 5
requiretty If set, sudo will only run when the user is logged in
to a real tty. When this flag is set, sudo can only be
run from a login session and not via other means such
as cron(8) or cgi-bin scripts. This flag is off by
default.

Call remote sh script from local sh file

I have a problem with running sh scripts on CentOS which calls remote sh file. On user#host1 I have start.sh file with next command inside
NODE1_SSH_PATH=user#host2
PROGRAM_HOME=/home/user/app
ssh $NODE1_SSH_PATH $PROGRAM_HOME/bin/run.sh > start.log
Result of this script is next:
bash: /home/user/app/bin/run.sh: Permission denied
I tried run this script with chmod like this:
ssh $NODE1_SSH_PATH chmod u+x $PROGRAM_HOME/bin/run.sh > start.log
But in this case I have't got any result, log file is empty to. Could someone help me to slow this I hope simple task?
I believe /home/user/app/bin/run.sh is not executable.
try this
ssh $NODE1_SSH_PATH /bin/bash $PROGRAM_HOME/bin/run.sh > start.log

Sourcing a shell script, while running with sudo

I would like to write a shell script that sets up a mercurial repository, and allow all users in the group
"developers" to execute this script.
The script is owned by the user "hg", and works fine when ran. The problem comes when I try to run it
with another user, using sudo, the execution halts with a "permission denied" error, when it tries to source another file.
The script file in question:
create_repo.sh
#!/bin/bash
source colors.sh
REPOROOT="/srv/repository/mercurial/"
... rest of the script ....
Permissions of create_repo.sh, and colors.sh:
-rwxr--r-- 1 hg hg 551 2011-01-07 10:20 colors.sh
-rwxr--r-- 1 hg hg 1137 2011-01-07 11:08 create_repo.sh
Sudoers setup:
%developer ALL = (hg) NOPASSWD: /home/hg/scripts/create_repo.sh
What I'm trying to run:
user#nebu:~$ id
uid=1000(user) gid=1000(user) groups=4(adm),20(dialout),24(cdrom),46(plugdev),105(lpadmin),113(sambashare),116(admin),1000(user),1001(developer)
user#nebu:~$ sudo -l
Matching Defaults entries for user on this host:
env_reset
User user may run the following commands on this host:
(ALL) ALL
(hg) NOPASSWD: /home/hg/scripts/create_repo.sh
user#nebu:~$ sudo -u hg /home/hg/scripts/create_repo.sh
/home/hg/scripts/create_repo.sh: line 3: colors.sh: Permission denied
So the script is executed, but halts when it tries to include the other script.
I have also tried using:
user#nebu:~$ sudo -u hg /bin/bash /home/hg/scripts/create_repo.sh
Which gives the same result.
What is the correct way to include another shell script, if the script may be ran with a different user, through sudo?
What is probably happening is that the script tries to source the file colors.sh in the current directory and fails because it doesn't have permission to read your current directory because of sudo.
Try using source /home/hg/scripts/colors.sh.

Resources