How to make a bash script - linux

How can i make a bash script for this?My command is :
mysql -h(sample) -u(sample) -p -e "insert into databasename.tablename select count( * )from information_schema.processlist;"
Truly yours,
Joey

-bash-4.2$ cat > my_bash_script << "EOF"
> #!/bin/bash
>
> mysql -h(sample) -u(sample) -p -e "insert into databasename.tablename select count( * )from information_schema.processlist;"
> EOF
-bash-4.2$ chmod 755 my_bash_script

Related

Copy data from production DB to Archival DB

Please suggest how to fix below error.
My .sh file location: /archiveDB/service_arc.sh (x.x.x.85 server)
service_arc.sh script:
export PGPASSWORD=puser
/appdb/edb/install/9.5AS/bin/psql -h x.x.x.85 -p 5444 -U puser -d pdb -c "COPY (SELECT * FROM service_arc_old ) TO STDOUT;" | /appdb/edb/9.5as/bin/psql -h x.x.x.92 -p 2442 -U puser -d pdb -c "COPY service_arc FROM STDIN;"
nohup ./service_arc.sh > service_arc.log 2>&1 &
tail -f service_arc.log
Error:
nohup: ignoring input
./service_arc.sh: line 2: /appdb/edb/9.5as/bin/psql: No such file or directory

Multithreading in bash scripting

I run a bash script, and looping as much line in text file. to cURL the site listed in the txt file.
here is my script :
SECRET_KEY='zuhahaha'
FILE_NAME=""
case "$1" in
"sma")
FILE_NAME="sma.txt"
;;
"smk")
FILE_NAME="smk.txt"
;;
"smp")
FILE_NAME="smp.txt"
;;
"sd")
FILE_NAME="sd.txt"
;;
*)
echo "not in case !"
;;
esac
function save_log()
{
printf '%s\n' \
"Header Code : $1" \
"Executed at : $(date)" \
"Response Body : $2" \
"====================================================================================================="$'\r\n\n' >> output.log
}
while IFS= read -r line;
do
HTTP_RESPONSE=$(curl -L -s -w "HTTPSTATUS:%{http_code}\\n" -H "X-Gitlab-Event: Push Hook" -H 'X-Gitlab-Token: '$SECRET_KEY --insecure $line 2>&1) &
HTTP_BODY=$(echo $HTTP_RESPONSE | sed -e 's/HTTPSTATUS\:.*//g') &
HTTP_STATUS=$(echo $HTTP_RESPONSE | tr -d '\n' | sed -e 's/.*HTTPSTATUS://') &
save_log "$HTTP_STATUS" "$HTTP_BODY" &
done < $FILE_NAME
how i can run threading or make the loop fast in bash ?
You should be able to do this relatively easily. Don't try to background each command, but instead put the body of your while loop into a subshell and background that. That way, your commands (which clearly depend on each other) run sequentially, but all the lines in the file can be process in parallel.
while IFS= read -r line;
do
(
HTTP_RESPONSE=$(curl -L -s -w "HTTPSTATUS:%{http_code}\\n" -H "X-Gitlab-Event: Push Hook" -H 'X-Gitlab-Token: '$SECRET_KEY --insecure $line 2>&1)
HTTP_BODY=$(echo $HTTP_RESPONSE | sed -e 's/HTTPSTATUS\:.*//g')
HTTP_STATUS=$(echo $HTTP_RESPONSE | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
save_log "$HTTP_STATUS" "$HTTP_BODY" ) &
done < $FILE_NAME
My favourite was to do this is generate a file that lists all the commands you wish to perform. If you have a script that performs your operations create a file like:
$ cat commands.txt
echo 1
echo 2
echo $[12+3]
....
For example this could be hundreds of commands long.
To execute each line in parallel, use the parallel command with, say, at most 3 jobs running in parallel at any time.
$ cat commands.txt | parallel -j
1
2
15
For your curl example you could generate thousands of curl commands, execute them say 30 in parallel at any one time.

Unable to have aws userdata echo onto /etc/crontab

Long story short I am trying to have my aws userdata create a cronjob on launch. I am unable to echo into the /etc/crontab location. Here is a snippet of my code.
echo '# description of cronjob being addeded'
echo '0 16 * * 2,4,6 root some commands' | sudo tee -a /etc/crontab > /dev/null
echo ' ' | sudo tee -a /etc/crontab > /dev/null
Here is the Answer incase someone else needs to know.
"sudo sh -c 'echo \"# description of cronjob being addeded\" >> /etc/crontab' \n",
"sudo sh -c 'echo \"0 16 * * 2,4,6 root some commands\" >> /etc/crontab' \n",
"sudo sh -c 'echo \" \" >> /etc/crontab' \n",

Passing value from one shell script to other

This is what I am trying
script1
var=10
sh -x script2 "$var"
script2
su - someuser << EOF
1
cd dir
echo "This is : $1 Error" >> somefile
2
2
0
exit
EOF
Everything in script2 is executing. When I am checking the file "somefile" the output is
This is : Error
It is not showing the value of var
It is working fine for me:
cat s1
var=10
sh -x /tmp/s2 "$var"
cat s2
su - my_id << EOF
id
echo $1
EOF
./s1
+ su - my_id
+ 0<<
id
echo 10
my_id's Password: <<< su is prompting for my password
uid=222(my_id) gid=222(my_group) ...
10
Because you are not adding the #!/bin/xxxxx I believe the default is to either execute /bin/sh or maybe what $SHELL is set to. Check that both of those are what you expect. Maybe add the explicit #!/bin/ksh (or #!/bin/sh ...) to make sure you are getting the shell that you want / expect.

setting crontab from bash script

I have written the following script but i am getting error sqlcorn.sh: No such file or directory
here is the script
#!/bin/bash
ORACLE_HOME="/opt/app/oracle/product/11.2.0/dbhome_1"
ORACLE_SID="HEER"
ORACLE_USER="USER1"
ORACLE_PASSWORD="USERP"
echo "export ORACLE_HOME=$ORACLE_HOME" >> sqlcronprocedure.sh
echo "export PATH=\$ORACLE_HOME/bin:\$PATH" >> sqlcronprocedure.sh
echo "export ORACLE_SID=$ORACLE_SID" >> sqlcronprocedure.sh
echo "rTmpDir=/tmp" >> sqlcronprocedure.sh
echo "sqlplus -s $ORACLE_USER#$ORACLE_SID/$ORACLE_PASSWORD > $rTmpDir/deleteme.txt 2>&1 <<EOF" >> sqlcronprocedure.sh
echo " select 1 from dual;" >> sqlcronprocedure.sh
echo " execute someproc(1000,14);" >> sqlcronprocedure.sh
echo "EOF" >> sqlcronprocedure.sh
chmod 755 sqlcronprocedure.sh
crontab -l > sqlcron.sh
echo "0,15,30,45 * * * * /sqlcronprocedure.sh" >> sqlcron.sh
crontab sqlcorn.sh
This is my first ever script. so i apologize if things are too obvious to ask
The real problem: You misspelled the file name on the crontab command. Change it from:
crontab sqlcorn.sh
to
crontab sqlcron.sh
Some more comments on your code:
Your multiple echo commands are better written as a "here document". Rather than this:
echo "export ORACLE_HOME=$ORACLE_HOME" >> sqlcronprocedure.sh
echo "export PATH=\$ORACLE_HOME/bin:\$PATH" >> sqlcronprocedure.sh
echo "export ORACLE_SID=$ORACLE_SID" >> sqlcronprocedure.sh
echo "rTmpDir=/tmp" >> sqlcronprocedure.sh
echo "sqlplus -s $ORACLE_USER#$ORACLE_SID/$ORACLE_PASSWORD > $rTmpDir/deleteme.txt 2>&1 <<EOF" >> sqlcronprocedure.sh
echo " select 1 from dual;" >> sqlcronprocedure.sh
echo " execute someproc(1000,14);" >> sqlcronprocedure.sh
echo "EOF" >> sqlcronprocedure.sh
you can do this:
cat <<EOF >sqlcronprocedure.sh
export ORACLE_HOME=$ORACLE_HOME
export PATH=\$ORACLE_HOME/bin:\$PATH
export ORACLE_SID=$ORACLE_SID
rTmpDir=/tmp
EOF
cat <<END_OF_SCRIPT >sqlcronprocedure.sh
export ORACLE_HOME=$ORACLE_HOME
export PATH=\$ORACLE_HOME/bin:\$PATH
export ORACLE_SID=$ORACLE_SID
rTmpDir=/tmp
sqlplus -s $ORACLE_USER#$ORACLE_SID/$ORACLE_PASSWORD > $rTmpDir/deleteme.txt 2>&1 <<EOF
select 1 from dual;
execute someproc(1000,14);
EOF
END_OF_SCRIPT
which is a bit easier to read. (Your version wasn't incorrect, just hard to read and error-prone; you have to get the >> sqlcronprocedure.sh right on eacn and every line.)
Note: You use >> to build sqlcronprocedure.sh, which appends to the existing sqlcronprocedure.sh if it exists. I don't think that's what you want to do; you probably want to create the file from scratch. My code assumes you want to create the file rather than appending to it.
The last part of your script:
crontab -l > sqlcron.sh
echo "0,15,30,45 * * * * /sqlcronprocedure.sh" >> sqlcron.sh
crontab sqlcorn.sh
is fine except for two things.
First sqlcron.sh is not a good name for the file, since it's not a shell script; just call it sqlcron. The system doesn't care, but you should.
Second, the misspelling I mentioned above.
you need to specify the full path to sqlcron.sh in your script...

Resources