Jenkins build trying to run sh file, keep getting permissions denied
Check the permissions on the sh file - it needs to be executable by the user that runs your Jenkins build.
Unfortunately I'm not very familiar with Windows, but I know that recent versions include a Linux shell so here is how to do it on Linux:
ls -l /path/to/example.sh
will show you something like this:
-rwxr-xr-x 1 myuser my group 519 Mar 7 14:54 example.sh
the first column in the output is the file permissions, and you're looking for an 'x' in at least the first position, which means it's executable by the owner (the value in the third column).
If you need to make the file executable, run
chmod +x /path/to/example.sh
which will make it executable by any user. This isn't always a good idea but the full subject of file permissions is a bit much to go into here; here's a good reference if you want to learn more.
Why does a bash script require an execute bit if a windows batch script can just be executed?
To run a script you have two options in unix like systems. First Option is to use a direct interpreter call with the script as parameter.
# run a bash script
bash test.sh
# run a python scripts
python test.py
The second option is mark your file as executable, with the execute bit and after a call like this ...
# sample bash
./test.sh
# sample python
./test.py
... your system tries to find the right interpreter for you. For this the first line 'shebang' of the script is used.
Bash example:
#!/bin/bash
# points to the installed bash interpreter - bash example
Python example:
#!/usr/bin/python
# points to the installed python interpreter
To your question windows only use the file extension to detect a executable file.
How to create file execute mode permissions in Git on Windows?
There's no need to do this in two commits, you can add the file and mark it executable in a single commit:
C:\Temp\TestRepo>touch foo.sh
C:\Temp\TestRepo>git add foo.sh
C:\Temp\TestRepo>git ls-files --stage
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0 foo.sh
As you note, after adding, the mode is 0644 (ie, not executable). However, we can mark it as executable before committing:
C:\Temp\TestRepo>git update-index --chmod=+x foo.sh
C:\Temp\TestRepo>git ls-files --stage
100755 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0 foo.sh
And now the file is mode 0755 (executable).
C:\Temp\TestRepo>git commit -m"Executable!"
[master (root-commit) 1f7a57a] Executable!
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100755 foo.sh
And now we have a single commit with a single executable file.
How do I run .sh or .bat files from Terminal?
The .sh
is for *nix systems and .bat
should be for Windows. Since your example shows a bash error and you mention Terminal, I'm assuming it's OS X you're using.
In this case you should go to the folder and type:
./startup.sh
./
just means that you should call the script located in the current directory. (Alternatively, just type the full path of the startup.sh
). If it doesn't work then, check if startup.sh
has execute permissions.
getting permission to execute a bash script
File Permissions
First, make sure that you have the correct file permissions:
chmod +x /var/www/script_name #Gives the current user execute permissions
Executing Your Bash Script
In order to execute your bash
script, the easiest option is to just simply call it (without any additional commands) by typing in the relative path to the script:
/var/www/script_name
There are other options for explicitly executing your script from the shell (in your case, use the bash
shell to execute your script as a bash
script). From TLDP documentation...
A script can also explicitly be executed by a given shell, but generally we only do this if we want to obtain special behavior, such as checking if the script works with another shell or printing traces for debugging:
rbash script_name.sh # Execute using the restricted bash shell
sh script_name.sh # Execute using the sh shell
bash -x script_name.sh # Execute using the bash shell
A Note on File Extensions: "Shebang" line > File extension
It is not an advised practice to use file extensions with your scripts, especially if you think your code may evolve beyond its current functionality.
Just in case you were wondering if the file extension may be your problem... it is not. It is important that you know that the file extension of a script isn't necessary at all. What matter is what you put in the "shebang" line:
To use the sh
shell:
#!/bin/sh
To use the bash
shell:
#!/bin/bash
It won't matter what file extension you use - the "shebang" line indicates what shell will be used to execute the script. You could save a script with the "shebang" of #!/bin/bash
as script_name.py
, but it would remain a bash
script. If you attempt to execute it, ./script_name.py
, it would be executed as a bash
script.
As @Arjan mentioned in the comments, using file extensions for your script could lead to unnecessary complications if you decide to change the implementation of your project (i.e., a different shell / language):
I could decide later to shift my project to
sh
,python
,perl
,C
, etc. Perhaps because I want to add functionality. Perhaps because I want to make it portable to a system withoutbash
. It would be much more difficult if I used the.sh
file extension, since then I'd need to change all my references to the script just because I changed its implementation.
Execute a shell script in current shell with sudo permission
What you are trying to do is impossible; your current shell is running under your regular user ID (i.e. without root the access sudo
would give you), and there is no way to grant it root access. What sudo
does is create a new *sub*process that runs as root. The subprocess could be just a regular program (e.g. sudo cp ...
runs the cp
program in a root process) or it could be a root subshell, but it cannot be the current shell.
(It's actually even more impossible than that, because the sudo
command itself is executed as a subprocess of the current shell -- meaning that in a sense it's already too late for it to do anything in the "current shell", because that's not where it executes.)
How to run .sh on Windows Command Prompt?
The error message indicates that you have not installed bash
, or it is not in your PATH
.
The top Google hit is http://win-bash.sourceforge.net/ but you also need to understand that most Bash scripts expect a Unix-like environment; so just installing Bash is probably unlikely to allow you to run a script you found on the net, unless it was specifically designed for this particular usage scenario. The usual solution to that is https://www.cygwin.com/ but there are many possible alternatives, depending on what exactly it is that you want to accomplish.
If Windows is not central to your usage scenario, installing a free OS (perhaps virtualized) might be the simplest way forward.
The second error message is due to the fact that Windows nominally accepts forward slash as a directory separator, but in this context, it is being interpreted as a switch separator. In other words, Windows parses your command line as app /build /build.sh
(or, to paraphrase with Unix option conventions, app --build --build.sh
). You could try app\build\build.sh
but it is unlikely to work, because of the circumstances outlined above.
Permission denied for build.sh file
Looks like you need to check in the file build.sh
with execution permissions. Please try the following from your own machine:
git update-index --add --chmod=+x build.sh
git commit -m 'Make build.sh executable'
git push
Related Topics
Customize Tab Completion in Shell
How to Execute The Command 'Appcfg Rollback'
How Do Shared Libraries Work in a Mixed 64Bit/32Bit System
Understanding Linux Directory Permissions Reasoning
How to Answer Yes in Bash Script
Execute Bash Script Remotely via Curl
Jenkins/Hudson Ci Minimum Requirements for a Linux Rh Installation
The Sort -R Command Doesn't Sort Lines Randomly in Linux
Which Characters Are Allowed in a Bash Alias
How Delete File from Fortran Code
Using a Chef Recipe to Append Multiple Lines to a Config File
How to Grep for The Exact Word If The String Has Got Dot in It
What Would Be a Pratical Example of Sysroot and Prefix Options for Qt
Bash: Send Sigtstp Signal (Ctrl+Z)
Capture Nethogs Output in Log File
How to Monitor Cwnd and Ssthresh Values for a Tcp Connection
What Is The Fastest Way to Display an Image in Qt on X11 Without Opengl