Script and Scriptreplay give me some problems - shell

I want to replicate what I do on my console.
I'm using this command:
roberto#rcisla-pc:~/Desktop$ script -t 2> timing.log -a output.session
and then I execute some commands but at the moment of replication this occurs:
roberto#rcisla-pc:~/Desktop$ scriptreplay time.log record.session
scriptreplay: unexpected end of file on record.session
And record.session is empty. I don't know what's wrong!!!
I'm on ubuntu 13.04,
thanks !!

Well, just I missed the exit command.
This is the complete sequence:
1- To start the recording
roberto#rcisla-pc:~/Desktop$ script -t 2> timing.log -a output.session
2- To stop the recording execute exit
roberto#rcisla-pc:~/Desktop$ exit
3- and then I execute the next command to see the complete recording:
roberto#rcisla-pc:~/Desktop$ scriptreplay timing.log output.session

Related

The 2nd command is not executed before terminating the previous one

I'm very new to bash. I'm running a bash script. It is supposed to start Neo4j and then execute a series of queries located in a file called "cypher.ex1". Here is the code:
#!/bin/bash
./bin/neo4j console
./bin/cypher-shell -u neo4j -p 123456 --file cypher.ex1
In order to use Cypher-shell, we should start the Neo4j service first. So, this line:
./bin/neo4j console
starts the Neo4j, so that cypher-shell can be used using the following line:
./bin/cypher-shell -u neo4j -p 123456 --file cypher.ex1
The problem is that since ./bin/neo4j console starts the Neo4j service, the next command (./bin/cypher-shell -u neo4j -p 123456 --file cypher.ex1) is not executed unless I press the "Ctrl + C". If I press "Ctrl + C", the Neo4j service will be stopped and the following command will not be executed too (I get "connection refused" error). What should I do in order to start the Neo4j service and then run the cypher shell in this bash script?
I tried the solutions given in Run a command in background and another command in frontground in the same line. None of them worked for me. For example, When I execute the code with "(command1 &); command2" (As it was suggested in the proposed topic), my script is executed 2 times automatically. The first time command2 is executed and since the command1 is not executed I get "connection refused" error; The second time command1 is executed and command2 is not executed.
As it it mentioned in first answer in the Q/A you should exec the commands on this way:
#!/bin/bash
./bin/neo4j console &
./bin/cypher-shell -u neo4j -p 123456 --file cypher.ex1
This will run ./bin/neo4j console in background. So you need to take care about this process and stop it in case of need:
PID=$(jobs -l |awk '{print $2}')
kill $PID

bash -x /var/lib/cloud/instance/user-data.txt runs but user-data from terraform gives error

I am trying to run a script using terraform. The content of the user-data is as follows:
.
.
cat <<EOH | java -jar ./jenkins-cli.jar -s $JENKINS_URL -auth admin:$PASSWORD create-credentials-by-xml system::system::jenkins _
<com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey plugin="ssh-credentials#1.16">
<scope>GLOBAL</scope>
<id>$CRED_ID</id>
<description>$SLAVE_IP pem file</description>
<username>ec2-user</username>
<privateKeySource class="com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey\$DirectEntryPrivateKeySource">
<privateKey>${worker_pem}</privateKey>
</privateKeySource>
</com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey>
EOH
.
.
When it executes as part of user-data it gives out an error as No such command create-credentials-by-xml.
But when I log in to the instance and execute bash -x /var/lib/cloud/instance/user-data.txt it runs as expected.
Can anyone please tell what is the reason for it and how to fix it? Thanks!
I have tried #cloudhook and separating the lines as well, but didn't work.
Answering my question:
Where was the problem:
The issue was not with bash but with jenkins-cli.jar itself.
The error message showed No such command create-credentials-by-xml which made me think, it was bash error but in reality, it was jar file error all along.
Reason:
The reason it was failing at user-data execution and not at bash execution was that it was unable to load configuration of plugins in a short amount of time.
Solution:
From the given reason, it is obvious that it needs time so I gave it sleep 25 to confirm it works or not and yes it does work but it was not an ideal solution.
Optimized Solution:
To make it better I listed the plugins before executing any jar commands and if the list came empty re run the commnd
# Creating CMD utility for jenkins-cli commands
jenkins_cmd="java -jar /opt/jenkins-cli.jar -s $JENKINS_URL -auth admin:$PASSWORD"
# Waiting for Jenkins to load all plugins
while (( 1 )); do
count=$($jenkins_cmd list-plugins 2>/dev/null | wc -l)
ret=$?
echo "count [$count] ret [$ret]"
if (( $count > 0 )); then
break
fi
sleep 30
done

Run bash script loop in background which will write result of jar command to file

I'm novice to running bash script. (you can suggest me, if title I've given is incorrect.)
I want to run a jar file using bash script in loop. Then it should write the output of jar command into some file.
Bash file datagenerate.sh
#!/bin/bash
echo Total iterations are 500
for i in {1..500}
do
the_output="$(java -jar data-generator.jar 10 1 mockData.csv data_200GB.csv)"
echo $the_output
echo Iteration $i processed
done
no_of_lines="$(wc -l data_200GB.csv)"
echo "${no_of_lines}"
I'm running above script using command nohup sh datagenerate.sh > datagenerate.log &. As I want to run this script in background, so that even I log out from ssh it should keep running & output should go into datagenerate.log.
But when I ran above command and hit enter or close the terminal it ends the process. Only Total iterations are 500 is getting logged into output file.
Let me know what I'm missing. I followed following two links to create above shell script: link-1 & link2.
nohup sh datagenerate.sh > datagenerate.log &
nohup should work this way without using screen program, but depending on your distro your sh shell might be linked to dash.
Just make your script executable:
chmod +x datagenerate.sh
and run your command like this:
nohup ./datagenerate.sh > datagenerate.log &
You should check this out:
https://linux.die.net/man/1/screen
With this programm you can close your shell while a command or script is still running. They will not be aborted and you can pick the session up again later.

How to Catch an Error While Establishing Remote Connection in Shell Script

This is how part of my shell script looks like.
#! /bin/sh
sftp -i $IdentityFile $ServerAddress << EOF
command 1 #Execute in the remote
command 2 #Execute in the remote
bye
EOF
command 3 #Execute locally
As per my current knowledge of scripting, if a command fails to execute, the control simply passes to the next command. But what if the sftp command fails to establish a network connection in the above block? Does it mean command 1 and command 2 will be executed locally? Or will the control jump to command 3?
How do I catch a possible error in sftp and direct the control to command 3? And if that is possible, can I detect the error using the ? variable, to take certain pre-emptive action? Some guidance will be great.
There are several things to do.
First, you need to extract the input as a function, to allow piping with command3, and it will be more legible:
function sfpInstruction() {
cat << EOF
command 1
command 2
bye
EOF
}
Thus, your sftp instruction can be changed to:
tmpFile="/tmp/errorFile.txt"
sftp -i $IdentityFile $ServerAddress $( sfpInstruction ) 2>"$tmpFile" || command3
Such a way:
all error messages are outpu in your error file
in any case, if sftp exits with a failing status (NOT 0), GNU/Bash will execute command3
if in addition you want command3 to read/check/parse the error messages, you can give it the "$tmpFile"

Running a JMeter script with nohup

For the first time I'm just playing around with nohup on top of an Ubuntu server. I read few docs about nohup and got to know about the running commands with options such as nohup ./server.sh &.
What I want to know is that, how should I be running the JMeter script (in headless mode) using nohup? Following is the script I needed to run with nohup:
./jmeter.sh -n -t /home/chamith/WSO2MB/new/apache-jmeter-2.13/bin/GamesSubscriber.jmx
When I tried using the normal nohup operation within the script it always throws me an error saying -n command not found. How should I move on with this? Any help would be appreciated.
Although I cannot reproduce your issue you can try surrounding your command with quotation marks like:
nohup "./jmeter.sh -n -t /home/chamith/WSO2MB/new/apache-jmeter-2.13/bin/GamesSubscriber.jmx"
Also don't forget -l key to save the results into a file.
The full command which runs script totally in the background will look like:
nohup "./jmeter.sh -n -t /home/chamith/WSO2MB/new/apache-jmeter-2.13/bin/GamesSubscriber.jmx -l result.jtl" > /dev/null 2>&1 &
References:
nohup man page
nohup Execute Commands After You Exit From a Shell Prompt
How Do I Run JMeter in Non-GUI Mode?
Full list of JMeter command-line options

Resources