Why not working shell command with variables on envoy? - laravel

I have simple Envoy setup. A server:
#task('build_todo_list', ['on'=> $server])
echo 'Build todo list'
cd {{ $releases_dir }}
current=$(ls -td -- * | head -n 1 | cut -d'/' -f1)
d=$(echo $current | cut -c 1-8)
echo $d
since=$(date -d $d +"%Y-%m-%d")
until=$(date +"%Y-%m-%d")
echo $since
echo $until
cd {{$source_dir}}
git log staging --pretty="%an %B" --since=$since --until=$until | grep 'Fixed \| Did'
#endtask
But i have an error because i use $since and $until variables in my git log command. And this variables good work with echo command.

Problem has been not variables. If between "since" and "until" not have records - will not be task complete successfully.

Related

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.

Same KSH script run in test env but fails in production

I run the below script in a reference env and get the correct output in the output file, but when I run the same in production the output file is empty.
I tried debugging the using set -x, and understand that the for loop is not getting executed in the prod env.
please suggest what might be the issue.
#!/bin/ksh
DIR=/some/log/dir/error
DATE=$1
OUTPUT=/some/logs/dir/scripts/output/output.csv
. /calling/env/setupscript.ksh
for file in $(find $DIR 2>/dev/null| grep $ASOF | grep -i something1 | grep -vi someotherthing2 | grep -iv someotherthing3 | grep -vi someotherthing4 | grep -vi someotherthing4 )
do
echo "Checking file $file..."
if [[ -z "$DONE" ]] then
head -1 $file | read line
echo "File name;Filter name;Type;Scenario;$line" > $OUTPUT.tmp
DONE=1
fi
fullfile=$file
file=$(basename $file)
file=${file#SPR_RPT_}
file=${file#SPR_BY__}
echo $file | awk -F_ '{ print $(NF-2)" "$(NF-1) }' | read type scen
filter=${file%%_$type\_*}
grep ERROR $fullfile | while read line
do
echo "$(basename $fullfile);$filter;$type;$scen;$line" >> ${OUTPUT}.tmp
done
done

if statement throws error as end of file

i have a script that looks something like
#!/bin/bash$
#x=new value$
#y=old value$
$
export PATH=/xxx/xxx/xxx:$PATH$
$
#get the difference of two files$
diff --side-by-side --suppress-common-lines file.txt file1.txt | tr -d "|,<,>,'\t'" | sed 's/ /:/g' | sed 's/^://' > diff.txt$
cat diff.txt$
$
#get the values$
for i in `cat diff.txt`; do$
plug_x=`echo $i | cut -d ":" -f1`$
echo "the value of jenkins plugin is $plug_x"$
ver_x=`echo $i | cut -d ":" -f2`$
echo "the value of jenkins version is $ver_x"$
plug_y=`echo $i | cut -d ":" -f3`$
echo "the value of db plugin is $plug_y"$
ver_y=`echo $i | cut -d ":" -f4`$
echo "the value of db version is $ver_y"$
if [ -z "$ver_y" ] && [ -z "$ver_x" ] ;$
then $
echo "the plugin is newly added"$
#newly added plugin should be updated in the db$
# mysql -u root -ppassword -h server --local-infile db << EOFMYSQL$
#update the table with the new version$
#EOFMYSQL$
else$
echo "the plugin has changes"$
mysql -u root -ppassword -h server --local-infile db << EOFMYSQL$
insert into table (xxx, xxx) values('$ver_x','$plug_x');$
$
EOFMYSQL $
fi$
done$
but when i run this script it saya
Syntax error: end of file unexpected (expecting "fi")
but the fi is there..i cant figure out why it is throwing the error
this error does not come when i just have echo statements in the script
I would suggest that if you are just running that one query to insert into table, then echo the query and pipe it to the mysql command, that would be (in my opinion) a better and efficient choice.
#!/bin/bash
diff --side-by-side --supress-common-lines test1.txt test2.txt | tr -d "|,<,>,'\t'" | sed 's/ /:/g' | sed 's/^://' > ./diff.txt
for i in `cat ./diff.txt`; do
plug_x=`echo $i | cut -d ":" -f1`
echo "the value of jenkins plugin is $plug_x"
ver_x=`echo $i | cut -d ":" -f2`
echo "the value of jenkins version is $ver_x"
plug_y=`echo $i | cut -d ":" -f3`
echo "the value of db plugin is $plug_y"
ver_y=`echo $i | cut -d ":" -f4`
echo "the value of db version is $ver_y"
if [ -z "$ver_y" ] && [ -z "$ver_x" ]
then
echo "the plugin is newly added"
else
echo "the plugin has changes"
echo "insert into table (xxx, xxx) values('$ver_x','$plug_x')" | mysql -u root -ppassword -h server --local-infile db
fi
done
That should do the job or if you'd like to use While loop, you can certainly do so.
#!/bin/bash
diff --side-by-side --supress-common-lines test1.txt test2.txt | tr -d "|,<,>,'\t'" | sed 's/ /:/g' | sed 's/^://' > ./diff.txt
cat ./diff.txt | while read i
do
plug_x=`echo $i | cut -d ":" -f1`
echo "the value of jenkins plugin is $plug_x"
ver_x=`echo $i | cut -d ":" -f2`
echo "the value of jenkins version is $ver_x"
plug_y=`echo $i | cut -d ":" -f3`
echo "the value of db plugin is $plug_y"
ver_y=`echo $i | cut -d ":" -f4`
echo "the value of db version is $ver_y"
if [ -z "$ver_y" ] && [ -z "$ver_x" ]
then
echo "the plugin is newly added"
else
echo "the plugin has changes"
echo "insert into table (xxx, xxx) values('$ver_x','$plug_x')" | mysql -u root -ppassword -h server --local-infile db
fi
done

shell script not working through cron but manually

I'm using below command in a shell script:
echo "1" > log.txt
if [ `ifconfig eth0 | grep 'inet addr' | cut -d ":" -f 2 |
awk {'print $1'}` = 'ipaddress' ] && [ `whoami` = 'userid' ]; then
echo "2" >> log.txt
crontab -l > Cron.txt
echo "3" >> log.txt
fi
The script runs fine when run manually but when scheduled through cron, it
stucks at this IF.
cron entry: 31 11 * * * /home/abc/cron_backup.sh
Output in log.txt Manual run: prints 1,2,3 in log.txt through
cron: prints 1 in log.txt
I would suggest to put as first line of your script the command interpreter line #! to make sure that sh will run it
after that, have you consider to use double bracket syntax [[ ]]?
#!/bin/sh
echo "1" > log.txt
if [[ `ifconfig eth0 | grep 'inet addr' | cut -d ":" -f 2 |
awk {'print $1'}` = 'ipaddress' ]] && [[ `whoami` = 'userid' ]]; then
echo "2" >> log.txt
crontab -l > Cron.txt
echo "3" >> log.txt
fi
The problem could be with the ifconfig, grep, cut awk and whoami calls. When you run it from the command line you have your profile, which has your PATH setting.
When it is run from cron, it does not have your profile. If you modify your PATH variable to point to the location of these programs then you wouldn't have that change when run from cron.
Try putting in the full path for the each of the commands and see it that makes any difference when run from cron.

OpenLDAP: get `directory` from cn=config

How can I get directory for specified DN by one ldapsearch request?
I mean - I have few databases. OpenLDAP configured with cn=config. For each DN - it hve own ldif-file, where it's olcDbDirectory specified.
Can I obtain olcDbDirectory value for each DN?
For backup script - I need to set varibale which contains directory, and this variable changes every time for every DN, wich backuped/restored at this moment.
So - in bash I just found solution to create function like:
#!/bin/bash
getDir () {
file=`grep -R "$1" /etc/openldap/slapd.d/ | cut -d":" -f 1 | tail -n 1`
echo $file
dir=`cat $file | grep "olcDbDirectory" | awk '{print $2}'`
echo $dir
}
getDir testdb;
$ ./dn.sh
/etc/openldap/slapd.d/cn=config/olcDatabase={9}bdb.ldif
/var/lib/ldap/testdb
But this solution seems not tidy... And I'd preffered to use something like:
getDir () {
dir=`ldapsearch -x -D "cn-root,cn=config" "*somefilter*"
}
Here it is:
$ ldapsearch -x -LLL -D 'cn=root,cn=config' -w PassWord -b 'cn=config' '(&(olcDbDirectory=*)(olcSuffix='testdb'))' olcDbDirectory | grep "olcDbDirectory" | cut -d":" -f 2
/var/lib/ldap/testdb
Of in bash function:
#!/bin/bash
getDir () {
dirtodel=`ldapsearch -x -LLL -D 'cn=root,cn=config' -w PassWord -b 'cn=config' '(&(olcDbDirectory=*)(olcSuffix='${1}'))' olcDbDirectory | grep "olcDbDirectory" | cut -d":" -f 2`
echo $dirtodel
}
getDir 'dc=testdb'
Result:
$ ./dn.sh
/var/lib/ldap/testdb

Resources