I am trying to create a simple bash script.
(just started working on bash scripting)
the script is simple. There is an issue with rsyslog service and from time to time dies.
I am trying to make a script to check if service is dead to restart it
till now I want to check if my conditions are ok but it seems I am getting an error. Can you advice me ?
Here is the script:
#!/bin/bash
a="dead"
b="running"
while i in $(/etc/init.d/rsyslog status | grep -o 'running\|dead');
do
if
[ "$a" == "$i" ];
then
echo "service rsyslog is dead "
if
[ "$b" == "$i" ];
then
echo "service rsyslog is running"
else
echo "nothing to do";
fi;
done
-------------
I am getting the following syntax error.
./rsyslogcheck.sh: line 17: syntax error near unexpected token done'
./rsyslogcheck.sh: line 17:done'
Thank you in advance!!
There are several problems here:
Invalid while loop syntax
Unnecessary loop: it seems you don't need a loop at all
Missing closing fi of an if that was opened
I suppose you're looking for something like this:
#!/bin/bash
status=$(/etc/init.d/rsyslog status | grep -o 'running\|dead')
case "$status" in
dead) echo "service rsyslog is dead";;
running) echo "service rsyslog is running";;
*) echo "nothing to do";;
esac
Related
I'm trying to write a shell script for Ubuntu, which checks the Teamspeak-server status and reacts to them.
This is my actual version:
a=$(sh /home/teamspeak3/ts3/teamspeak3-server_linux_amd64/ts3server_startscript.sh status)
echo "$a"
if [ "$a" -ne "Server is running"]
then
echo "..."
fi
exit 0
This is my actual output (and problem):
user1234#euve252903:~$ ./keepAlive.sh
Server is running
./keepAlive.sh: line 8: syntax error: unexpected end of file
(The text "Server is running" is the output from echo "$a").
I don't get the reason, why the syntax error appears...
I checked the file for MSDOS-ending signs.
Any ideas out there?
You're missing a space before the last bracket in your if statement
#!/bin/bash
a=$(sh /home/teamspeak3/ts3/teamspeak3-
server_linux_amd64/ts3server_startscript.sh status)
echo "$a"
if [ "$a" -ne "Server is running" ]; then
echo "..."
fi
exit 0
Also, I'd recommend adding a shebang and lastly, running the script in debug mode (bash -x) to find out what's wrong. If neither one of those work for you, try Shell Check, at least for syntax errors that is.
I am writing a bash script that goes through a list of filenames and attempts to copy each file using scp from two servers into a local folder. The script then compares the local files to each other. Sometimes however, the file will not exist on one server or the other or both.
At first, I was using this code:
scp $user#$host:/etc/$file ./$host/conf/ 2>/tmp/Error 1>/dev/null
error=$(</tmp/Error) # error catching
if [[ -n "$error" ]]; then echo -e "$file not found on $host"; fi
But I found that some (corporate) servers output a (legalese) message (to stderr I guess) every time a user connects via scp or ssh. So I started looking into utilizing exit codes.
I could simply use
scp $user#$host:/etc/$file ./$host/conf/ 2>/tmp/Error 1>/dev/null
if [[ $? -ne 0 ]]; then echo -e "$file not found on $host"; fi
but since the exit code for "file does not exist" is supposed to be 6, I would rather have a more precise
scp $user#$host:/etc/$file ./$host/conf/ 2>/tmp/Error 1>/dev/null
if [[ $? -eq 6 ]]; then echo -e "$file not found on $host"; fi
The problem is that I seem to be getting an exit code of 1 no matter what went wrong. This question is similar to this one, but that answer does not help me in Bash.
Another solution I am considering is
scp $user#$host:/etc/$file ./$host/conf/ 2>/tmp/Error 1>/dev/null
error=$(</tmp/Error) # error catching
if [[ ${error: -25} = "No such file or directory" ]]; then echo -e "$file not found on $host"; fi
But I am concerned that different versions of scp could have different error messages for the same error.
Is there a way to get the actual exit code of scp in a Bash script?
Per the comments (#gniourf_gniourf, #shelter, #Wintermute) I decided to simply switch tools to rsync. Thankfully the syntax doesn't need to be changed at all.
23 was the error code I was getting when files didn't exist so here is the code I ended up with
rsync -q $user#$host:/etc/$file ./$host/conf/ 2>/tmp/Error 1>/dev/null
if [[ $? -eq 23 ]]; then echo -e "$file not found on $host"; continue; fi
I'm seeing 1 for "file not found" not found, you can do testing for these sorts of things against localhost, if you need to differentiate different errors capture stdout instead.
if $err=`scp $host:$file 2>&1`
then
echo "copied successfully
else
case "$err" in
*"file not found"* )
echo "$file Not Found on $host"
;;
*"Could not resolve hostname"* )
echo "Host not found: $host"
;;
"Permission denied "* )
echo "perm-denied! $host"
;;
* )
echo "other scp error $err"
;;
esac
this isn't going to work if you have a different locale with different messages.
So i am writing a script to call a process 365 times and they should run in 10 batches, so this is something i wrote but there are multiple issues -
1. the log message is not getting written to the log file, i see the error message in err file
2. there is this "Command not found" error I keep getting from the script for the line process.
3. even if the command doesnt succeed, still it doesn't print FAIL but prints success
#!/bin/bash
set -m
FAIL=0
for i in {1..10}
do
waitPIDS=()
j=$i
while [ $j -lt 366 ]; do
exec 1>logfile
exec 2>errorfile
`process $j &`
waitPIDS[${#waitPIDS[#]}]=$!
j=$[$j+1]
done
for jpid in "${waitPIDS[#]}"
do
echo $jpid
wait $jpid
if [[ $? != 0 ]] ; then
echo "fail"
else
echo "success"
fi
done
done
What is wrong with it ?
thanks!
At the very least, this line:
`process $j &`
Shouldn't have any backticks in it. You probably just want:
process $j &
Besides that, you're overwriting your log files instead of appending to them; is that intended?
I have the following shell script :
#!/bin/sh
output=`./process_test.sh status_pid | grep "NOT STARTED: process_1" --line-buffered`
if[[ -z ${output} ]]
then
echo "process is not running"
else
echo "process is running"
fi
where ./process_test.sh status_pid is my utility for finding whether a process is running or not .e.g. if process_1 is not running it will give: NOT STARTED: process_1. Further
this utility is perfect and does not have any issue. I suspect the issue is with if syntax
on running this script I get the following output:
./test.sh: line 18: if[[ -z NOT: command not found
./test.sh: line 19: syntax error near unexpected token `then'
./test.sh: line 19: `then'
Can you help to resolve this issue?
You must use spaces to separate keywords such as if from the arguments or commands such as [[.
#!/bin/sh
output=$(./process_test.sh status_pid | grep -e "NOT STARTED: process_1" --line-buffered)
if [[ -z ${output} ]]
then
echo "process is not running"
else
echo "process is running"
fi
You should write it like
if [[ -z ${output} ]]
then
...
So you had missed a .
It would be a lot cleaner to write this:
#!/bin/sh
if ! ./process_test.sh status_pid |
grep "NOT STARTED: process_1" > /dev/null; then
echo "process is not running"
else
echo "process is running"
fi
Note that the --line-buffering argument is irrelevant, since
the pipe is not going to finish until after all of the input
is read. (Well, it's not totally irrelevant--it will make the
script run negligibly slower.)
Also note that '[[' is not standard. According to the shell language specification, it
"may be recognized as ( a ) reserved (word) on some implementations ..., causing unspecified results". In other words, it is what is commonly known as a "bashism" (although it is valid in shells other than bash), and if you use it you must not use #!/bin/sh as your interpreter, but should specify #!/bin/bash.
I've written a script for me to start and stop my Perforce server. To shutdown the server I use the kill -SIGTERM command with the PID of the server daemon. It works as it should but there are some discrepancies in my script concerning the output behavior.
The script looks as follows:
#!/bin/sh -e
export P4JOURNAL=/var/log/perforce/journal
export P4LOG=/var/log/perforce/p4err
export P4ROOT=/var/local/perforce_depot
export P4PORT=1666
PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"
. /lib/lsb/init-functions
p4start="p4d -d"
p4stop="p4 admin stop"
p4user=perforce
case "$1" in
start)
log_action_begin_msg "Starting Perforce Server"
daemon -u $p4user -- $p4start;
echo "\n"
;;
stop)
echo "BLABLA"
echo "$(pidof /usr/local/bin/p4d)"
#daemon -u $p4user -- $p4stop;
p4dPid="$(pidof /usr/local/bin/p4d)"
echo $p4dPid
if [ -z "$(pidof /usr/local/bin/p4d)" ]; then
echo "ERROR: No Perforce Server running!"
else
echo "SUCCESS: Found Perforce Server running!\n\t"
echo "Shutting down Perforce Server..."
kill -15 $p4dPid;
fi
echo "\n"
;;
restart)
stop
start
;;
*)
echo "Usage: /etc/init.d/perforce (start|stop|restart)"
exit 1
;;
esac
exit 0
When p4d is running the stop block works as intended, but when there is no p4d running the script with stop only outputs BLABLA and an empty new line because of the echo "$(pidof /usr/local/bin/p4d)". The error message stating that no server is running is never printed. What am I doing wrong here?
PS: The part if [ -z "$(pidof /usr/local/bin/p4d)" ]; then has been changed from if [ -z "$p4dPid" ]; then for debug reasons.
EDIT: I narrowed down the problem. If I don't use the p4dPid variable and comment out the lines p4dPid="$(pidof /usr/local/bin/p4d)" and echo $p4dPid the if block is processed and the error messages is printed. Still I don't unterstand what is causing this behavior.
EDIT 2: Problem solved!
The -e in #!/bin/sh -e was causing the shell to exit the script after any statement returning a non-zero return value.
When your service is not running, the command
echo "$(pidof /usr/local/bin/p4d)"
is processed as
echo ""
because pidof did not return any string. So the command outputs an empty line.
If you do not want this empty line, then just remove this statement, after all you print an error message when the process is not running.
Problem solved!
The -e in #!/bin/sh -e was causing the shell to exit after any statement returning a non-zero return value.