shell script is not working when it is called using cron scheduler - linux

I have this sh script
SPTH="/home/db_backup/log/log_"$(date +'%d-%m-%Y %H:%M')".txt"
echo "===DUMP started at $(date +'%d-%m-%Y %H:%M:%S')===" >> "$SPTH";
DBNAME="test"
PGPASSWORD=postgres psql -U postgres -d postgres -c "CREATE DATABASE $DBNAME";
PGPASSWORD=postgres psql -U postgres -d $DBNAME -c "ALTER SCHEMA public RENAME TO public_old";
for entry in /home/SQL/*.*
do
echo "$entry"
SUBSTRING=$(echo $entry| cut -d'/' -f 4)
#echo $SUBSTRING
SCHEMA=$(echo $SUBSTRING| cut -d'.' -f 1)
echo $SCHEMA
echo "===================== CREATING public SCHEMA =====================" >> "$SPTH";
PGPASSWORD=postgres psql -U postgres -d $DBNAME -c "Create Schema public";
echo "===================== RESTORING DATA OF $SCHEMA =====================" >> "$SPTH";
PGPASSWORD=postgres psql -U postgres -d $DBNAME -f "$entry";
echo "===================== DELETING SCHEMA $SCHEMA=====================" >> "$SPTH";
PGPASSWORD=postgres psql -U postgres -d $DBNAME -c "DROP SCHEMA $SCHEMA CASCADE";
echo "===================== RENAMING SCHEMA PUBLIC TO $SCHEMA=====================" >> "$SPTH";
PGPASSWORD=postgres psql -U postgres -d $DBNAME -c "ALTER SCHEMA public RENAME TO $SCHEMA";
echo "===================== RENAMING SQL FILE =====================" >> "$SPTH";
mv /home/SQL/{$SUBSTRING,DONE--$SUBSTRING};
echo "===================== DONE =====================" >> "$SPTH";
done
PGPASSWORD=postgres psql -U postgres -d $DBNAME -c "ALTER SCHEMA public_old RENAME TO public";
echo "===DUMP finished at $(date +'%d-%m-%Y %H:%M:%S')===" >> "$SPTH";
When I am running this command using command
sh script.sh
its working fine, working as I expected and running each and every command. But when I am running this script using cron scheduler commands related to POSTGRES is not executing. I also want log of every command I in this shell script.
I want to run this script using cron scheduler.
Where I am wrong?

Related

Kubectl with Bash command is always passed in LowerCase and not CamelCase

Consider the Bash code:
function dropMyDB() {
kubectl -n $1 exec -ti $1-CLUSSTER-0 -- psql -d MYDBNAME -U postgres -c "truncate table "$2";"
}
dropMyDB $1 "myTableNameInCamelCase"
When I execute the code it produces:
ERROR: relation "mytablenameincamelcase" does not exist
command terminated with exit code 1
Which means that the table name is not passed in its CamelCase form.
How can we fix this ?
First
Escape your "$2" because it is inside another double quote
postgres -c "truncate table "$2";"
# to
postgres -c "truncate table $2;"
# or
postgres -c "truncate table \"$2\";"
Second
You can test that the issue is not bash
function dropMyDB() {
echo "kubectl -n $1 exec -ti $1-CLUSSTER-0 -- psql -d MYDBNAME -U postgres -c \"truncate table \"$2\";\""
}
dropMyDB $1 "myTableNameInCamelCase"
Then
chmod +x script.sh
./script.sh foo
kubectl -n foo exec -ti foo-CLUSSTER-0 -- psql -d MYDBNAME -U postgres -c "truncate table "myTableNameInCamelCase";"
IMO it's no kubectl's fault:
fun(){ k exec aaaaaaaaaaaaa -it -- echo "$1"; }
fun AdsdDasfsFsdsd
AdsdDasfsFsdsd
But probably psql's one, try it like this:
... psql -d MYDBNAME -U postgres -c "truncate table '$2';"

Shell script: execute 2 commands and keep first running

I'm trying to write a shell script for my docker image where:
a mssqql server is started
database setup happens
However with my current script my sql server instance stops as soon as the data import is done. Could anyone point me out what I'm doing wrong?
#!/bin/bash
database=myDB
wait_time=30s
password=myPw
exec /opt/mssql/bin/sqlservr &
echo importing data will start in $wait_time...
sleep $wait_time
echo importing data...
/opt/mssql-tools/bin/sqlcmd -S 0.0.0.0 -U sa -P $password -i ./init.sql
for entry in "table/*.sql"
do
echo executing $entry
/opt/mssql-tools/bin/sqlcmd -S 0.0.0.0 -U sa -P $password -i $entry
done
for entry in "data/*.csv"
do
shortname=$(echo $entry | cut -f 1 -d '.' | cut -f 2 -d '/')
tableName=$database.dbo.$shortname
echo importing $tableName from $entry
/opt/mssql-tools/bin/bcp $tableName in $entry -c -t',' -F 2 -S 0.0.0.0 -U sa -P $password
done
I did not see any clear mistakes in your shell script. I am just advising you the below:-
Try to run the server from the current shell of the script without exec
/opt/mssql/bin/sqlservr &
Put some echo in both the loop statements to check what is going on there.
Hope this will help.
Seems I need to add set -m to resolve this.

Using cqlsh with -f option

Can we use cqlsh -f like it?
psql -U maint_sa -p 3254 postgres -f - << EOT
SHOW ALL;
EOT
On cqlsh I have following:
cqlsh -f - << EOT
> DISCRIBE KEYSTORE ks1;
> EOT
Can't open '-': [Errno 2] No such file or directory: '-'
Have you tried echoing it to the command line:
echo "DESC KEYSPACE ks1; exit" | cqlsh

Import cassandra schema script doesn't run in docker when using volumes

I have a problem on using cassandra in Docker
I've created a Dockerfile this like
----------------------Dockerfile----------------------------
FROM spotify/cassandra:base
COPY cassandra-schema.cql /tmp/
COPY cassandra-init.sh /usr/local/bin/
COPY cassandra-singlenode.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/cassandra-init.sh
RUN chmod +x /usr/local/bin/cassandra-singlenode.sh
#import schema
RUN /usr/local/bin/cassandra-init.sh
EXPOSE 9160
ENTRYPOINT ["/usr/local/bin/cassandra-singlenode.sh"]
If i have use
docker run --name cassandradb cassandra
everything works properly but if i use
docker run --name cassandradb -v /opt/argus/cassandra/data/:/var/lib/cassandra/data -v /opt/argus/cassandra/commitlog:/var/lib/cassandra/commitlog cassandra
the cassandra starts but the /usr/local/bin/cassandra-init.sh doesn't import my scheam
Any idea?
These are my files contents
------------------cassandra-init.sh-----------------------
echo "===================================================="
echo "starting running cqlsh"
echo "===================================================="
cassandra &
while : ;do
# Get the status of this machine from the Cassandra nodetool
STATUS=`nodetool status | grep 'UN' | awk '{print $1}'`
echo $STATUS
# Once the status is Up and Normal, then we are ready
if [ $STATUS = "UN" ]; then
cqlsh -f /tmp/cassandra-schema.cql
break
fi
sleep 1;
done
----------------------------cassandra-singlenode.sh--------------------------------
echo "=============================================== Change configuration ====================================================="
IP=`hostname --ip-address`
SEEDS=`env | grep CASS[0-9]_PORT_9042_TCP_ADDR | sed 's/CASS[0-9]_PORT_9042_TCP_ADDR=//g' | sed -e :a -e N -e 's/\n/,/' -e ta`
if [ -z "$SEEDS" ]; then
SEEDS=$IP
fi
echo "Listening on: "$IP
echo "Found seeds: "$SEEDS
# Setup Cassandra
CONFIG=/etc/cassandra/
sed -i -e "s/^listen_address.*/listen_address: $IP/" $CONFIG/cassandra.yaml
sed -i -e "s/^rpc_address.*/rpc_address: 0.0.0.0/" $CONFIG/cassandra.yaml
sed -i -e "s/- seeds: \"127.0.0.1\"/- seeds: \"$SEEDS\"/" $CONFIG/cassandra.yaml
sed -i -e "s/# JVM_OPTS=\"$JVM_OPTS -Djava.rmi.server.hostname=<public name>\"/ JVM_OPTS=\"$JVM_OPTS -Djava.rmi.server.hostname=$IP\"/" $CONFIG/cassandra-env.sh
echo "=========================================================================================================================="
echo "starting running cassandra server"
echo "=========================================================================================================================="
cassandra &
while :
do
echo "Cassandra running, Press [CTRL+C] to stop.."
sleep 1
done

shell script ssh command not working

I have a small list of servers, and I am trying to add a user on each of these servers. I can ssh individually to each server and run the command.
sudo /usr/sbin/useradd -c "Arun" -d /home/amurug -e 2014-12-12 -g users -u 1470 amurug
I wrote a script to loop through the list and run this command but I get some errors.
#!/bin/bash
read -p "Enter server list: " file
if [[ $file == *linux* ]]; then
for i in `cat $file`
do
echo "creating amurug on" $i
ssh $i sudo /usr/sbin/useradd -c "Arun" -d /home/amurug -e 2014-12-12 -g users -u 1470 amurug
echo "==============================================="
sleep 5
done
fi
When I run the script it does not execute the command.
creating amurug on svr102
Usage: useradd [options] LOGIN
Options:
What is wrong with my ssh crommand in my script?
Try this script:
#!/bin/bash
read -p "Enter server list: " file
if [[ "$file" == *linux* ]]; then
while read -r server
do
echo "creating amurug on" "$server"
ssh -t -t "$server" "sudo /usr/sbin/useradd -c Arun -d /home/amurug \
-e 2014-12-12 -g users -u 1470 amurug"
echo "==============================================="
sleep 5
done < "$file"
fi
As per man bash:
-t
Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.

Resources