snmpd on Beaglebone/Debian, reading file with source - linux

I have installed snmpd on a Beaglebone Black with Debian and everything works perfect so far, except one thing.
I have configured snmpd.conf for a pass-through
pass .1.3.6.1.4.1.45919 /bin/sh /usr/local/bin/snmp-20
Then snmp-20 is a batch script that looks like this
#!/bin/bash
if [ "$1" = "-n" ]
then
exit 0
fi
. /root/snmp.cfg
#sSerialNumber
if [ "$2" = ".1.3.6.1.4.1.45919.1.120.5" ]
then
echo 1.3.6.1.4.1.45919.1.120.5
echo string
echo $serial
exit 0
fi
In snmp.cfg it looks like this
serial=12345
I feel this is all pretty straight forward. Now when I run /bin/sh /usr/local/bin/snmp-20 or just /usr/local/bin/snmp-20 I get the expected output.
When I do the snmpget -c public -v2c localhost 1.3.6.1.4.1.45919.1.120.5 it returns with "No such instance currently exists ..."
However when I comment the ./root/snmp.cfg the snmpget call is successful, so the calling parameters all work correctly.
It seems the script exits when the source /root/snmp.cfg command is called, but only when called by snmpget, not when called from the prompt.
Any idea would be appreciated.

Related

how to make sure first command finishes and then only execute second command in shell script

how to make sure first command finishes and then only execute second command in shell script
#!/bin/sh
echo "Stopping application"
#command to stop application
echo "Starting application"
#command to start application
In above code, I wanted to make sure that command to stop application is finished properly and then only start the application.
How to handle this.
Please note in my case if application is already stopped then command to stop application takes some random time to complete i.e. 20sec, 30 sec .
So adding sleep is not proper way.
Main moto behind script is to restart application.
Considering fact that if application is allready stopped it doesnt work properly.
If application is running then the script works perfect.
You can use the command return code and a condition to do this.
#!/bin/sh
echo "Stopping application"
#command to stop application
rc=$?
# if the stop command was executed successfuly
if [ $rc == 0 ]; then
echo "Starting application"
#command to start application
else
echo "ERROR - return code: $rc"
fi
There are 'exit codes', try this:
ls
...
echo $?
0
than:
ls non_existing_file
ls: cannot access 'non_existing_file': No such file or directory
echo $?
2
This command echo $? prints exit code of previous command, if it's 0 than it's OK, all non 0 codes means some kind of error which is not OK.

What does ps actually return? (Different value depending on how it is called)

I have a script containing this snippet:
#!/bin/bash
set +e
if [ -O "myprog.pid" ]; then
PID=`/bin/cat myprog.pid`
if /bin/ps -p ${PID}; then
echo "Already running" >> myprog.log
exit 0
else
echo "Old pidfile found" >> myprog.log
fi
else
echo "No pidfile found" >> myprog.log
fi
echo $$ > myprog.pid
This file is called by a watchdog script, callmyprog, which looks like this:
#!/bin/bash
myprog &
It seems to be a problem with if /bin/ps -p ${PID}. The problem manifests itself in this way. If I manually call myprog when it is running I get the message "Already running" as it should. Same thing happens when I manually run the script callmyprog. But when the watchdog runs it, I instead get "Old pidfile found".
I have checked the output from ps and in all cases it finds the process. When I'm calling myprog manually - either directly or through callmyprog, I get the return code 0, but when the watchdog calls it I get the return code 1. I have added debug printouts to the above snippets to print basically everything, but I really cannot see what the problem is. In all cases it looks something like this in the log when the ps command is run from the script:
$ ps -p 1
PID TTY TIME CMD
1 ? 01:06:36 systemd
The only difference is that the return value is different. I checked the exit code with code like this:
/bin/ps -p ${PID}
echo $? >> myprog.log
What could possibly be the cause here? Why does the return code vary depending on how I call the script? I tried to download the source code for ps but it was to complicated for me to understand.
I was able to "solve" the problem with an ugly hack. I piped ps -p $PID | wc -l and checked that the number of lines were at least 2, but that feels like an ugly hack and I really want to understand what the problem is here.
Answer to comment below:
The original script contains absolute paths so it's not a directory problem. There is no alias for ps. which ps yields /bin/ps. The scripts are run as root, so I cannot see how it can be a permission problem.

Function in shell script not executed correctly

I write a script to start/stop/restart a custom server application.
When starting the daemon server it should make the following:
#!/bin/sh -e
### BEGIN INIT INFO
...
...
### END INIT INFO
# Start service
pga_server_start()
{
/opt/pga/server/server -d
}
# Stop service
pga_server_stop()
{
PID=`cat /var/lock/pga_server.lock`
/bin/kill --signal SIGTERM $PID
}
pga_load_excalibur()
{
is_loaded=`lsmod | grep excalbr`
echo "Done"
if [ -z "$is_loaded" ]; then
/usr/local/bin/excload
echo "Driver excalibur loaded."
else
echo "Driver excalibur already loaded."
fi
}
case "$1" in
start)
pga_load_excalibur
pga_server_start
;;
...
...
Initialy it worked fine. Then I've added the pga_load_excalibur function.
Afterward, it does not work anymore.
It never returns from the function pga_load_excalibur.
It seems that the call to is_loaded=lsmod | grep excalbrnever returns as the subsequentecho` is never printed.
However, if I copy/paste this function in a separate shell script...it works.
But if I launch the starter script manually this way:
/etc/init.d/server start
or
service server start
it does not work.
I'm using a Debian Wheezy 7.9 x64.
Although I'm not a schell script, it looks correct. I don't understand why it does not work when it's embedded into this service starter script.
Please note that I've also tried to replace the grep line with:
is_loaded=$(lsmod | grep excalbr)
But it does not work either.
I'm running out of ideas :(
Z.
What do you get if you run the script in debug mode? try to run it with:
#!/bin/sh -xv
That may give some idea of why it's failing, post the output if you can't figure it out

commands in bash script doesn't work properly

I have this script :
#!/bin/bash
./process-list $1
det=$?
echo $det
if [ $det -eq 1 ]
then
echo "!!!"
ssh -n -f 192.0.2.1 "/usr/local/bin/sshfs -r 192.0.2.2:/home/sth/rootcheck_redhat /home/ossl7/r"
rk=$(ssh -n -f 192.0.2.1 'cd /home/s/r/rootcheck-2.4; ./ossec-rootcheck >&2; echo $?' 2>res)
if [ $rk -eq 0 ]
then
echo "not!"
fi
fi
exit;
I ssh to system 192.0.2.1 and run sshfs command on it. actualy I want to mount a directory of system 192.0.2.2 on system 192.0.2.1 and then run a program (which is located in that directory) on system 192.0.2.1. all these ssh and sshfs commands work properly. when I run them manually and output of program ossec-rootcheck is written to file res ,but when I run this script, mount is done but no output is written to file res. I guess program ossec-rootcheck is runned but I don't know why the output isn't written!
this script used to work properly before I don't know what happend suddenly!
As far as I understand the program, the remote machine has stdin>stderr, but how do you get that to the local machine where ssh is being evaluated?
The end ' means on the rk= line, the 2>res happens locally. (and there is no error from ssh, the remote error, if any, is lost when ssh successfully completes.) You could try >res it will get whatever ssh prints out, unfortunately including non-errors.

Adding a service startup script for Amazon linux AMI

I am using an Amazon Linux AMI and doing some custom modifications(added an axis2server, etc) on it and saving it as a new AMI. Now what I want to do is when the AMI boots up, start up axis2server(ie.axis2server should automatically start when the instance boots up). For that I used a init script like below and ran the following command:
chkconfig --add axisservice
But when I launch a new instance from my image, the axis2server is not getting started.
I just only need to execute the script /home/ec2-user/axis2-1.6.1/bin/axis2server.sh at startup. Am I missing anything here?
#! /bin/sh
# Basic support for IRIX style chkconfig
###
# chkconfig: 235 98 55
# description: Manages the services you are controlling with the chkconfig command
###
case "$1" in
start)
echo -n "Starting axisservice"
touch ~/temp.txt
cd /home/ec2-user/axis2-1.6.1/bin
./axis2server.sh &
echo "."
;;
stop)
echo -n "Stopping axisservice"
echo "."
;;
*)
echo "Usage: /sbin/service axisservice {start|stop}"
exit 1
esac
exit 0
I went through https://help.ubuntu.com/community/CloudInit as well and it provides a mechanism called User-Data Scripts, where a user can execute a script when launching the script.
$ euca-run-instances --key mykey --user-data-file myscript.sh ami-axxxx
This is a command line option and what I want is something like when I launch the instance through the UI, the script should be started.Therefore, I think the above option can not be used in my case. Please correct me if I am wrong.
Thanks,
H.
I bet the environment is not set(up correctly). This means that I am guessing that your shell script tries to start another program and it's not to be found.
So at first, I'd adjust the start part of your script (current):
echo -n "Starting axisservice"
touch ~/temp.txt
cd /home/ec2-user/axis2-1.6.1/bin
./axis2server.sh &
echo "."
Edited:
echo -n "Starting axisservice"
touch ~/temp.txt
cd /home/ec2-user/axis2-1.6.1/bin
./axis2server.sh
RETVAL=$?
[ $RETVAL -eq 0 ] && echo Success
[ $RETVAL -ne 0 ] && echo Failure
echo "."
So what did I do?
removed & so script waits for your shell script (axis2server.sh) to complete
checked the return status ($?) of your shell script
Further debugging:
Add set -x to your scripts to enable tracing and log both stderr and stdout.
Questions:
Are you are aware that stop (in your service script) doesn't do anything?
touch ~/temp.txt is that supposed to create /root/temp.txt? (I'm guessing root runs this script.)
If none of my suggestions work, can you share axis2server.sh and paste stderr and stdout?

Resources