i can't run bash script in other script - bash

This is my script in bash which I added to user crontab, and the command bash /home/lobby/start.sh doesn't work. Can someone help me with this?
#!/bin/bash
if ps aux | grep lobby | grep "Ss" | grep "SCREEN"; then
pkill -f lobby
screen -wipe
if bash /home/lobby/start.sh; then
echo "$(date '+%y-%m-%d') Serwer dzialal Uruchomiono pomyslnie" >> /home/logi-serwery/logi-autorestart-lobby.txt
bash /home/lobby/start.sh;
else
echo "$(date '+%y-%m-%d') Serwer dzialal blad podczas uruchamiania" >> /home/logi-serwery/logi-autorestart-lobby.txt
fi
else
if bash /home/lobby/start.sh; then
echo "$(date '+%y-%m-%d') Serwer nie dzialal uruchomilem" >> /home/logi-serwery/logi-autorestart-lobby.txt
bash /home/lobby/start.sh;
else
echo "$(date '+%y-%m-%d') Serwer nie dzialal blad podczas uruchomienia" >> /home/logi-serwery/logi-autorestart-lobby.txt
fi
fi

Related

Append bash timer to file

I am struggling to both provide a decent timer to a user and append the value in the log. I'm not sure why the following loop isn't logging using exec.
My ideal situation is to add >> log.txt to the echo line, but that breaks the visual timer to the user.
One workaround is to write a redundant echo line after the first:
Some progress was made
exec > >(tee log.txt) 2>&1
SECONDS=0
date1=`date +%s`
while ! [ $((`date +%s` - $date1)) -gt 10 ]; do
echo -ne "$(date -u --date #$((`date +%s` - $date1)) +%H:%M:%S)\r"
#Redundant echo
echo "$(date -u --date #$((`date +%s` - $date1)) +%H:%M:%S)" >> log.txt
done
You could pipe the output to tee -a which will send it to standard output and append to log.txt:
echo "$(date -u --date #$((`date +%s` - $date1)) +%H:%M:%S)" | tee -a log.txt

bash check if string exists in output

I have the following bash script:
#!/bin/bash
KEY=$(./get-aws-profile.sh --profile=$1 --key)
SECRET=$(./get-aws-profile.sh --profile=$1 --secret)
ES="https://search-****.eu-west-1.es.amazonaws.com"
INDEXES=$(AWS_ACCESS_KEY_ID="$KEY" AWS_SECRET_ACCESS_KEY="$SECRET" aws-es-curl $ES/_cat/indices | awk '{print $3}')
YESTERDAY=$(date --date="yesterday" +"%Y.%m.%d")
for i in $INDEXES
do
# $i is like report-processing-2019.10.10
if grep "$YESTERDAY" $i; then
echo "Pulling documents from $i to $i.ndjson"
echo "docker run -it blaze blaze --host=$ES --index=$i --insecure > /tmp/$i.ndjson"
echo "Index $i saved"
else
echo no
fi
done
exit 0
where $i is like report-processing-2019.10.10
when i run this, i am not able to get the the lines which contain only lines with 2019.10.10 in them, what am i missing?
any advice is much appreciated

Bash Script can run php script manually but cannot work in Cron

I have a bash script like this:
#!/bin/bash
log_file=/home/michael/bash/test.log
checkalive=checkalive.php
#declare
needRestart=0
#Check checkalive.php
is_checkalive=`ps aux | grep -v grep| grep -v "$0" | grep $checkalive| wc -l | awk '{print $1}'`
if [ $is_checkalive != "0" ] ;
then
checkaliveId=$(ps -ef | grep $checkalive | grep -v 'grep' | awk '{ printf $2 }')
echo "Service $checkalive is running. $checkaliveId"
else
echo "$checkalive OFF"
needRestart=1
fi
#NEED needRestart
if [ $needRestart == "1" ];
then
#START SERVICE
echo "Restarting services..."
/usr/bin/php5.6 /home/michael/bash/$checkalive >/dev/null 2>&1 &
echo "$checkalive..."
echo `date '+%Y-%m-%d %H:%M:%S'` " Start /home/michael/bash/$checkalive" >> $log_file
fi
I can run it manually but when I try to run it in Cron, it doesn't work for some reasons. Apparently the command:
/usr/bin/php5.6 /home/michael/bash/$checkalive >/dev/null 2>&1 &
does not work.
All of file permissions are already set to executable. Any advice?
Thank you
You have run into one of cron's most common mistakes, trying to use it like an arbitrary shell script. Cron is not a shell script and you can't do everything you can do in one, like dereferencing variables or setting arbitrary new variables.
I suggest you replace your values into the cron line and avoid usage of variables
/usr/bin/php5.6 /home/michael/bash/checkalive.php >/dev/null 2>&1 &
Also, consider removing the trailing & as it is not necessary.

Bash Script issue, command not found, PATH seems to be correct

I have a issue with my Script, i am just trying to fingure out if my screen session is running or not (line 19).
The rest of the script is working.
#!/bin/bash
echo $PATH // /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
echo "0"
content=$(wget http://interwebs.com/index.php?page=count -q -O -)
z=$(($content / 5))
z=$(($z + 1))
echo $z // 4
lockfile=/var/tmp/mylock
if ( set -o noclobber; echo "$$" > "$lockfile") 2> /dev/null; then
trap 'rm -f "$lockfile"; exit $?' INT TERM EXIT
# do stuff here
x=1
count=0
while [ $x -le $z ]
do
$req ="$(ps -ef | grep -i mystatus$count | grep -v grep)"
if [ "$req" = "" ]; then
# run bash script
screen -amds mystatus$count /usr/bin/wget --spider interwebs.com/index.php?page=cronwhatsoever$(( $count +1))-$(( $count +5))
else
echo "Cron running"
fi
x=$(( $x + 1 ))
count=$(( $count +5))
done
# clean up after yourself, and release your trap
rm -f "$lockfile"
trap - INT TERM EXIT
else
echo "Lock Exists: $lockfile owned by $(cat $lockfile)"
fi
sleep 15
It returns line 19: =: command not found. Actually running:
ps -ef | grep -i bukkit | grep -v grep
Works without issues if i run it directly in my Terminal, so any idea how to solve this issue?
I guess it something PATH related but grep is located in /bin/grep.
$req ="$(ps -ef | grep -i mystatus$count | grep -v grep)"
should be
req="$(ps -ef | grep -i mystatus$count | grep -v grep)"
Don't use $ on the left-hand side of an assignment, and you must not have spaces around the =

Redirection based on variables assigned within while loop setup

I want to access while loop variable out side the loop
while read line
do
...
...
...
done < $file > /home/Logs/Sample_$line_$(date +"%Y%m%d_%H%M%S").log
In the above example whatever the log file is getting generated that doesn't have the value for the line variable. i.e. $line is not working here.
Please let me know how this can be written to make it work.
#!/bin/sh
exec 1> /home/Logs/`basename $0 | cut -d"." -f1 | sed 's/\.sh//g'`_$(date +"%Y%m%d_%H%M%S").log 2>&1
echo "Execution Started : `date` \n"
SQL_DIR=/home/Sql
INFILE=in_file
TEMPFILE=temp_file
RETURN_CODE=0
ls -ltr $SQL_DIR|grep ".sql"|awk -F" " '{print $9}'|awk -F"." '{print $1}' > $INFILE
sed '/^$/d' $INFILE > $TEMPFILE; mv $TEMPFILE $INFILE
while read line
do
{
START_TIME=`date +%s`
printf "\n SQL File Executed Is : $line.sql"
SQL_FILE_NM=$line.sql
SQL_FILE=$SQL_DIR/$SQL_FILE_NM
nzsql -db netezza_db -Atqf $SQL_FILE > /dev/null
RETURN_CODE=$?
if [ $RETURN_CODE -eq 0 ]; then
echo "Time taken to execute sqlfile $SQL_FILE=$TT_HRS:$TT_MINS:$TT_REM_SECS HH:MM:SS" > $TEMPFILE
printf "\n Success: Sql completed successfully at `date` \n"
cat $TEMPFILE|mailx -s "Time taken to execute sqlfile $SQL_FILE=$TT_HRS:$TT_MINS:$TT_REM_SECS HH:MM:SS" 'koushik.chandra#a.com'
else
printf "\n Error: Failed in sql execution at `date`"
exit $RETURN_CODE
fi
END_TIME=`date +%s`
TT_SECS=$(( END_TIME - START_TIME))
TT_HRS=$(( TT_SECS / 3600 ))
TT_REM_MS=$(( TT_SECS % 3600 ))
TT_MINS=$(( TT_REM_MS / 60 ))
TT_REM_SECS=$(( TT_REM_MS % 60 ))
printf "\n"
printf "Total time taken to execute the sql $line="$TT_HRS:$TT_MINS:$TT_REM_SECS HH:MM:SS
printf "\n"
} > /home/Logs/sql_query_time_$line_$(date +"%Y%m%d_%H%M%S").log
done < $INFILE
rm -f $INFILE $TEMPFILE
exit $RETURN_CODE
You actually need redirection inside the while loop:
while read -r line; do
{ cmd1; cmd2; cmd3; } > "/home/Logs/Sample_${line}_$(date +"%Y%m%d_%H%M%S").log"
done < "$file"
When you have > outfile after done then output is redirected to one file only.

Resources