Bash Script is returning non-zero status - linux

I'm trying to install beanstalkd on an Ubuntu 14.04 LTS system. Seems to go okay, except for the following is giving me non-zero return status. Vagrant halts once this occurs and I have other scripts to install after wards that don't run.
I've tried
grep -q "START=yes"
grep --quiet "START=yes"
grep "START=yes" > /dev/null
non seem to suppress the output of GREP
echo -n "Checking /etc/default/beanstalkd for beanstalkd startup line ... "
if [ -f /etc/default/beanstalkd ]; then
echo -n "START=yes is"
grep "START=yes" /etc/default/beanstalkd > /dev/null
if [ $? = 0 ]; then
echo -n "..already present"
else
echo -n "START=yes" >> /etc/default/beanstalkd
echo -n "..Added to /etc/default/beanstalkd"
fi
fi
echo "Done!"
Result :
==> default: Checking /etc/default/beanstalkd for beanstalkd startup line ...
==> default: START=yes is
The SSH command responded with a non-zero exit status. Vagrant
assumes that this means the command failed. The output for this command
should be in the log above. Please read the output to determine what
went wrong.

The following invocation of grep will suppress all grep output (both stderr and stdout). No warnings, no errors, nothing.
grep -q "START=yes" /etc/default/beanstalkd >/dev/null 2>&1
If your query was about the if [ $? = 0 ]; then line not working as expected, you can replace it with this:
if grep -q "START=yes" /etc/default/beanstalkd >/dev/null 2>&1; then
# already present

Related

How to prperly launch mjpg_streamer when a webcam is pluged in?

I am trying to launch mjpg streamer when a webcam is plugged in.
So far I've been able to detect when the webcam is pluged and I've been able to launch it.
But two issues appear:
first the port doesn't appear to be open
second the process is killed after about 3 minutes
Im running on Ubuntu 18.04.4 LTS
here is what I assume is the culprit file
/etc/udev/rules.d/10-usbmount.rules
#https://stackoverflow.com/questions/20084740/udev-run-program-on-usb-flash-drive-insert
KERNEL=="video[0-9]*", ENV{ID_SERIAL}!="?*", SUBSYSTEMS=="usb", RUN+="/usr/bin/usbdevinserted"
/usr/bin/usbdevinserted
#!/bin/bash
echo $DEVNAME 2>&1 > /tmp/usbdevinfo
#set 2>&1 >> /tmp/usbdevinfo
if [[ $(pgrep mjpg_streamer) ]];
then
echo "Stoping Mjpg_Streamer" 2>&1 >> /tmp/usbdevinfo;
pkill mjpg_streamer;
echo "mjpg_Streamer stoped" 2>&1 >> /tmp/usbdevinfo;
fi
if [[ $(v4l2-ctl --device=$DEVNAME --all | grep "Video input") ]];
then
echo "launching mjpg_streamer on $DEVNAME" 2>&1 >> /tmp/usbdevinfo;
(mjpg_streamer -i "input_uvc.so -f 15 -r 1080x720 -d $DEVNAME" -o "output_http.so -w /pathToHome/mjpg-streamer/www -p 8080" 2>&1 >> /tmp/usbdevinfo) | at now
fi

Getting exit code on the remote server after jump through a bastion

I would like to get the exit code of my remote command.
It's normally pretty easy, but I need to execute my command through a bastion host and I don't know take that with a easy methode.
So, this is a diagram of my architecture.
PC => Bastion => remote host
I used this code to execute my command and check the return code:
ssh -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -At ubuntu#$ip_bastion ssh -Aq -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ubuntu#$ip_server bash <<EOF
{ ${cmd} ; } 2>&1 | sed -e 's/^/$(colors_next exec)$region:$ip:$type${normal} /'
EOF
echo $?
if [[ $? -ne 0 ]]; then
error "Error during the command execution."
exit 1
fi
But, the return code is always equal 0, while my command failed (Return 255 on the final server).
Thanks for your help :)
If you are using bash, you can use the PIPESTATUS array variable to get the exit status of each element of the pipeline.
ssh -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -At ubuntu#$ip_bastion ssh -Aq -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ubuntu#$ip_server bash <<EOF
{ ${cmd} ; } 2>&1 | sed -e 's/^/$(colors_next exec)$region:$ip:$type${normal} /'
exit "\${PIPESTATUS[0]}"
EOF
remote_exitcode=$?
echo "remote_exitcode=${remote_exitcode}"
if [[ ${remote_exitcode} -ne 0 ]]; then
error "Error during the command execution."
exit 1
fi
The echo command is hurting you. You need to capture the value of $? before echoing it, because echo will reset it to 0, assuming that the echo command is successful.
ssh -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -At ubuntu#$ip_bastion ssh -Aq -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ubuntu#$ip_server bash <<EOF
{ ${cmd} ; } 2>&1 | sed -e 's/^/$(colors_next exec)$region:$ip:$type${normal} /'
EOF
rc=$?
echo $rc
if [[ $rc -ne 0 ]]; then
error "Error during the command execution."
exit 1
fi
Optionally, you may want to exit $rc so that your script exits with the same return code that your SSH command exited with, which might in some cases be helpful in figuring out what went wrong.

Check whether a process is running or not Linux

Here is my code:
#!/bin/bash
ps cax | grep testing > /dev/null
while [ 1 ]
do
if [ $? -eq 0 ]; then
echo "Process is running."
sleep 10
else
nohup ./testing.sh &
sleep 10
fi
done
I run it as nohup ./script.sh &
and it said nohup: failed to run command './script.sh': No such file or directory
What is wrong?
The file script.sh simply does not exist in the directory that you are issuing the command from.
If it did exist and was not executable you would get:
`nohup: failed to run command ‘./script.sh’: Permission denied
For each newly created scripts on Linux, you should first change the permission as you can see the permission details by using
ls -lah
The following content may help you:
#!/bin/bash
while [ 1 ];
do
date=`date`
pid=`ps -ef | grep "your process" | grep -v grep | awk -F' ' '{print $2}'`
if [[ -n $pid ]]; then
echo "$date - processID $pid is running."
else
echo "$date - the process is not running"
# script to restart your process
say: start the process
fi
sleep 5m
done
Make sure your script is saved as script.sh
and your executing nohup ./script.sh & from the same directory in which script.sh.
Also you can give executable permission for script.sh by
chmod 776 script.sh
or
nohup ./script.sh &
Run as
nohup sh ./script.sh &

get return value of command run with script -c

Is there a way to capture the return value of a program run using script -c?
For example (in bash)
/bin/false; echo $? # outputs 1
/usr/bin/script -c "/bin/false" /dev/null; echo $?
# outputs 0 as script exited successfully.
I need to get the return value from /bin/false instead of from /usr/bin/script.. is this possible? I'm using script to trick a program into thinking it is running in a real tty even though it isn't... Thanks!
According to man script, using -e option will return the exit code of the child process.
-e, --return
Return the exit code of the child process.
Uses the same format as bash termination
on signal termination exit code is 128+n.
Here's some example.
$ /usr/bin/script -e -c "/bin/false" /dev/null; echo $?
1
$ /usr/bin/script -e -c "/bin/true" /dev/null; echo $?
0
$ /usr/bin/script -e -c "exit 123" /dev/null; echo $?
123

Why linux redirect loss info?

I write a script like this:
#!/bin/bash
LOG_PATH=/root/cngiqos-log
LOG_NAME=term.log
TERM_PATH=/home/bnrcqos/qos_M11/term
test -d $LOG_PATH || mkdir -p $LOG_PATH
routeID='M11'
if [ `ps -ef | grep 'term$' | grep -v grep | wc -l` -gt 0 ]; then
echo $routeID' term process is already running'
else
cd $TERM_PATH
(nohup ./term > $LOG_PATH/$LOG_NAME 2>&1 &)
fi
And I input "tail -f /root/cngiqos-log/term.log" and see the log, the log loss info, the log only output part of a log and then don't output any more. But when I input "./term" and run it in fg, the output is fine.
Does any body know why? Is it a system bug?
Maybe you just get what you asked for? tail just gives the last 10 lines by default.

Resources