Docker image openjdk:8-jdk-alpine fails to execute a simple command - java

I created a docker image from openjdk:8-jdk-alpine using the below Dockerfile:
But when I try to execute simple commands I get the following errors:
/bin/sh: ./run.sh: not found
my run.sh looks like this
enter image description here
I try to "docker run -it [images] bash" enter to the interactive environment,I can see the file "run.sh".In the directory /bin bash exist,but I execute run.sh also display " /bin/sh: ./run.sh: not found"
PS:Sorry for my poor english,I am a chinese student

The printed content of the run.sh, indicates that my original assessment was incorrect; however based on the error message, and the image of the run.sh file, I have a lead.
Your run.sh script has an exec line of #!/bin/sh, which means that it does not need bash to operate so my previous assessment was incorrect.
Starting on a mac, I created a run.sh script, duplicated the dockerfile (mostly), and it ran correctly, producing a valid run.
I then converted the run.sh to use dos line endings and got the following:
$ file run.sh
run.sh: POSIX shell script text executable, ASCII text, with CRLF line terminators
$ docker run --rm -it bob
/bin/sh: ./run.sh: not found
Which looks suspiciously like your error message.
From this, it would lead me to believe that your run.sh file contains dos line endings. Based on the images, I'm guessing that you're on windows, which is where the problem with the run.sh script originates.
how to convert the line endings (some examples):
dos2unix run.sh
perl -pi -e 's/\r\n/\n/g' run.sh
Previous Answer
The most likely reason for this issue is that the shebang line in the run.sh contains: #!/usr/bin/bash, or something of that ilk - i.e. it doesn't reference the valid path to the binary that will run the shell script.
On alpine, bash is installed into /bin, so if you try to run the script you will see the error:
/ # ./run.sh
/bin/sh: ./run.sh: not found
/ # cat run.sh
#!/usr/bin/bash
echo "Hi"
workaround (1): after the apk add bash, do an:
RUN ln -s /bin/bash /usr/bin
in your Dockerfile. This will create a symlink for bash, allowing the program to run:
/ # ln -s /bin/bash /usr/bin
/ # ./run.sh
Hi
workaround(2) - if you don't want to make a symlink like this, you can always invoke bash as part of the CMD -
CMD [ 'bash', './run.sh' ]

Related

Error executing ant :-bash: ant: command not found

I am trying to deploy my application in a Linux box, I have a file called setAppPath.sh file as:
#!/bin/sh
APP_HOME=`pwd`
ANT_HOME=$APP_HOME/lib/ant
echo $ANT_HOME
PATH=$ANT_HOME/bin:$APP_HOME/scripts/unix:$PATH
echo $PATH
chmod +x $ANT_HOME/bin/ant
chmod +x $APP_HOME/scripts/unix/*.sh
export APP_HOME ANT_HOME PATH
When I try to execute ant command I get an error message as:
-bash: ant: command not found
The echo $ANT_HOME is printing my ant home location the PATH is printed properly too.
After execting setAppPath.sh file I tried echo $ANT_HOME it gave empty line.
Please help me figuring out this issue.
Edit 1: which ant give no ant
I am using sh setAppPath.sh command to execute the sh file.
When you run your script normally, what happens is that your shell starts a new process, the script runs in that process, and when the script is done the process dies and control returns to your shell.
All modifications that the script did to its environment die with it. The changes have no effect on the parent shell. Same if you're trying to run cd in a script and expecting the parent shell to move.
To run your script in the context of your shell and not in a subprocess, use the source or . commands:
source setAppPath.sh
. setAppPath.sh

Jenkins slave process on ubuntu not showing any logging

I have an Ubuntu VM dedicated as a Jenkins slave. I wrote a one-liner script to run the slave jar, and I run that script from /etc/rc.local. When I run the script manually, I get a few lines of output showing that it's working. I've tried to define the rc.local line and the script so that it stores stdout/stderr in a file, but the file is always zero length, with a modtime of when I start the process.
In the following, some fields are elided with "=stuff=".
The end of my "/etc/rc.local" looks like this:
su -c "/home/=user=/bin/jenkinsconnect" =user=
exit 0
The "jenkinsconnect" script looks like this:
#! /bin/bash
java -jar /home/=user=/opnfv_slave_root/slave.jar -jnlpUrl https://=host=/ci/computer/att-build/slave-agent.jnlp -secret =secret= 2>&1 > /home/=user=/jc.log
As I said, "/home/=user=/jc.log" is always zero length, and the modtime is when I started the process.
The 2>&1 > file syntax will not work. You should either:
use &> for directing both stderr and stdout to a file, or
surround the entire command with parentheses to capture the output:
(java -jar /home/=user=/opnfv_slave_root/slave.jar ... 2>&1) > /home/=user=/jc.log

Unix script on startup in /etc/init.d not working

I've been trying to get my Java application to run as a daemon in the background after startup. I've followed the instructions given in the top answer here and to no avail.
This is my /etc/init.d/myapp file:
#!/bin/bash
# MyApp
#
# description: bla bla
case $1 in
start)
/bin/bash /var/lib/myapp/start.sh
;;
stop)
/bin/bash /var/lib/myapp/stop.sh
;;
restart)
/bin/bash /var/lib/myapp/stop.sh
/bin/bash /var/lib/myapp/start.sh
;;
esac
exit 0
as for the /var/lib/myapp/start.sh, it looks like this:
#!/bin/bash
java -jar myapp-1.0.0RC.jar &
and works fine when run from a terminal via ssh.
i also ran the update-rc.d myscript defaults command, and was only given a warning about headers and LSB
After this, once i reboot the server, the app isnt running. Any help is appreciated.
Thanks.
When bash scripts are run, they are not automatically ran from the same directory that contains them.
You will either need to update your scripts to change directory to that which holds the scripts before starting the jar:
#!/bin/bash
cd /var/lib/myapp/
java -jar myapp-1.0.0RC.jar &
Or, refer to the jar file with a full path:
#!/bin/bash
java -jar /var/lib/myapp/myapp-1.0.0RC.jar &
Check if your service is registered properly via chkconfig
$ chkconfig --list
If not you can see your service listed on the output, then try adding this lines to your script
#!/bin/bash
# chkconfig: 2345 95 20
# description: bla bla
# processname: myapp
and then run
chkconfig --add myapp
For more information you can check the man page for chkconfig

How to run a script in user mode inside an executable jar that is executed as root?

I want to run the following script within a Java executable jar on the Raspberry Pi.
the script (= stream.sh):
#!/bin/sh
raspivid -fps 25 -w 640 -h 360 -vf -n -o - -t 999999 |cvlc -vvv stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8554/cam.sdp,rtcp-mux}' :demux=h264
the Java code:
Runtime.getRuntime().exec("sh stream.sh"));
The problem is that the jar must be run with sudo and the vlc command doesn't accept sudo. Neither the script or the Java code contain sudo but as the jar is executed as sudo, vlc still gives the error "VLC is not supposed to be run as root...".
What is the easiest way to make the script run in user mode inside the jar?
I would use su -l $LOGIN -c $CMD or sudo -u $LOGIN $CMD.
Runtime.getRuntime().exec("sudo -u myuser sh stream.sh"));
man sudoers has all the information you need.
You need to change /etc/sudoers

Command works in terminal but not in script?

I have the following script which won't work when executed as a script, but does work when the exact same commands are entered into the terminal:
#! /bin/sh
cd ~/Desktop/Example/
javac Generator.java
The error message is:
my_script.sh 3: my_script.sh: javac: not found
The above script is named my_script.sh and I execute it from the terminal using:
sh my_script.sh
when I do
echo $SHELL
in the terminal I get:
/bin/bash
Add jmlc to your path and run the script again.
To check: Open a new shell and type 'jmc'.
Another way to get your script working is to specify the full path in your script. Replace 'jmlc' with '/full_path_here/jmlc'.
Also make sure that any other commands in jmlc script are also available in the path.
You can also made jmlc available by exporting its PATH:
#! /bin/sh
export jmlc_bin=FULL_PATH_TO_JMLC
cd ~/Desktop/Example/
$jmlc_bin Generator.java
Navigate to the directory where your single line commands were working and save your script in that directory.
then execute
./my_script.sh

Categories