cron job unexpected EOF error - cron

I really don't know anything about cron jobs but I am trying to get this to work for a brokerage companies website I am working on. Any help will be greatly appreciated.
Thanks in advance.
I have been given an xml datafeed that I am trying to update my own xml from.
I was given the following xml.sh file
cd `dirname $0`
echo `date` "Importing boats" >> /home/content/91/11071291/html/used-boats/import.log
curl -o /home/content/91/11071291/html/used-boats/horizon.xml "https://I-hid-this-address-incase-ishouldnt-share-on-stackoverflow.com?status=on" >> import.log
SUCCESS=$?
echo $SUCCESS
if [ $SUCCESS -ne 0 ]; then
echo "Data Download failed. Exiting"
exit $SUCCESS
fi
home/content/91/11071291/html/used-boats/import-local-xml.php >> /home/content/91/11071291/html/used-boats/import.log
when running the cron job I always get this error:
/bin/sh: -c: line 0: unexpected EOF while looking for matching `"'
/bin/sh: -c: line 1: syntax error: unexpected end of file

The error message says that a double-quote character (") was not matched.
The quotes all match in what your question shows, so either you've transcribed something incorrectly, or the error lies within the definition of the cron job. Use crontab -e to edit your cron table, and double-check that to make sure all the quotes match.

Related

How to fail a bash script when while/if/etc has errors?

I run a Jenkins pipeline job with Groovy. The Groovy calls bash scripts for each step.
I want to fail the whole job when something in the way has errors.
For Groovy I use the returnStatus: true.
For Bash I use set -e.
But a bash script with set -e, does not exit if, for example, a while statement has errors. This is what should actually happen, according to the Linux manual page for 'set'.
I would like to know how to exit immediately in that scenario.
The script:
[jenkins-user#jenkins ~]$ cat script.sh
#!/bin/bash
set -xe
FILE=commands.txt
echo "echos before while"
# Run the commands in the commands file
while read COMMAND
do
$COMMAND
done < $FILE
echo $?
echo "echos after faulty while"
Let's say 'commands.txt' doesn't exist.
Running script:
[jenkins-user#jenkins ~]$ sh script.sh
echos before while
script.sh: line 13: commands.txt: No such file or directory
1
echos after faulty while
[jenkins-user#jenkins ~]$ echo $?
0
Although the while statement returns exit code 1, the script continues and ends successfully, as checked right after, with echo $?.
This is how I force the Groovy to fail, after a step with bash/python/etc command/script returns a none-zero exit code:
pipeline {
agent any
stages {
stage("A") {
steps {
script {
def rc = sh(script: "sh A.sh", returnStatus: true)
if (rc != 0) {
error "Failed, exiting now..."
}
}
}
}
}
}
First question, how can I make the SHELL script to fail when the while/if/etc statements have errors? I know I can use command || exit 1 but it doesn't seem elegant if I have dozens of statements like this in the script.
Second question, is my Groovy error handling correct? Can anyone suggest an event better way? Or maybe there is a Jenkins plugin/official way to do so?
First question this link may be helpful Aborting a shell script if any command returns a non-zero value
Second question: You can improve your error handling using try and catch for exception handling.
try{
def rc = sh(script: "sh A.sh", returnStatus: true)
if (rc != 0) {
error "Failed, exiting now..."
}
}
catch (Exception er){
errorMessage = er.getMessage();
}
About the Bash script.
Your issue is that the fail redirection does not abort the bash script, despite the use of set -e. I was surprised my-self. But it's my first disappointment about set -e, so now I consider to not trust it and I abuse of stuff like $command || exit 1 ...
Here you can do the following:
set -xe -o pipefail
cat $FILE | while read command; do $command ; done
But the whole loop should be simplified into:
bash $FILE
Why don't you just use the while exit code and return it? (See this modified version of your script, the last lines)
[jenkins-user#jenkins ~]$ cat script.sh
#!/bin/bash
set -xe
FILE=commands.txt
echo "echos before while"
# Run the commands in the commands file
while read COMMAND
do
$COMMAND
done < $FILE
status=$?
echo "echos after faulty while"
exit $status
[jenkins-user#jenkins ~]$ cat script.sh
#!/bin/bash
set -xe
FILE=commands.txt
echo "echos before while"
# Run the commands in the commands file
while read COMMAND
do
$COMMAND
done < $FILE
echo $?
echo "echos after faulty while"
When yor perform a echo $? after this script it will always be 0, because the last command was echo "echos after faulty while" you can ad an exit 1 at the end of your script. In exit 1 the number 1 will be the error code, you can use other. So the script will be
[jenkins-user#jenkins ~]$ cat script.sh
#!/bin/bash
set -xe
FILE=commands.txt
echo "echos before while"
# Run the commands in the commands file
while read COMMAND
do
$COMMAND
done < $FILE
echo $?
exit 1

Unsolved Syntax Error in Bash Function

I'm writing a script for a Raspberry Pi that I had working a few days ago, but now seems to have died once I added some additional code. I've attempted to resuscitate, but I'm coming up short. I am getting "Syntax error near unexpected token `done". I've researched it, but I'm not getting anywhere. dos2unix doesn't help (especially since I've coded the entirety of this on Debian 8.) and cat -v doesn't turn up anything. Offending module is as follows:
24Monitor() {
clear
echo "Beginning speed tests and traceroutes now. You can just... go do whatever it is you do."
while [ $SECONDS -lt $end ]; do
TestNum=$(($TestNum+1))
echo "Speedtest Result #" $TestNum >> ~/speedtest.log
curl -s https://raw.githubusercontent.com/sive/speedtest-cli/master/speedtest.py | python -
cat ~/tmp.log | sed -e "s/^/$(date -R) /" >> ~/speedtest.log
echo "" >> ~/speedtest.log
traceroute mayernetworks.com > ~/traceroutes.log
done
}
I would appreciate any and all insight, as I'm still growing in my scripting knowledge...
EDIT: Variable initilization by request:
end=$((SECONDS+86400))

How to run bash script while it returns code 0?

I have bash script with many lines of code and I need run it while it returns $? == 0, but in case if it has error I need stop it and exit with code 1?
The question is how to do it?
I tried to use set -e command, but Jenkins does not marks build as failed, for him it looks like Success
I also need to get the Error message to show it in my Jenkins log
I managed to get error code(in my case it will be 126), but how to get error message?
main file
fileWithError.sh
rc=$?; if [[ $rc != 0 ]]; then
echo "exit {$rc} ";
fi
fileWithError.sh
#!/bin/sh
set -e
echo "Test"
agjfsjgfshgd
echo "Test2"
echo "Test3"
Just add the command set -e to the beginning of the file
This should look something similar to this
#!/bin/sh
set -e
#...Your code...
I think you just want:
#!/bin/sh
while fileWithError.sh; do
sleep 1;
done
echo fileWithError.sh failed!! >&2
Note that if the script is written well, then the echo is
redundant as fileWithError.sh should have written a decent
error message already. Also, the sleep may not be needed, but is useful to prevent a fast loop if the script succeeds quickly.
You can get the explicit return value, but it requires a bit of refactoring.
#!/bin/sh
true
while test $? = 0; do fileWithError.sh; done
echo fileWithError.sh failed with status $?!! >&2
since the return value of the while script will be the
return value of sleep in the first construction.
Its not quite easy to get an error code only.
How about this ...
#!/bin/bash
Msg=$(fileWithError.sh 2>&1) # redirect all error messages to stdout
if [ "$?" -ne 0 ] # Not Equal
then
echo "$Msg"
exit 1
fi
exit 0
You catch all messages created by fileWithError.sh and if the programm returned an error code then you have the error message already saved in a variable.
But this will make a disadvantage, because you will temporary store all messages created by fileWithError.sh till the error appears.
You can filter the error message with echo "$Msg" |tail -n 1, but its not 100% save.
You should also do some changes in fileWithError.sh...
Switch set -e with trap "exit 1" ERR. this will close the script on errors.
Hope this will help.

Bash - while loop syntax error

I'm new to bash scripting, and atm I'm trying to learn it.
When I run this bash code:
#!/bin/bash
while true
do
./pokecli.py
echo ">pokecli exited... restarting...";
sleep 5;
done;
I receive this error:
opt/PokemonGo-Bot# ./start.sh ./start.sh: line 6: syntax error near unexpected token `done'
./start.sh: line 6: `done;'
Any help is appreciated
You have semicolons where it's not needed. Also, you should start indenting nested commands and get your do on the same line as the while for readability. In addition, "while true" can be dangerous, and a "while sleep" is better.
#!/bin/bash
while sleep 5; do
./pokecli.py
echo ">pokecli exited... restarting..."
done
However, that being said-- none of that will cause an error. Most likely you have either typo'd your do, commented it out, or have (most likely) a Windows (or other special) character prior to the "do", like a ^M or such. The dos2unix command might help, or run this.
tr -cd '[:graph:]\n\t ' <start.sh >file.tmp && mv file.tmp start.sh
You'll have to chmod +x the script again.
The correct syntax is
7.3 While sample
#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ]; do
echo The counter is $COUNTER
let COUNTER=COUNTER+1
done
So you should change to
#!/bin/bash
while true; do
./pokecli.py
echo ">pokecli exited... restarting...";
sleep 5;
done

Shell script for parsing log file

I'm writing a shell script to parse through log file and pull out all instances where sudo succeeded and/or failed. I'm realizing now that this probably would've been easier with shell's equivalent of regex, but I didn't want to take the time to dig around (and now I'm paying the price). Anyway:
sudobool=0
sudoCount=0
for i in `cat /var/log/auth.log`;
do
for word in $i;
do
if $word == "sudo:"
then
echo "sudo found"
sudobool=1;
sudoCount=`expr $sudoCount + 1`;
fi
done
sudobool=0;
done
echo "There were " $sudoCount " attempts to use sudo, " $sudoFailCount " of which failed."
So, my understanding of the code I've written: read auth.log and split it up line by line, which are stored in i. Each word in i is checked to see if it is sudo:, if it is, we flip the bool and increment. Once we've finished parsing the line, reset the bool and move to the next line.
However, judging by my output, the shell is trying to execute the individual words of the log file, typically returning '$word : not found'.
why don't you use grep for this?
grep sudo /var/log/auth.log
if you want a count pipe it to wc -l
grep sudo /var/log/auth.log | wc -l
or still better use -c option to grep, which prints how many lines were found containing sudo
grep -c sudo /var/log/auth.log
or maybe I am missing something simple here?
EDIT: I saw $sudoFailCount after scrolling, do you want to count how many failed attempts were made to use sudo ?? You have not defined any value for $sudoFailCount in your script, so it will print nothing. Also you are missing the test brackets [[ ]] around your if condition checking
Expanding on Sudhi's answer, here's a one-liner:
$ echo "There were $(grep -c ' sudo: ' /var/log/auth.log) attempts to use sudo, $(grep -c ' sudo: .*authentication failure' /var/log/auth.log) of which failed."
There were 17 attempts to use sudo, 1 of which failed.
Your error message arises from a lack of syntax in your if statement: you need to put the condition in [[brackets]]
Using the pattern matching in bash:
#!/bin/bash
sudoCount=0
while read line; do
sudoBool=0
if [[ "$line" = *sudo:* ]]; then
sudoBool=1
(( sudoCount++ ))
# do something with sudobool ?
fi
done < /var/log/auth.log
echo "There were $sudoCount attempts to use sudo."
I'm not initimately familiar with the auth.log -- what is the pattern to determine success or failure?

Resources