I have a number of Jhipster apps to scaffold.
Creating the entities by typing in the name, fields and relationships is time consuming, boring and error prone when you have many tables.
Where is the SQL script to run against my database? The one that outputs a script which can be run as a batch file to create the entities programmatically.
Great product by the way.
JHipster generates a Liquibase changelog, there is no SQL script (but it's basically the same, only it is database independant and easier to version).
What you would like is to script the generator: it does not exist yet.
Yeoman generators don't usually work that way: one good reason is that there is quite a lot of validation for each question, so it's much less error prone to answer them than to script them, normally.
Then, you have a specific use case as you want to auto generate several applications with the same code and database tables: I don't know the rational behind this, but you can understand that for a "normal" situation this would be considered a bad practice.
I thought it is never too late for this.
I read this post and tried out an auto-generation script, based on that post's instructions and the links in it, to generate my entities.
My sample script below creates a "book" entity.
Instructions:
Create in the project directory, a batch file with extension .cmd
and as content the script below.
Open a Windows console (Console 1) with "cmd" command.
CD to your JHipster project directory.
Open a second console (Console 2).
CD to your JHipster project directory as above.
Run the script in Console 2.
Immediately click the titlebar of Console 1, for it to get the
focus.
You will see your book entity being created.
From there you may manually edit an entity's JSON file and re-run (this time manually!!) the "yo jhipster:entity" command for that entity.
You may put all your entities with all fields and relations in the script and enjoy the ride.
It might be very useful if you have a UML tool produce a script like this.
The pings are to pause the input being send to the console windows with the focus, because the command "yo jhipster:entity" takes some time to present the first prompt and some times also the others.
#if (#CodeSection == #Batch) #then
#echo off
rem Use %SendKeys% to send keys to the keyboard buffer
set SendKeys=CScript //nologo //E:JScript "%~F0"
rem set SendKeys=CScript //nologo //E:JScript
rem Start the other program in the same Window
start "" /B cmd
ping -n 12 -w 1 127.0.0.1 > NUL
%SendKeys% "yo jhipster:entity book{ENTER}"
ping -n 2 -w 1 127.0.0.1 > NUL
%SendKeys% "{Y}{ENTER}"
ping -n 2 -w 1 127.0.0.1 > NUL
%SendKeys% "name{ENTER}"
ping -n 2 -w 1 127.0.0.1 > NUL
%SendKeys% "{ENTER}"
ping -n 2 -w 1 127.0.0.1 > NUL
%SendKeys% "{Y}{ENTER}"
ping -n 2 -w 1 127.0.0.1 > NUL
%SendKeys% " "
ping -n 2 -w 1 127.0.0.1 > NUL
%SendKeys% "{DOWN}"
ping -n 2 -w 1 127.0.0.1 > NUL
%SendKeys% " "
ping -n 2 -w 1 127.0.0.1 > NUL
%SendKeys% "{DOWN}"
ping -n 2 -w 1 127.0.0.1 > NUL
%SendKeys% " "
ping -n 2 -w 1 127.0.0.1 > NUL
%SendKeys% "{ENTER}"
ping -n 2 -w 1 127.0.0.1 > NUL
%SendKeys% "4{ENTER}"
ping -n 2 -w 1 127.0.0.1 > NUL
%SendKeys% "64{ENTER}"
ping -n 2 -w 1 127.0.0.1 > NUL
%SendKeys% "N{ENTER}"
ping -n 2 -w 1 127.0.0.1 > NUL
%SendKeys% "N{ENTER}"
ping -n 2 -w 1 127.0.0.1 > NUL
%SendKeys% "{ENTER}"
goto :EOF
#end
// JScript section
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));
Related
Does anyone know if TERA TERM allows me to copy a file on my server to my local PC?
My job does not allow me to install any other software on this server, so I plan to create a macro with whatever tera term offers.
For example, I want to copy a file name test.bin located in /tmp to my PC C:\test\logs.
I tried using TERA TERM "SCPSEND," but it did not work; it gave me a pop box with the error "File open error." I assume this command is only meant to copy files into the host server, not from the host server to my local PC.
SOURFILE = '/tmp/test.bin'
DESTFILE = 'C:\test\logs'
;; Send a file.
scpsend SOURFILE DESTFILE
;; Confirm a scp process.
do
mpause 5000
sprintf2 str 'ps -ef |grep -v grep |grep -c scp'
sendln str
waitln '0' '1'
loop while result != 1
;; Do next macro after finishing the file sending.
sendln 'echo SCP finish'
;; Exit this macro script.
end
I'm working on a script in Linux Bash, with different kinds of options to use.
Basically, the program is going to ping to the given ip-address.
Now, I want to enable the user to write a range of ip-adresses in the terminal, which the program then will ping.
Fe: bash pingscript 25 - 125
the script will then ping all the addresses between 192.168.1.25 and 192.168.1.125.
That's not to hard, I just need to write a little case with
[0-9]-[0-9] ) ping (rest of code)
Now the problem is: this piece of code will only enable me to ping numbers of fe. 0 - 9 and not 10 - 25.
For that I'd need to write:
[0-9][0-9]-[0-9][0-9] (fe: ping 25 - 50)
But then there's the possibility of having 1 number on one side and 2 on the other: [0-9]-[0-9][0-9] (fe: ping 1 - 25)
or: [0-9]-[0-9][0-9][0-9] (fe: ping 1 - 125)
and so on... That means there're a lot of possibilities.
There's probably another way to write it, but how?
I don't want any letters to be in the arguments, but I can't start with that (loop-through system).
How about that:
for i in 192.168.1.{25..125}; do ping -qnc1 $i; done
Or in a script with variables as arguments:
for i in $(seq -f "192.168.1.%g" $1 $2); do ping -qnc1 -W1 $i; done
Where the first argument is the number where to begin and the second argument where to end. Call the script like this:
./script 25 125
The ping options:
-q: that ping doesn't print a summary
-n: no dns lookups
-c1: Only send 1 package
-W1: timeout to 1 second (can be increased of cource)
You can use extended pattern matching in your script by enabling the extglob shell option:
shopt -s extglob
So you can use braces with a + quantifier like this:
#!/bin/bash
shopt -s extglob
case $1 in
+([0-9])-+([0-9]) )
# do stuff
;;
esac
I am trying to set up a Minecraft server. However, the basic startup scripts provided do not fit my needs. I want a script that will:
Start a new screen running the jarfile and (pretty much) only the jarfile (so i can ^C it if needed without killing other things like screen or my gzip commands)
Gzip any logs that weren't gzipped automatically by the jarfile (for if/when i ^C'ed the server, or if it crashed)
Run a command with sudo to set the process in the first argument to a high priority (/usr/bin/oom-priority)
Run a http-server on the resource-pack directory in a different screen and send ^C to it when the server closes
I have these three commands. I run startserver to start the server.
startserver:
#!/bin/bash
set -m
cd /home/minecraftuser/server/
echo
screen -dm -S http-server http-server ./resource-pack
screen -dm -S my-mc-server startserver_command
(sleep 1; startserver_after) &
screen -S my-mc-server
startserver_command:
#!/bin/bash
set -m
cd /home/minecraftuser/server/
echo
java -Xmx768M -Xms768M -jar ./craftbukkit.jar $# &
env MC_PID=$! > /dev/null
(sleep 0.5; sudo /usr/bin/oom-priority $MC_PID) &
fg 1
echo
read -n 1 -p 'Press any key to continue...'
and startserver_after:
#!/bin/bash
cd /home/minecraftuser/server/
wait $MC_PID
find /home/minecraftuser/server/logs -type f -name "*.log" -print | while read file; do gzip $file &
done
screen -S http-server -p 0 -X stuff \^c\\r
Edit: When I run startserver, I get a command prompt then a bunch of gzip errors of files already existing (I am expecting these errors, but when I run startserver I'm supposed to get the java program). Somehow I am in a screen because when I do ^A d, I am brought to a new prompt.
Once I am out of the screen, screen -ls returns two instances of my-mc-server. One is a blank command prompt, the other is the server running successfully.
Edit 2: I changed startserver_command to remove the asterisk from env MC_PID=$! & (not needed there) and added it to (sleep 1; startserver_after) (makes it faster), redirected env line to /dev/null (removes entire environment listing at beginning of output). Still didn't fix the entire problem.
Instead of starting each screen session from the scripts, you can just use a custom .screenrc to specify some startup windows (and to run commands/scripts):
#$HOME/mc-server.screenrc
screen -t http-server 0 'startserver'
screen -t my-mc-server 1 'startserver_command'
screen -t gzip-logs 2 'startserver_after'
Then simply start screen (specifying the config file to use, if it's not the default ~/.screenrc)
screen -dm -c mc-server.screenrc
I decided to make a text based adventure and I realized I didn't know much about making one. I do, however, know that I want to make it with a batch file, just because I think it is easier to work with and share. I don't have many questions right now but I'm sure I'll come up with more as time goes on (if I decide this is fun) but right now I have two questions:
How do you make lines appear as if someone was typing it?
How do you make the line wait x seconds before going to the next process (you know for "dramatic effect")
edit I forgot to put the script that I need help with sorry (it's supposed to look like the "wake up neo" screen from The Matrix but I cant get the intervals smaller than 2 or hide the ping text underneath).
echo h
PING 127.0.0.1 -n 2
cls
echo he
PING 127.0.0.1 -n 2
cls
echo hel
PING 127.0.0.1 -n 2
cls
echo hell
PING 127.0.0.1 -n 2
cls
echo hello
PING 127.0.0.1 -n 3
cls
echo hello.
PING 127.0.0.1 -n 3
cls
echo hello..
PING 127.0.0.1 -n 3
cls
echo hello...
PING 127.0.0.1 -n 5
Wait/Delay [Source]
PING 127.0.0.1 -n 6 >nul
5 Second Delay
How it Works: 6 ping echos with default 1 second pause between them with loopback ip.
-n cannot be less than 2 or there will be no delay.
Delay < 1 Second
PING 10.1.1.1 -n 1 -w 200 >nul
200 Millisecond Delay by using a Private IP Address and the timeout flag -w. ( only adjust the -w value and leave -n as 1 when using this method )
Great Getting Started Resources
Rob van der Woude
SS64
DosTips
ComputerHope
TechNet
Example
Here is an example Typing routine that will print out each character of the message with a 200ms delay between each character.
#echo off
call :Typing "hello..."
exit /b 0
:Typing <Message>
setlocal
set "Message=%~1"
:TypingLoop
ping 10.1.1.1 -n 1 -w 200 >nul
<nul set /p "=%Message:~0,1%"
set "Message=%Message:~1%"
if defined Message goto TypingLoop
endlocal
exit /b 0
I want to use ping to check to see if a server is up. How would I do the following:
ping $URL
if [$? -eq 0]; then
echo "server live"
else
echo "server down"
fi
How would I accomplish the above? Also, how would I make it such that it returns 0 upon the first ping response, or returns an error if the first ten pings fail? Or, would there be a better way to accomplish what I am trying to do above?
I'ld recommend not to use only ping. It can check if a server is online in general but you can not check a specific service on that server.
Better use these alternatives:
curl
man curl
You can use curl and check the http_response for a webservice like this
check=$(curl -s -w "%{http_code}\n" -L "${HOST}${PORT}/" -o /dev/null)
if [[ $check == 200 || $check == 403 ]]
then
# Service is online
echo "Service is online"
exit 0
else
# Service is offline or not working correctly
echo "Service is offline or not working correctly"
exit 1
fi
where
HOST = [ip or dns-name of your host]
(optional )PORT = [optional a port; don't forget to start with :]
200 is the normal success http_response
403 is a redirect e.g. maybe to a login page so also accetable and most probably means the service runs correctly
-s Silent or quiet mode.
-L Defines the Location
-w In which format you want to display the response
-> %{http_code}\n we only want the http_code
-o the output file
-> /dev/null redirect any output to /dev/null so it isn't written to stdout or the check variable. Usually you would get the complete html source code before the http_response so you have to silence this, too.
nc
man nc
While curl to me seems the best option for Webservices since it is really checking if the service's webpage works correctly,
nc can be used to rapidly check only if a specific port on the target is reachable (and assume this also applies to the service).
Advantage here is the settable timeout of e.g. 1 second while curl might take a bit longer to fail, and of course you can check also services which are not a webpage like port 22 for SSH.
nc -4 -d -z -w 1 ${HOST} ${PORT} &> /dev/null
if [[ $? == 0 ]]
then
# Port is reached
echo "Service is online!"
exit 0
else
# Port is unreachable
echo "Service is offline!"
exit 1
fi
where
HOST = [ip or dns-name of your host]
PORT = [NOT optional the port]
-4 force IPv4 (or -6 for IPv6)
-d Do not attempt to read from stdin
-z Only listen, don't send data
-w timeout
If a connection and stdin are idle for more than timeout seconds, then the connection is silently closed. (In this case nc will exit 1 -> failure.)
(optional) -n If you only use an IP: Do not do any DNS or service lookups on any specified addresses, hostnames or ports.
&> /dev/null Don't print out any output of the command
You can use something like this -
serverResponse=`wget --server-response --max-redirect=0 ${URL} 2>&1`
if [[ $serverResponse == *"Connection refused"* ]]
then
echo "Unable to reach given URL"
exit 1
fi
Use the -c option with ping, it'll ping the URL only given number of times or until timeout
if ping -c 10 $URL; then
echo "server live"
else
echo "server down"
fi
Short form:
ping -c5 $SERVER || echo 'Server down'
Do you need it for some other script? Or are trying to hack some simple monitoring tool? In this case, you may want to take a look at Pingdom: https://www.pingdom.com/.
I using the following script function to check servers are online or not. It's useful when you want to check multiple servers. The function hide the ping output, and you can handle separately the server live or server down case.
#!/bin/bash
#retry count of ping request
RETRYCOUNT=1;
#pingServer: implement ping server functionality.
#Param1: server hostname to ping
function pingServer {
#echo Checking server: $1
ping -c $RETRYCOUNT $1 > /dev/null 2>&1
if [ $? -ne 0 ]
then
echo $1 down
else
echo $1 live
fi
}
#usage example, pinging some host
pingServer google.com
pingServer server1
One good solution is to use MRTG (a simple graphing tool for *NIX) with ping-probe script. look it up on Google.
read this for start.
Sample Graph: